UT: Refactor hardcoded URL's to resource references

This commit is contained in:
vakarisz 2022-05-23 14:51:42 +03:00
parent 4069cc8084
commit 0344ee1a7c
4 changed files with 23 additions and 17 deletions

View File

@ -4,6 +4,7 @@ from unittest.mock import MagicMock
import pytest
from common.utils.exceptions import IncorrectCredentialsError
from monkey_island.cc.resources.auth.auth import Authenticate
USERNAME = "test_user"
PASSWORD = "test_password"
@ -22,7 +23,7 @@ def mock_authentication_service(monkeypatch):
@pytest.fixture
def make_auth_request(flask_client):
url = "/api/auth"
url = Authenticate.urls[0]
def inner(request_body):
return flask_client.post(url, data=request_body, follow_redirects=True)

View File

@ -4,8 +4,9 @@ from unittest.mock import MagicMock
import pytest
from common.utils.exceptions import AlreadyRegisteredError, InvalidRegistrationCredentialsError
from monkey_island.cc.resources.auth.registration import Registration
REGISTRATION_URL = "/api/registration"
REGISTRATION_URL = Registration.urls[0]
USERNAME = "test_user"
PASSWORD = "test_password"

View File

@ -5,6 +5,7 @@ from tests.utils import raise_
from monkey_island.cc.models.island_mode_model import IslandMode
from monkey_island.cc.resources import island_mode as island_mode_resource
from monkey_island.cc.resources.island_mode import IslandMode as IslandModeResource
@pytest.fixture(scope="function")
@ -19,21 +20,21 @@ def test_island_mode_post(flask_client, mode, monkeypatch):
lambda _: None,
)
resp = flask_client.post(
"/api/island-mode", data=json.dumps({"mode": mode}), follow_redirects=True
IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True
)
assert resp.status_code == 200
def test_island_mode_post__invalid_mode(flask_client):
resp = flask_client.post(
"/api/island-mode", data=json.dumps({"mode": "bogus mode"}), follow_redirects=True
IslandModeResource.urls[0], data=json.dumps({"mode": "bogus mode"}), follow_redirects=True
)
assert resp.status_code == 422
@pytest.mark.parametrize("invalid_json", ["42", "{test"])
def test_island_mode_post__invalid_json(flask_client, invalid_json):
resp = flask_client.post("/api/island-mode", data="{test", follow_redirects=True)
resp = flask_client.post(IslandModeResource.urls[0], data="{test", follow_redirects=True)
assert resp.status_code == 400
@ -41,23 +42,25 @@ def test_island_mode_post__internal_server_error(monkeypatch, flask_client):
monkeypatch.setattr(island_mode_resource, "set_mode", lambda x: raise_(Exception()))
resp = flask_client.post(
"/api/island-mode", data=json.dumps({"mode": "ransomware"}), follow_redirects=True
IslandModeResource.urls[0], data=json.dumps({"mode": "ransomware"}), follow_redirects=True
)
assert resp.status_code == 500
@pytest.mark.parametrize("mode", ["ransomware", "advanced"])
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)
flask_client.post(
IslandModeResource.urls[0], data=json.dumps({"mode": mode}), follow_redirects=True
)
resp = flask_client.get(IslandModeResource.urls[0], follow_redirects=True)
assert resp.status_code == 200
assert json.loads(resp.data)["mode"] == mode
def test_island_mode_endpoint__invalid_mode(flask_client, uses_database):
resp_post = flask_client.post(
"/api/island-mode", data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True
IslandModeResource.urls[0], data=json.dumps({"mode": "bogus_mode"}), follow_redirects=True
)
resp_get = flask_client.get("/api/island-mode", follow_redirects=True)
resp_get = flask_client.get(IslandModeResource.urls[0], follow_redirects=True)
assert resp_post.status_code == 422
assert json.loads(resp_get.data)["mode"] is None

View File

@ -4,6 +4,7 @@ from unittest.mock import MagicMock
import pytest
from tests.common import StubDIContainer
from monkey_island.cc.resources import RemoteRun
from monkey_island.cc.services import AWSService
from monkey_island.cc.services.aws import AWSCommandResults, AWSCommandStatus
@ -23,18 +24,18 @@ def flask_client(build_flask_client, mock_aws_service):
def test_get_invalid_action(flask_client):
response = flask_client.get("/api/remote-monkey?action=INVALID")
response = flask_client.get(f"{RemoteRun.urls[0]}?action=INVALID")
assert response.text.rstrip() == "{}"
def test_get_no_action(flask_client):
response = flask_client.get("/api/remote-monkey")
response = flask_client.get(RemoteRun.urls[0])
assert response.text.rstrip() == "{}"
def test_get_not_aws(flask_client, mock_aws_service):
mock_aws_service.island_is_running_on_aws = MagicMock(return_value=False)
response = flask_client.get("/api/remote-monkey?action=list_aws")
response = flask_client.get(f"{RemoteRun.urls[0]}?action=list_aws")
assert response.text.rstrip() == '{"is_aws":false}'
@ -47,7 +48,7 @@ def test_get_instances(flask_client, mock_aws_service):
mock_aws_service.island_is_running_on_aws = MagicMock(return_value=True)
mock_aws_service.get_managed_instances = MagicMock(return_value=instances)
response = flask_client.get("/api/remote-monkey?action=list_aws")
response = flask_client.get(f"{RemoteRun.urls[0]}?action=list_aws")
assert json.loads(response.text)["instances"] == instances
assert json.loads(response.text)["is_aws"] is True
@ -57,12 +58,12 @@ def test_get_instances(flask_client, mock_aws_service):
def test_post_no_type(flask_client):
response = flask_client.post("/api/remote-monkey", data="{}")
response = flask_client.post(RemoteRun.urls[0], data="{}")
assert response.status_code == 500
def test_post_invalid_type(flask_client):
response = flask_client.post("/api/remote-monkey", data='{"type": "INVALID"}')
response = flask_client.post(RemoteRun.urls[0], data='{"type": "INVALID"}')
assert response.status_code == 500
@ -103,6 +104,6 @@ def test_post(flask_client, mock_aws_service):
},
]
response = flask_client.post("/api/remote-monkey", data=request_body)
response = flask_client.post(RemoteRun.urls[0], data=request_body)
assert json.loads(response.text)["result"] == expected_result