Merge pull request #2145 from guardicore/2106-remove-mode-keyword

2106 remove mode keyword
This commit is contained in:
Mike Salvatore 2022-08-03 08:55:56 -04:00 committed by GitHub
commit 17b264d28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 25 deletions

View File

@ -120,8 +120,8 @@ class MonkeyIslandClient(object):
assert False
def _reset_island_mode(self):
if self.requests.put("api/island/mode", data='{"mode": "unset"}').ok:
LOGGER.info("Resseting island mode after the test.")
if self.requests.put("api/island/mode", data='"unset"').ok:
LOGGER.info("Resetting island mode after the test.")
else:
LOGGER.error("Failed to reset island mode")
assert False

View File

@ -2,7 +2,7 @@ import json
import logging
from http import HTTPStatus
from flask import make_response, request
from flask import request
from monkey_island.cc.models import IslandMode as IslandModeEnum
from monkey_island.cc.resources.AbstractResource import AbstractResource
@ -21,8 +21,7 @@ class IslandMode(AbstractResource):
@jwt_required
def put(self):
try:
body = json.loads(request.data)
mode = IslandModeEnum(body.get("mode"))
mode = IslandModeEnum(request.json)
self._island_mode_service.set_mode(mode)
@ -35,4 +34,4 @@ class IslandMode(AbstractResource):
@jwt_required
def get(self):
island_mode = self._island_mode_service.get_mode()
return make_response({"mode": island_mode.value}, HTTPStatus.OK)
return island_mode.value, HTTPStatus.OK

View File

@ -20,7 +20,7 @@ class IslandHttpClient extends AuthComponent {
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(contents)
})
.then(res => {status = res.status; return res.json()})
.then(res => {status = res.status; return res})
.then(res => new Response(res, status));
}

View File

@ -115,7 +115,7 @@ class AppComponent extends AuthComponent {
setMode = () => {
return IslandHttpClient.get('/api/island/mode')
.then(res => {
this.setState({islandMode: res.body.mode});
this.setState({islandMode: res.body});
});
}

View File

@ -74,7 +74,7 @@ const LandingPageComponent = (props: Props) => {
}
function setScenario(scenario: string) {
IslandHttpClient.put('/api/island/mode', {'mode': scenario})
IslandHttpClient.put('/api/island/mode', scenario)
.then(() => {
props.onStatusChange();
});

View File

@ -113,7 +113,14 @@ const IslandResetModal = (props: Props) => {
}})
.then(res => {
if (res.ok) {
return auth.authFetch('/api/island/mode', {method: 'PUT', body: '{"mode": "unset"}'})
return auth.authFetch(
'/api/island/mode',
{
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: '"unset"'
}
)
}})
.then(res => {
if (!res.ok) {

View File

@ -1,4 +1,3 @@
import json
from http import HTTPStatus
from unittest.mock import MagicMock
@ -38,24 +37,22 @@ def flask_client(build_flask_client):
)
def test_island_mode_post(flask_client, mode):
resp = flask_client.put(
IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True
IslandModeResource.urls[0],
json=mode,
follow_redirects=True,
)
assert resp.status_code == HTTPStatus.NO_CONTENT
def test_island_mode_post__invalid_mode(flask_client):
resp = flask_client.put(
IslandModeResource.urls[0], data=json.dumps({"mode": "bogus mode"}), follow_redirects=True
IslandModeResource.urls[0],
json="bogus mode",
follow_redirects=True,
)
assert resp.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
@pytest.mark.parametrize("invalid_json", ["42", "{test"])
def test_island_mode_post__invalid_json(flask_client, invalid_json):
resp = flask_client.put(IslandModeResource.urls[0], data="{test", follow_redirects=True)
assert resp.status_code == HTTPStatus.BAD_REQUEST
def test_island_mode_post__internal_server_error(build_flask_client):
mock_island_mode_service = MagicMock(spec=IslandModeService)
mock_island_mode_service.set_mode = MagicMock(side_effect=RetrievalError)
@ -66,7 +63,7 @@ def test_island_mode_post__internal_server_error(build_flask_client):
with build_flask_client(container) as flask_client:
resp = flask_client.put(
IslandModeResource.urls[0],
data=json.dumps({"mode": IslandMode.RANSOMWARE.value}),
json=IslandMode.RANSOMWARE.value,
follow_redirects=True,
)
@ -76,17 +73,21 @@ def test_island_mode_post__internal_server_error(build_flask_client):
@pytest.mark.parametrize("mode", [IslandMode.RANSOMWARE.value, IslandMode.ADVANCED.value])
def test_island_mode_endpoint(flask_client, mode):
flask_client.put(
IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True
IslandModeResource.urls[0],
json=mode,
follow_redirects=True,
)
resp = flask_client.get(IslandModeResource.urls[0], follow_redirects=True)
assert resp.status_code == HTTPStatus.OK
assert json.loads(resp.data)["mode"] == mode
assert resp.json == mode
def test_island_mode_endpoint__invalid_mode(flask_client):
resp_post = flask_client.put(
IslandModeResource.urls[0], data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True
IslandModeResource.urls[0],
json="bogus_mode",
follow_redirects=True,
)
resp_get = flask_client.get(IslandModeResource.urls[0], follow_redirects=True)
assert resp_post.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert json.loads(resp_get.data)["mode"] == IslandMode.UNSET.value
assert resp_get.json == IslandMode.UNSET.value