From 2778b69dfb4ef6a9f0c259a4c02c1fed8331f5af Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 12 Jul 2021 15:59:36 +0300 Subject: [PATCH] Island: Add unit test infrastructure for testing resources --- .../monkey_island/cc/resources/conftest.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py b/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py new file mode 100644 index 000000000..0e82fe163 --- /dev/null +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/conftest.py @@ -0,0 +1,32 @@ +import flask_jwt_extended +import flask_restful +import pytest +from flask import Flask + +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 + + +# We can't scope to module, because monkeypatch is a function scoped decorator. +# Potential solutions: https://github.com/pytest-dev/pytest/issues/363#issuecomment-406536200 or +# https://stackoverflow.com/questions/53963822/python-monkeypatch-setattr-with-pytest-fixture-at-module-scope +@pytest.fixture(scope="function") +def flask_client(monkeypatch): + monkeypatch.setattr(flask_jwt_extended, "verify_jwt_in_request", lambda: None) + + with mock_init_app().test_client() as client: + yield client + + +def mock_init_app(): + app = Flask(__name__) + + api = flask_restful.Api(app) + api.representations = {"application/json": output_json} + + monkey_island.cc.app.init_app_url_rules(app) + monkey_island.cc.app.init_api_resources(api) + + return app