From 5fe7a9d2046c7247013cdd640d09b5d4d21f9979 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Tue, 13 Jul 2021 18:26:11 +0200 Subject: [PATCH 1/2] Island: Add inital get method to island mode --- monkey/monkey_island/cc/resources/island_mode.py | 10 +++++++++- .../cc/services/mode/island_mode_service.py | 5 +++++ .../cc/resources/test_island_mode.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/resources/island_mode.py b/monkey/monkey_island/cc/resources/island_mode.py index 51df90e2d..77e1e6953 100644 --- a/monkey/monkey_island/cc/resources/island_mode.py +++ b/monkey/monkey_island/cc/resources/island_mode.py @@ -5,7 +5,7 @@ import flask_restful from flask import make_response, request from monkey_island.cc.resources.auth.auth import jwt_required -from monkey_island.cc.services.mode.island_mode_service import set_mode +from monkey_island.cc.services.mode.island_mode_service import get_mode, set_mode from monkey_island.cc.services.mode.mode_enum import IslandModeEnum logger = logging.getLogger(__name__) @@ -26,3 +26,11 @@ class IslandMode(flask_restful.Resource): return make_response({}, 400) except ValueError: return make_response({}, 422) + + @jwt_required + def get(self): + try: + island_mode = get_mode() + return make_response({"mode": island_mode}, 200) + except IndexError: + return make_response({}, 422) diff --git a/monkey/monkey_island/cc/services/mode/island_mode_service.py b/monkey/monkey_island/cc/services/mode/island_mode_service.py index 05195e301..e850c64a7 100644 --- a/monkey/monkey_island/cc/services/mode/island_mode_service.py +++ b/monkey/monkey_island/cc/services/mode/island_mode_service.py @@ -6,3 +6,8 @@ def set_mode(mode: IslandModeEnum): island_mode_model = IslandMode() island_mode_model.mode = mode.value island_mode_model.save() + + +def get_mode(): + mode = IslandMode.objects[0].mode + return mode diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py index b8d6a84c2..c06c6f62e 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py @@ -54,3 +54,18 @@ def test_island_mode_post__set_invalid_model(flask_client, uses_database): "/api/island-mode", data=json.dumps({"mode": "bogus mode"}), follow_redirects=True ) assert len(IslandMode.objects) == 0 + + +@pytest.mark.parametrize("mode", ["ransomware", "advanced"]) +def test_island_mode_get(flask_client, uses_database, mode): + flask_client.post("/api/island-mode", data=json.dumps({"mode": mode}), follow_redirects=True) + resp = flask_client.get("/api/island-mode", follow_redirects=True) + assert json.loads(resp.data)["mode"] == mode + + +def test_island_mode_get__invalid_mode(flask_client, uses_database): + flask_client.post( + "/api/island-mode", data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True + ) + resp = flask_client.get("/api/island-mode", follow_redirects=True) + assert resp.status_code == 422 From 56b5e8bb87455d335cc884b0b4aa1ea40fc15d16 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 14 Jul 2021 11:09:57 +0200 Subject: [PATCH 2/2] Tests: Remove post tests that interact with the model --- .../cc/resources/test_island_mode.py | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py index c06c6f62e..f4fb8a797 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_island_mode.py @@ -42,28 +42,15 @@ def test_island_mode_post__internal_server_error(monkeypatch, flask_client): assert resp.status_code == 500 -def test_island_mode_post__set_model(flask_client, uses_database): - flask_client.post( - "/api/island-mode", data=json.dumps({"mode": "ransomware"}), follow_redirects=True - ) - assert IslandMode.objects[0].mode == "ransomware" - - -def test_island_mode_post__set_invalid_model(flask_client, uses_database): - flask_client.post( - "/api/island-mode", data=json.dumps({"mode": "bogus mode"}), follow_redirects=True - ) - assert len(IslandMode.objects) == 0 - - @pytest.mark.parametrize("mode", ["ransomware", "advanced"]) -def test_island_mode_get(flask_client, uses_database, mode): +def test_island_mode_endpoint(flask_client, uses_database, mode): flask_client.post("/api/island-mode", data=json.dumps({"mode": mode}), follow_redirects=True) resp = flask_client.get("/api/island-mode", follow_redirects=True) + assert resp.status_code == 200 assert json.loads(resp.data)["mode"] == mode -def test_island_mode_get__invalid_mode(flask_client, uses_database): +def test_island_mode_endpoint__invalid_mode(flask_client, uses_database): flask_client.post( "/api/island-mode", data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True )