Island: Switch get_deployment_from_file() to use Paths

This commit is contained in:
Mike Salvatore 2021-09-14 10:19:14 -04:00
parent 38011f20b5
commit 412aefab3e
2 changed files with 15 additions and 25 deletions

View File

@ -1,6 +1,6 @@
import json
import logging
import os
from pathlib import Path
import requests
@ -8,6 +8,8 @@ from common.utils.exceptions import VersionServerConnectionError
from common.version import get_version
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
DEPLOYMENT_FILE_PATH = Path(MONKEY_ISLAND_ABS_PATH) / "cc" / "deployment.json"
logger = logging.getLogger(__name__)
@ -41,10 +43,9 @@ class VersionUpdateService:
Checks if newer monkey version is available
:return: False if not, version in string format ('1.6.2') otherwise
"""
deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json")
url = VersionUpdateService.VERSION_SERVER_CHECK_NEW_URL % (
VersionUpdateService.get_deployment_from_file(deployment_info_file_path),
VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(),
)
@ -64,15 +65,13 @@ class VersionUpdateService:
@staticmethod
def get_download_link():
deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json")
return VersionUpdateService.VERSION_SERVER_DOWNLOAD_URL % (
VersionUpdateService.get_deployment_from_file(deployment_info_file_path),
VersionUpdateService.get_deployment_from_file(DEPLOYMENT_FILE_PATH),
get_version(),
)
@staticmethod
def get_deployment_from_file(file_path: str) -> str:
def get_deployment_from_file(file_path: Path) -> str:
deployment = "unknown"
try:

View File

@ -1,4 +1,4 @@
import os
from pathlib import Path
import pytest
@ -6,30 +6,20 @@ from monkey_island.cc.services.version_update import VersionUpdateService
@pytest.fixture
def deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment.json")
monkeypatch.setattr(os.path, "join", lambda *args: path)
def deployment_info_file_path(data_for_tests_dir):
path = data_for_tests_dir / "deployment.json"
return path
@pytest.fixture
def ghost_file(monkeypatch):
path = "ghost_file"
monkeypatch.setattr(os.path, "join", lambda *args: path)
def key_error_deployment_info_file_path(data_for_tests_dir):
path = data_for_tests_dir / "deployment_key_error.json"
return path
@pytest.fixture
def key_error_deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_key_error.json")
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
@pytest.fixture
def flawed_deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_flawed")
monkeypatch.setattr(os.path, "join", lambda *args: path)
def flawed_deployment_info_file_path(data_for_tests_dir):
path = data_for_tests_dir / "deployment_flawed"
return path
@ -38,7 +28,8 @@ def test_get_deployment_field_from_file(deployment_info_file_path):
assert deployment == "develop"
def test_get_deployment_field_from_nonexistent_file(ghost_file):
def test_get_deployment_field_from_nonexistent_file():
ghost_file = Path("ghost_file")
deployment = VersionUpdateService().get_deployment_from_file(ghost_file)
assert deployment == "unknown"