From 8fff1f5372c4f5e0c3c71c6669ef62cbfc2e3a91 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Mon, 13 Dec 2021 12:40:40 +0100 Subject: [PATCH] [tests] fix tests and add "mix_stderr=False" to CliRunner instance --- halfapi/testing/test_domain.py | 28 +++++++----- tests/conftest.py | 4 +- tests/dummy_domain/acl.py | 9 +--- .../routers/arguments/__init__.py | 34 +++++++++++++++ tests/dummy_domain/routers/config/__init__.py | 2 +- tests/test_debug_routes.py | 43 ++++++++++++++----- tests/test_dummy_domain.py | 2 +- 7 files changed, 91 insertions(+), 31 deletions(-) diff --git a/halfapi/testing/test_domain.py b/halfapi/testing/test_domain.py index 34397c8..fba5d1f 100644 --- a/halfapi/testing/test_domain.py +++ b/halfapi/testing/test_domain.py @@ -38,22 +38,30 @@ class TestDomain(TestCase): return wrapper class_.invoke = invoke_wrapper(class_.invoke) - self.runner = class_() + self.runner = class_(mix_stderr=False) def tearDown(self): pass def check_domain(self): - result = self.runner.invoke(cli, '--version') - self.assertEqual(result.exit_code, 0) - print(result.stdout) - result = self.runner.invoke(cli, ['domain', self.DOMAIN]) - self.assertEqual(result.exit_code, 0) - result_d = json.loads(result.stdout) - result = self.runner.invoke(cli, ['run', '--dryrun', self.DOMAIN]) - print(result.stdout) - self.assertEqual(result.exit_code, 0) + result = None + try: + result = self.runner.invoke(cli, '--version') + self.assertEqual(result.exit_code, 0) + result = self.runner.invoke(cli, ['domain', self.DOMAIN]) + self.assertEqual(result.exit_code, 0) + result_d = json.loads(result.stdout) + result = self.runner.invoke(cli, ['run', '--help']) + self.assertEqual(result.exit_code, 0) + result = self.runner.invoke(cli, ['run', '--dryrun', self.DOMAIN]) + self.assertEqual(result.exit_code, 0) + except AssertionError as exc: + print(f'Result {result}') + print(f'Stdout {result.stdout}') + print(f'Stderr {result.stderr}') + raise exc + return result_d diff --git a/tests/conftest.py b/tests/conftest.py index 313a089..b5de1e6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -76,7 +76,7 @@ def token_debug_true_builder(): @pytest.fixture def runner(): - return CliRunner() + return CliRunner(mix_stderr=False) @pytest.fixture @@ -104,7 +104,7 @@ def cli_runner(): return wrapper class_.invoke = invoke_wrapper(class_.invoke) - cli_runner_ = class_() + cli_runner_ = class_(mix_stderr=False) yield cli_runner_ diff --git a/tests/dummy_domain/acl.py b/tests/dummy_domain/acl.py index 7787180..b8f9a8f 100644 --- a/tests/dummy_domain/acl.py +++ b/tests/dummy_domain/acl.py @@ -1,5 +1,5 @@ from halfapi.lib import acl -from halfapi.lib.acl import public +from halfapi.lib.acl import public, private from random import randint def random(*args): @@ -7,13 +7,8 @@ def random(*args): """ return randint(0,1) == 1 -def denied(*args): - """ Access denied - """ - return False - ACLS = ( ('public', public.__doc__, 999), ('random', random.__doc__, 10), - ('denied', denied.__doc__, 0) + ('private', private.__doc__, 0) ) diff --git a/tests/dummy_domain/routers/arguments/__init__.py b/tests/dummy_domain/routers/arguments/__init__.py index 5ea8f53..81f6c42 100644 --- a/tests/dummy_domain/routers/arguments/__init__.py +++ b/tests/dummy_domain/routers/arguments/__init__.py @@ -26,7 +26,33 @@ ACLS = { } } }, + ], + 'POST' : [ + { + 'acl':acl.private, + 'args': { + 'required': { + 'foo', 'bar' + }, + 'optional': { + 'x' + } + } + + }, + { + 'acl':acl.public, + 'args': { + 'required': { + 'foo', 'baz' + }, + 'optional': { + 'truebidoo' + } + } + }, ] + } def get(halfapi, data): @@ -36,3 +62,11 @@ def get(halfapi, data): """ logger.error('%s', data['foo']) return {'foo': data['foo'], 'bar': data['bar']} + +def post(halfapi, data): + """ + description: + returns the configuration of the domain + """ + logger.error('%s', data) + return {'foo': data['foo'], 'bar': data.get('bar', data.get('baz'))} diff --git a/tests/dummy_domain/routers/config/__init__.py b/tests/dummy_domain/routers/config/__init__.py index 3cf45b0..70e6a33 100644 --- a/tests/dummy_domain/routers/config/__init__.py +++ b/tests/dummy_domain/routers/config/__init__.py @@ -14,4 +14,4 @@ def get(halfapi): returns the configuration of the domain """ logger.error('%s', halfapi) - return halfapi['config'] + return halfapi['config']['domain']['dummy_domain']['config'] diff --git a/tests/test_debug_routes.py b/tests/test_debug_routes.py index 7dfe46f..139f157 100644 --- a/tests/test_debug_routes.py +++ b/tests/test_debug_routes.py @@ -10,33 +10,56 @@ import pprint from halfapi.lib.constants import API_SCHEMA -def test_routes(application_debug): +def test_halfapi_whoami(application_debug): # @TODO : If we use isolated filesystem multiple times that creates a bug. # So we use a single function with fixture "application debug" c = TestClient(application_debug) r = c.get('/halfapi/whoami') assert r.status_code == 200 + +def test_halfapi_log(application_debug): + # @TODO : If we use isolated filesystem multiple times that creates a bug. + # So we use a single function with fixture "application debug" + + c = TestClient(application_debug) + r = c.get('/halfapi/log') assert r.status_code == 200 + +def test_halfapi_error_400(application_debug): + # @TODO : If we use isolated filesystem multiple times that creates a bug. + # So we use a single function with fixture "application debug" + + c = TestClient(application_debug) + r = c.get('/halfapi/error/400') assert r.status_code == 400 + +def test_halfapi_error_404(application_debug): + # @TODO : If we use isolated filesystem multiple times that creates a bug. + # So we use a single function with fixture "application debug" + + c = TestClient(application_debug) + r = c.get('/halfapi/error/404') assert r.status_code == 404 + +def test_halfapi_error_500(application_debug): + # @TODO : If we use isolated filesystem multiple times that creates a bug. + # So we use a single function with fixture "application debug" + + c = TestClient(application_debug) + r = c.get('/halfapi/error/500') assert r.status_code == 500 + +def test_schema(application_debug): + c = TestClient(application_debug) + r = c.get('/') schemas = r.json() assert isinstance(schemas, list) for schema in schemas: assert isinstance(schema, dict) assert API_SCHEMA.validate(schema) - - """ - TODO: Find a way to test exception raising - try: - r = c.get('/halfapi/exception') - assert r.status_code == 500 - except Exception: - print('exception') - """ diff --git a/tests/test_dummy_domain.py b/tests/test_dummy_domain.py index 793b0e8..56da1b1 100644 --- a/tests/test_dummy_domain.py +++ b/tests/test_dummy_domain.py @@ -5,7 +5,7 @@ def test_dummy_domain(): from .dummy_domain import acl assert acl.public() is True assert isinstance(acl.random(), int) - assert acl.denied() is False + assert acl.private() is False from .dummy_domain import routers