From 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Thu, 17 Jun 2021 18:52:18 +0200 Subject: [PATCH] [tests] update tests for 0.5.3 --- tests/conftest.py | 30 +++++++++++++ .../{routers => }/act/__init__.py | 0 .../{routers => }/act/personne/__init__.py | 0 .../{routers => }/act/personne/eo.py | 0 .../{test:uuid => TEST_uuid}/__init__.py | 0 .../routers/abc/alphabet/__init__.py | 2 +- .../routers/abc/pinnochio/__init__.py | 2 +- tests/test_debug_routes.py | 26 ++++------- tests/test_dummy_project_router.py | 42 +++++++++++++----- tests/test_lib_domain.py | 40 ++++++++++++----- tests/test_lib_router.py | 44 +++++++++++++++++++ tests/test_lib_schemas.py | 20 +++++++-- 12 files changed, 160 insertions(+), 46 deletions(-) rename tests/dummy_domain/{routers => }/act/__init__.py (100%) rename tests/dummy_domain/{routers => }/act/personne/__init__.py (100%) rename tests/dummy_domain/{routers => }/act/personne/eo.py (100%) rename tests/dummy_domain/routers/abc/alphabet/{test:uuid => TEST_uuid}/__init__.py (100%) create mode 100644 tests/test_lib_router.py diff --git a/tests/conftest.py b/tests/conftest.py index 4a68feb..c75a14c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,6 +11,7 @@ from uuid import uuid1, uuid4, UUID import click from click.testing import CliRunner import jwt +import sys from unittest.mock import patch import pytest from starlette.applications import Starlette @@ -251,6 +252,7 @@ def create_route(): @pytest.fixture def dummy_project(): + sys.path.insert(0, './tests') halfapi_config = tempfile.mktemp() halfapi_secret = tempfile.mktemp() domain = 'dummy_domain' @@ -271,3 +273,31 @@ def dummy_project(): f.write('turlututu') return (halfapi_config, 'dummy_domain', 'routers') + +@pytest.fixture +def routers(): + sys.path.insert(0, './tests') + + from dummy_domain import routers + return routers + + +@pytest.fixture +def application_debug(): + from halfapi.app import HalfAPI + return HalfAPI({ + 'SECRET':'turlututu', + 'PRODUCTION':False + }).application + + +@pytest.fixture +def application_domain(routers): + from halfapi.app import HalfAPI + return HalfAPI({ + 'SECRET':'turlututu', + 'PRODUCTION':True, + 'DOMAINS':{'dummy_domain':routers} + }).application + + diff --git a/tests/dummy_domain/routers/act/__init__.py b/tests/dummy_domain/act/__init__.py similarity index 100% rename from tests/dummy_domain/routers/act/__init__.py rename to tests/dummy_domain/act/__init__.py diff --git a/tests/dummy_domain/routers/act/personne/__init__.py b/tests/dummy_domain/act/personne/__init__.py similarity index 100% rename from tests/dummy_domain/routers/act/personne/__init__.py rename to tests/dummy_domain/act/personne/__init__.py diff --git a/tests/dummy_domain/routers/act/personne/eo.py b/tests/dummy_domain/act/personne/eo.py similarity index 100% rename from tests/dummy_domain/routers/act/personne/eo.py rename to tests/dummy_domain/act/personne/eo.py diff --git a/tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py b/tests/dummy_domain/routers/abc/alphabet/TEST_uuid/__init__.py similarity index 100% rename from tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py rename to tests/dummy_domain/routers/abc/alphabet/TEST_uuid/__init__.py diff --git a/tests/dummy_domain/routers/abc/alphabet/__init__.py b/tests/dummy_domain/routers/abc/alphabet/__init__.py index ab5ade4..4c884ae 100644 --- a/tests/dummy_domain/routers/abc/alphabet/__init__.py +++ b/tests/dummy_domain/routers/abc/alphabet/__init__.py @@ -1,5 +1,5 @@ from starlette.responses import PlainTextResponse -from dummy_domain import acl +from halfapi.lib import acl ACLS = { 'GET': [{'acl':acl.public}] diff --git a/tests/dummy_domain/routers/abc/pinnochio/__init__.py b/tests/dummy_domain/routers/abc/pinnochio/__init__.py index c10e94d..151db9b 100644 --- a/tests/dummy_domain/routers/abc/pinnochio/__init__.py +++ b/tests/dummy_domain/routers/abc/pinnochio/__init__.py @@ -1,6 +1,6 @@ from halfapi.lib import acl ACLS = { - 'GET' : [{acl.public}] + 'GET' : [{'acl':acl.public}] } def get(): raise NotImplementedError diff --git a/tests/test_debug_routes.py b/tests/test_debug_routes.py index 4369bad..0870717 100644 --- a/tests/test_debug_routes.py +++ b/tests/test_debug_routes.py @@ -2,31 +2,21 @@ import pytest from starlette.authentication import UnauthenticatedUser from starlette.testclient import TestClient -from halfapi.app import application import json -def test_get_api_routes(): - c = TestClient(application) - r = c.get('/') - d_r = r.json() - assert isinstance(d_r, dict) - -def test_current_user(project_runner): - """ - Missing HALFAPI_SECRET to give current user route - """ - c = TestClient(application) +def test_current_user(project_runner, application_debug): + c = TestClient(application_debug) r = c.get('/halfapi/current_user') assert r.status_code == 200 -def test_log(): - c = TestClient(application) +def test_log(application_debug): + c = TestClient(application_debug) r = c.get('/halfapi/log') assert r.status_code == 200 -def test_error(): - c = TestClient(application) +def test_error(application_debug): + c = TestClient(application_debug) r = c.get('/halfapi/error/400') assert r.status_code == 400 r = c.get('/halfapi/error/404') @@ -34,8 +24,8 @@ def test_error(): r = c.get('/halfapi/error/500') assert r.status_code == 500 -def test_exception(): - c = TestClient(application) +def test_exception(application_debug): + c = TestClient(application_debug) try: r = c.get('/halfapi/exception') assert r.status_code == 500 diff --git a/tests/test_dummy_project_router.py b/tests/test_dummy_project_router.py index eadfa90..ac77729 100644 --- a/tests/test_dummy_project_router.py +++ b/tests/test_dummy_project_router.py @@ -7,17 +7,35 @@ import pytest from starlette.routing import Route from starlette.testclient import TestClient -from halfapi.lib.routes import gen_starlette_routes +from halfapi.lib.domain import gen_router_routes -def test_get_route(dummy_project): - from halfapi.app import application - os.environ['HALFAPI_CONFIG'] = dummy_project[0] - c = TestClient(application) - print(f'/{dummy_project[1]}/alphabet') - r = c.get(f'/{dummy_project[1]}/alphabet') - try: - assert r.status_code == 200 - except AssertionError as exc: - print('.'.join((dummy_project[1], 'routers'))) - raise exc +def test_get_route(dummy_project, application_domain, routers): + c = TestClient(application_domain) + path = verb = params = None + for path, verb, _, params in gen_router_routes(routers, []): + if len(params): + route_path = '/dummy_domain/{}'.format(path) + try: + if verb.lower() == 'get': + r = c.get(route_path) + elif verb.lower() == 'post': + r = c.post(route_path) + elif verb.lower() == 'patch': + r = c.patch(route_path) + elif verb.lower() == 'put': + r = c.put(route_path) + elif verb.lower() == 'delete': + r = c.delete(route_path) + else: + raise Exception(verb) + try: + assert r.status_code in [200, 501] + except AssertionError as exc: + print('{} [{}] {}'.format(str(r.status_code), verb, route_path)) + + except NotImplementedError: + pass + + if not path: + raise Exception('No route generated') diff --git a/tests/test_lib_domain.py b/tests/test_lib_domain.py index 2327deb..441ba0d 100644 --- a/tests/test_lib_domain.py +++ b/tests/test_lib_domain.py @@ -1,18 +1,36 @@ #!/usr/bin/env python3 import importlib -from halfapi.lib.domain import VERBS, gen_router_routes +from halfapi.lib.domain import VERBS, gen_routes, gen_router_routes, MissingAclError + +from types import FunctionType def test_gen_router_routes(): from .dummy_domain import routers - for path, d_route in gen_router_routes(routers, ['dummy_domain']): + for path, verb, fct, params in gen_router_routes(routers, ['dummy_domain']): assert isinstance(path, str) - for verb in VERBS: - if verb not in d_route.keys(): - continue - route = d_route[verb] - print(f'[{verb}] {path} {route["fct"]}') - assert len(route['params']) > 0 - assert hasattr(route['fct'], '__call__') - if 'fqtn' in route: - assert isinstance(route['fqtn'], str) + assert verb in VERBS + assert len(params) > 0 + assert hasattr(fct, '__call__') + +def test_gen_routes(): + from .dummy_domain.routers.abc.alphabet import TEST_uuid + try: + gen_routes( + TEST_uuid, + 'get', + ['abc', 'alphabet', 'TEST_uuid', ''], + []) + except MissingAclError: + assert True + + fct, params = gen_routes( + TEST_uuid, + 'get', + ['abc', 'alphabet', 'TEST_uuid', ''], + TEST_uuid.ACLS['GET']) + + assert isinstance(fct, FunctionType) + assert isinstance(params, list) + assert len(TEST_uuid.ACLS['GET']) == len(params) + diff --git a/tests/test_lib_router.py b/tests/test_lib_router.py new file mode 100644 index 0000000..d167bcf --- /dev/null +++ b/tests/test_lib_router.py @@ -0,0 +1,44 @@ +import os +from halfapi.lib.router import read_router + +def test_read_router_routers(): + from .dummy_domain import routers + + router_d = read_router(routers) + assert '' in router_d + assert 'SUBROUTES' in router_d[''] + assert isinstance(router_d['']['SUBROUTES'], list) + + for elt in os.scandir(routers.__path__[0]): + if elt.is_dir(): + assert elt.name in router_d['']['SUBROUTES'] + +def test_read_router_abc(): + from .dummy_domain.routers import abc + router_d = read_router(abc) + + assert '' in router_d + assert 'SUBROUTES' in router_d[''] + assert isinstance(router_d['']['SUBROUTES'], list) + +def test_read_router_alphabet(): + from .dummy_domain.routers.abc import alphabet + router_d = read_router(alphabet) + + assert '' in router_d + assert 'SUBROUTES' in router_d[''] + assert isinstance(router_d['']['SUBROUTES'], list) + +def test_read_router_TEST(): + from .dummy_domain.routers.abc.alphabet import TEST_uuid + router_d = read_router(TEST_uuid) + + print(router_d) + assert '' in router_d + assert 'SUBROUTES' in router_d[''] + assert isinstance(router_d['']['GET'], list) + assert isinstance(router_d['']['POST'], list) + assert isinstance(router_d['']['PATCH'], list) + assert isinstance(router_d['']['PUT'], list) + + diff --git a/tests/test_lib_schemas.py b/tests/test_lib_schemas.py index b3f9d8a..42298ff 100644 --- a/tests/test_lib_schemas.py +++ b/tests/test_lib_schemas.py @@ -5,7 +5,6 @@ from starlette.authentication import ( AuthenticationBackend, AuthenticationError, BaseUser, AuthCredentials, UnauthenticatedUser) -from halfapi.app import application from halfapi.lib.schemas import schema_dict_dom def test_schemas_dict_dom(): @@ -15,8 +14,23 @@ def test_schemas_dict_dom(): assert isinstance(schema, dict) -def test_get_api_routes(project_runner): - c = TestClient(application) +def test_get_api_routes(project_runner, application_debug): + c = TestClient(application_debug) r = c.get('/') d_r = r.json() assert isinstance(d_r, dict) + + +def test_get_api_dummy_domain_routes(application_domain, routers): + c = TestClient(application_domain) + r = c.get('/dummy_domain') + assert r.status_code == 200 + d_r = r.json() + assert isinstance(d_r, dict) + print(d_r) + assert 'abc/alphabet' in d_r + assert 'GET' in d_r['abc/alphabet'] + assert len(d_r['abc/alphabet']['GET']) > 0 + assert 'acl' in d_r['abc/alphabet']['GET'][0] + +