UT: Reduce duplication of mock flask app initialization

This commit is contained in:
vakarisz 2022-05-23 16:19:42 +03:00
parent 0dc1bfc9f5
commit 0d0f4f63f3
2 changed files with 11 additions and 14 deletions

View File

@ -1,14 +1,12 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
import flask_jwt_extended import flask_jwt_extended
import flask_restful
import pytest 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.app
import monkey_island.cc.resources.auth.auth import monkey_island.cc.resources.auth.auth
import monkey_island.cc.resources.island_mode import monkey_island.cc.resources.island_mode
from monkey_island.cc.services.representations import output_json
@pytest.fixture @pytest.fixture
@ -18,7 +16,7 @@ def flask_client(monkeypatch_session):
container = MagicMock() container = MagicMock()
container.resolve_dependencies.return_value = [] 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 yield client
@ -27,19 +25,13 @@ def build_flask_client(monkeypatch_session):
def inner(container): def inner(container):
monkeypatch_session.setattr(flask_jwt_extended, "verify_jwt_in_request", lambda: None) 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 return inner
def mock_init_app(container): def get_mock_app(container):
app = Flask(__name__) app, api = init_mock_app()
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)
flask_resource_manager = monkey_island.cc.app.FlaskDIWrapper(api, container) flask_resource_manager = monkey_island.cc.app.FlaskDIWrapper(api, container)
monkey_island.cc.app.init_api_resources(flask_resource_manager) monkey_island.cc.app.init_api_resources(flask_resource_manager)

View File

@ -29,7 +29,7 @@ def create_empty_tmp_file(tmpdir: str) -> Callable:
return inner return inner
def mock_flask_resource_manager(container): def init_mock_app():
app = Flask(__name__) app = Flask(__name__)
app.config["SECRET_KEY"] = "test_key" app.config["SECRET_KEY"] = "test_key"
@ -37,6 +37,11 @@ def mock_flask_resource_manager(container):
api.representations = {"application/json": output_json} api.representations = {"application/json": output_json}
monkey_island.cc.app.init_app_url_rules(app) 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) flask_resource_manager = monkey_island.cc.app.FlaskDIWrapper(api, container)
return flask_resource_manager return flask_resource_manager