From 9fd6ea9598306ec9e3a3cef71dc2858fee299130 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 14 Sep 2021 14:02:24 +0530 Subject: [PATCH] island, tests: Modify function to get deployment type with file path as input and modify related tests --- monkey/monkey_island/cc/services/version_update.py | 14 ++++++++------ .../cc/services/test_version_update.py | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/monkey/monkey_island/cc/services/version_update.py b/monkey/monkey_island/cc/services/version_update.py index 473c96ff1..5bf773c6b 100644 --- a/monkey/monkey_island/cc/services/version_update.py +++ b/monkey/monkey_island/cc/services/version_update.py @@ -1,7 +1,6 @@ import json import logging import os -from typing import Optional import requests @@ -42,8 +41,10 @@ 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_file(), + VersionUpdateService.get_deployment_from_file(deployment_info_file_path), get_version(), ) @@ -63,17 +64,18 @@ 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_file(), + VersionUpdateService.get_deployment_from_file(deployment_info_file_path), get_version(), ) @staticmethod - def get_deployment_file() -> Optional[str]: + def get_deployment_from_file(file_path: str) -> str: deployment = "unknown" - deployment_info_file_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "deployment.json") - with open(deployment_info_file_path, "r") as deployment_info_file: + with open(file_path, "r") as deployment_info_file: deployment_info = json.load(deployment_info_file) deployment = deployment_info["deployment"] 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 01ebb81d5..84cb45535 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 @@ -6,11 +6,11 @@ from monkey_island.cc.services.version_update import VersionUpdateService @pytest.fixture -def deployment_file(data_for_tests_dir): +def deployment_info_file_path(data_for_tests_dir): return os.path.join(data_for_tests_dir, "deployment.json") -def test_get_deployment_field(deployment_file, monkeypatch): - monkeypatch.setattr(os.path, "join", lambda *args: deployment_file) - deployment = VersionUpdateService().get_deployment_file() +def test_get_deployment_field(deployment_info_file_path, monkeypatch): + monkeypatch.setattr(os.path, "join", lambda *args: deployment_info_file_path) + deployment = VersionUpdateService().get_deployment_from_file(deployment_info_file_path) assert deployment == "develop"