forked from p15670423/monkey
Merge pull request #2145 from guardicore/2106-remove-mode-keyword
2106 remove mode keyword
This commit is contained in:
commit
17b264d28b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -111,9 +111,16 @@ const IslandResetModal = (props: Props) => {
|
|||
if (res.ok) {
|
||||
return auth.authFetch('/api/propagation-credentials/configured-credentials', {method: 'PUT', body:'[]'})
|
||||
}})
|
||||
.then(res => {
|
||||
.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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue