From c1bb637be7e70197345e7e0c46aef1aa43bcba32 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Wed, 1 Dec 2021 21:13:35 +0100 Subject: [PATCH] [lib.router] forbid extra-keys in routes dict (no more FQTN at same level of methods) --- halfapi/lib/router.py | 18 ++++++------------ tests/test_lib_router.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/halfapi/lib/router.py b/halfapi/lib/router.py index a3a2c6d..d018351 100644 --- a/halfapi/lib/router.py +++ b/halfapi/lib/router.py @@ -27,19 +27,13 @@ def read_router(m_router: ModuleType) -> Dict: acls = getattr(m_router, 'ACLS') if hasattr(m_router, 'ACLS') else None if acls is not None: - for verb in VERBS: - if not hasattr(m_router, verb.lower()): - # verb in function names are lowercase - continue + for method in acls.keys(): + if method not in VERBS: + raise Exception( + 'This method is not handled: {}'.format(method)) - """ There is a "verb" route in the router - """ - - if verb.upper() not in acls: - continue - - routes[''][verb.upper()] = [] - routes[''][verb.upper()] = acls[verb.upper()].copy() + routes[''][method] = [] + routes[''][method] = acls[method].copy() routes['']['SUBROUTES'] = [] if hasattr(m_router, '__path__'): diff --git a/tests/test_lib_router.py b/tests/test_lib_router.py index d167bcf..f39a55e 100644 --- a/tests/test_lib_router.py +++ b/tests/test_lib_router.py @@ -1,5 +1,8 @@ import os +import pytest +from schema import SchemaError from halfapi.lib.router import read_router +from halfapi.lib.constants import ROUTER_SCHEMA, ROUTER_ACLS_SCHEMA def test_read_router_routers(): from .dummy_domain import routers @@ -29,6 +32,16 @@ def test_read_router_alphabet(): assert 'SUBROUTES' in router_d[''] assert isinstance(router_d['']['SUBROUTES'], list) + ROUTER_SCHEMA.validate(router_d) + + with pytest.raises(SchemaError): + """ Test that we cannot specify wrong method in ROUTES or ACLS + + TODO: Write more errors + """ + router_d['']['TEG'] = {} + ROUTER_SCHEMA.validate(router_d) + def test_read_router_TEST(): from .dummy_domain.routers.abc.alphabet import TEST_uuid router_d = read_router(TEST_uuid)