From fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Wed, 16 Jun 2021 15:34:25 +0200 Subject: [PATCH] [wip] tests dummy_domain --- tests/conftest.py | 30 ++++---- tests/dummy_domain/routers/__init__.py | 5 -- tests/dummy_domain/routers/abc/__init__.py | 5 -- .../routers/abc/alphabet/__init__.py | 13 +--- .../abc/alphabet/test:uuid/__init__.py | 19 +++++ .../routers/abc/pinnochio/__init__.py | 6 +- tests/test_dummy_project_router.py | 73 +++---------------- 7 files changed, 52 insertions(+), 99 deletions(-) create mode 100644 tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py diff --git a/tests/conftest.py b/tests/conftest.py index 9cfd68c..4a68feb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -212,8 +212,8 @@ def dummy_app(): backend=JWTAuthenticationBackend(secret_key='dummysecret') ) return app -@pytest.fixture +@pytest.fixture def dummy_debug_app(): app = Starlette(debug=True) app.add_route('/', @@ -251,19 +251,23 @@ def create_route(): @pytest.fixture def dummy_project(): - halfapi_dirname = tempfile.mkdtemp(prefix='halfapi_') - domain_dirname = os.path.join(halfapi_dirname, 'test_domain') - halfapi_path = os.path.join(halfapi_dirname, '.halfapi') - os.mkdir(halfapi_path) - os.mkdir(os.path.join(domain_dirname)) - os.mkdir(os.path.join(domain_dirname, 'test_router')) + halfapi_config = tempfile.mktemp() + halfapi_secret = tempfile.mktemp() + domain = 'dummy_domain' - with open(os.path.join(halfapi_path, 'config'), 'w') as f: + with open(halfapi_config, 'w') as f: f.writelines([ - '[domains]', - f'test_domain = test_router' + '[project]\n', + 'name = lirmm_api\n', + 'halfapi_version = 0.5.0\n', + f'secret = {halfapi_secret}\n', + 'port = 3050\n', + 'loglevel = debug\n', + '[domains]\n', + f'{domain}= .routers' ]) - with open(os.path.join(halfapi_dirname, 'test_domain', '__init__.py'), 'w') as f: - f.write('') - return (halfapi_dirname, 'test_domain') + with open(halfapi_secret, 'w') as f: + f.write('turlututu') + + return (halfapi_config, 'dummy_domain', 'routers') diff --git a/tests/dummy_domain/routers/__init__.py b/tests/dummy_domain/routers/__init__.py index c90a1c1..e69de29 100644 --- a/tests/dummy_domain/routers/__init__.py +++ b/tests/dummy_domain/routers/__init__.py @@ -1,5 +0,0 @@ -ROUTES={ - '': { - 'SUBROUTES': ['abc','act'] - } -} diff --git a/tests/dummy_domain/routers/abc/__init__.py b/tests/dummy_domain/routers/abc/__init__.py index 82dc85e..e69de29 100644 --- a/tests/dummy_domain/routers/abc/__init__.py +++ b/tests/dummy_domain/routers/abc/__init__.py @@ -1,5 +0,0 @@ -ROUTES={ - '': { - 'SUBROUTES': ['alphabet', 'pinnochio'] - } -} diff --git a/tests/dummy_domain/routers/abc/alphabet/__init__.py b/tests/dummy_domain/routers/abc/alphabet/__init__.py index 24a3c64..ab5ade4 100644 --- a/tests/dummy_domain/routers/abc/alphabet/__init__.py +++ b/tests/dummy_domain/routers/abc/alphabet/__init__.py @@ -1,17 +1,8 @@ from starlette.responses import PlainTextResponse from dummy_domain import acl -ROUTES={ - '': { - 'GET': [{'acl':acl.public}] - }, - '{test:uuid}': { - 'GET': [{'acl':None}], - 'POST': [{'acl':None}], - 'PATCH': [{'acl':None}], - 'PUT': [{'acl':None}] - } - +ACLS = { + 'GET': [{'acl':acl.public}] } async def get(request, *args, **kwargs): diff --git a/tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py b/tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py new file mode 100644 index 0000000..64c08da --- /dev/null +++ b/tests/dummy_domain/routers/abc/alphabet/test:uuid/__init__.py @@ -0,0 +1,19 @@ +from halfapi.lib import acl +ACLS = { + 'GET': [{'acl':acl.public}], + 'POST': [{'acl':acl.public}], + 'PATCH': [{'acl':acl.public}], + 'PUT': [{'acl':acl.public}] +} + +def get(test): + return str(test) + +def post(test): + return str(test) + +def patch(test): + return str(test) + +def put(test): + return str(test) diff --git a/tests/dummy_domain/routers/abc/pinnochio/__init__.py b/tests/dummy_domain/routers/abc/pinnochio/__init__.py index 5925edf..c10e94d 100644 --- a/tests/dummy_domain/routers/abc/pinnochio/__init__.py +++ b/tests/dummy_domain/routers/abc/pinnochio/__init__.py @@ -1,2 +1,6 @@ -ROUTES={ +from halfapi.lib import acl +ACLS = { + 'GET' : [{acl.public}] } +def get(): + raise NotImplementedError diff --git a/tests/test_dummy_project_router.py b/tests/test_dummy_project_router.py index abf88f7..eadfa90 100644 --- a/tests/test_dummy_project_router.py +++ b/tests/test_dummy_project_router.py @@ -10,69 +10,14 @@ from starlette.testclient import TestClient from halfapi.lib.routes import gen_starlette_routes -def test_create_route(dummy_project, create_route): - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'get', '/test') - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'post', '/test') - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'put', '/test') - - os.chdir(dummy_project[0]) - - sys.path.insert(0, '.') - router_path = os.path.join('.', dummy_project[1], 'test') - os.path.isdir(router_path) - try: - mod = importlib.import_module('.'.join((dummy_project[1], 'test'))) - except ModuleNotFoundError as exc: - print('.'.join((dummy_project[1], 'test'))) - print(os.listdir('.')) - raise exc - - assert hasattr(mod, 'get') - assert hasattr(mod, 'post') - assert hasattr(mod, 'put') - -def test_has_route(dummy_project, create_route): - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'get', '/test') - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'post', '/test/tutu') - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'patch', '/test/ID') - - - os.chdir(dummy_project[0]) - sys.path.insert(0, '.') - try: - mod = importlib.import_module(dummy_project[1], 'test') - except ModuleNotFoundError as exc: - print('.'.join((dummy_project[1], 'test'))) - print(os.listdir('.')) - raise exc - - for elt in gen_starlette_routes({dummy_project[1]: mod}): - assert(isinstance(elt, Route)) - - - -def test_get_route(dummy_project, create_route): - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'get', '/test') - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'post', '/test/tutu') - - create_route(os.path.join(dummy_project[0], dummy_project[1]), - 'post', '/test/ID') - +def test_get_route(dummy_project): from halfapi.app import application + os.environ['HALFAPI_CONFIG'] = dummy_project[0] c = TestClient(application) - r = c.get(f'/{dummy_project[1]}/test') - assert r.status_code == 200 + 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