From 412aefab3eb2494817dff0425c0730b554a77d85 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 14 Sep 2021 10:19:14 -0400 Subject: [PATCH] Island: Switch get_deployment_from_file() to use Paths --- .../cc/services/version_update.py | 13 +++++---- .../cc/services/test_version_update.py | 27 +++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 25872a45a..0430ba5be 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -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: diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py index 4824ddfed..e7fe7bdf5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_version_update.py @@ -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"