[lib.router] forbid extra-keys in routes dict (no more FQTN at same level of methods)

This commit is contained in:
Maxime Alves LIRMM@home 2021-12-01 21:13:35 +01:00
parent a37c2356d6
commit c1bb637be7
2 changed files with 19 additions and 12 deletions

View File

@ -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__'):

View File

@ -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)