From 0d0f4f63f3b60408eed825b6a56000047ecf2aba Mon Sep 17 00:00:00 2001 From: vakarisz Date: Mon, 23 May 2022 16:19:42 +0300 Subject: [PATCH] UT: Reduce duplication of mock flask app initialization --- .../monkey_island/cc/resources/conftest.py | 18 +++++------------- .../tests/unit_tests/monkey_island/conftest.py | 7 ++++++- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py b/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py index 723c777b8..a40766d5e 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py @@ -1,14 +1,12 @@ from unittest.mock import MagicMock import flask_jwt_extended -import flask_restful import pytest -from flask import Flask +from tests.unit_tests.monkey_island.conftest import init_mock_app import monkey_island.cc.app import monkey_island.cc.resources.auth.auth import monkey_island.cc.resources.island_mode -from monkey_island.cc.services.representations import output_json @pytest.fixture @@ -18,7 +16,7 @@ def flask_client(monkeypatch_session): container = MagicMock() container.resolve_dependencies.return_value = [] - with mock_init_app(container).test_client() as client: + with get_mock_app(container).test_client() as client: yield client @@ -27,19 +25,13 @@ def build_flask_client(monkeypatch_session): def inner(container): monkeypatch_session.setattr(flask_jwt_extended, "verify_jwt_in_request", lambda: None) - return mock_init_app(container).test_client() + return get_mock_app(container).test_client() return inner -def mock_init_app(container): - app = Flask(__name__) - app.config["SECRET_KEY"] = "test_key" - - api = flask_restful.Api(app) - api.representations = {"application/json": output_json} - - monkey_island.cc.app.init_app_url_rules(app) +def get_mock_app(container): + app, api = init_mock_app() flask_resource_manager = monkey_island.cc.app.FlaskDIWrapper(api, container) monkey_island.cc.app.init_api_resources(flask_resource_manager) diff --git a/monkey/tests/unit_tests/monkey_island/conftest.py b/monkey/tests/unit_tests/monkey_island/conftest.py index 03cc23259..d3c986df3 100644 --- a/monkey/tests/unit_tests/monkey_island/conftest.py +++ b/monkey/tests/unit_tests/monkey_island/conftest.py @@ -29,7 +29,7 @@ def create_empty_tmp_file(tmpdir: str) -> Callable: return inner -def mock_flask_resource_manager(container): +def init_mock_app(): app = Flask(__name__) app.config["SECRET_KEY"] = "test_key" @@ -37,6 +37,11 @@ def mock_flask_resource_manager(container): api.representations = {"application/json": output_json} monkey_island.cc.app.init_app_url_rules(app) + return app, api + + +def mock_flask_resource_manager(container): + _, api = init_mock_app() flask_resource_manager = monkey_island.cc.app.FlaskDIWrapper(api, container) return flask_resource_manager