From f60dcd17a096a1c266f6730d5ca3ceb2512aae1f Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Tue, 1 Jun 2021 10:36:21 +0200 Subject: [PATCH] added api tests --- brian_polling_manager/api.py | 4 ++-- test/conftest.py | 8 ++++++++ test/test_api.py | 38 ++++++++++++++++++++++++++++++++++++ test/test_e2e.py | 18 +++++++---------- 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 test/test_api.py diff --git a/brian_polling_manager/api.py b/brian_polling_manager/api.py index 9e7fbda..424813f 100644 --- a/brian_polling_manager/api.py +++ b/brian_polling_manager/api.py @@ -23,7 +23,7 @@ import pkg_resources from flask import Blueprint, current_app, request, Response, jsonify from brian_polling_manager import CONFIG_KEY -from brian_polling_manager.main import refresh +from brian_polling_manager import main routes = Blueprint("api-routes", __name__) logger = logging.getLogger(__name__) @@ -159,5 +159,5 @@ def update(): :return: """ - response = refresh(config=current_app.config[CONFIG_KEY]) + response = main.refresh(config=current_app.config[CONFIG_KEY]) return jsonify(response) diff --git a/test/conftest.py b/test/conftest.py index 9244fd8..2553e8f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -55,6 +55,14 @@ def config(): } +@pytest.fixture +def config_filename(config): + with tempfile.NamedTemporaryFile(mode='w') as f: + f.write(json.dumps(config)) + f.flush() + yield f.name + + @pytest.fixture def mocked_sensu(): diff --git a/test/test_api.py b/test/test_api.py new file mode 100644 index 0000000..c8df1ff --- /dev/null +++ b/test/test_api.py @@ -0,0 +1,38 @@ +import json +import os + +import jsonschema +import pytest +import responses + +import brian_polling_manager +from brian_polling_manager.api import VERSION_SCHEMA +from brian_polling_manager.main import REFRESH_RESULT_SCHEMA + + +@pytest.fixture +def client(config_filename, mocked_sensu, mocked_inventory): + os.environ['CONFIG_FILENAME'] = config_filename + with brian_polling_manager.create_app().test_client() as c: + yield c + + +def test_version(client): + rv = client.get( + '/api/version', + headers={'Accept': ['application/json']}) + assert rv.status_code == 200 + assert rv.is_json + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, VERSION_SCHEMA) + +@responses.activate +def test_update(client): + rv = client.get( + '/api/update', + headers={'Accept': ['application/json']}) + assert rv.status_code == 200 + assert rv.is_json + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, REFRESH_RESULT_SCHEMA) + diff --git a/test/test_e2e.py b/test/test_e2e.py index 11b6d74..ae130cf 100644 --- a/test/test_e2e.py +++ b/test/test_e2e.py @@ -8,15 +8,11 @@ from brian_polling_manager import main @responses.activate -def test_run_flashtest(config, mocked_sensu, mocked_inventory): +def test_run_flashtest(config_filename, mocked_sensu, mocked_inventory): - with tempfile.NamedTemporaryFile(mode='w') as f: - f.write(json.dumps(config)) - f.flush() - - runner = CliRunner() - result = runner.invoke( - main.cli, - ['--config', f.name, '--force'] - ) - assert result.exit_code == 0 + runner = CliRunner() + result = runner.invoke( + main.cli, + ['--config', config_filename, '--force'] + ) + assert result.exit_code == 0 -- GitLab