Island: Add inital get method to island mode

This commit is contained in:
Ilija Lazoroski 2021-07-13 18:26:11 +02:00
parent 3c84e70ab1
commit 5fe7a9d204
3 changed files with 29 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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