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