diff --git a/halfapi/half_domain.py b/halfapi/half_domain.py index 16394d2..eac9fc5 100644 --- a/halfapi/half_domain.py +++ b/halfapi/half_domain.py @@ -55,3 +55,12 @@ class HalfDomain(Starlette): ] ) + @staticmethod + def acls(domain): + """ Returns the ACLS constant for the given domain + """ + m_acl = importlib.import_module(f'{domain}.acl') + try: + return getattr(m_acl, 'ACLS') + except AttributeError: + raise Exception(f'Missing acl.ACLS constant in {domain} module') diff --git a/halfapi/testing/test_domain.py b/halfapi/testing/test_domain.py index 5c995a8..e1e6d05 100644 --- a/halfapi/testing/test_domain.py +++ b/halfapi/testing/test_domain.py @@ -4,8 +4,11 @@ import os import sys import json from unittest import TestCase +from starlette.testclient import TestClient from click.testing import CliRunner from ..cli.cli import cli +from ..halfapi import HalfAPI +from ..half_domain import HalfDomain from pprint import pprint class TestDomain(TestCase): @@ -53,3 +56,34 @@ class TestDomain(TestCase): self.assertEqual(result.exit_code, 0) return result_d + + def check_routes(self): + halfapi = HalfAPI({ + 'domain': { + 'dummy_domain': { + 'name': 'dummy_domain', + 'router': 'dummy_domain.routers', + 'prefix': False, + 'config': { + 'test': True + } + } + } + }) + + client = TestClient(halfapi.application) + r = client.get('/') + assert r.status_code == 200 + d_r = r.json() + assert isinstance(d_r, dict) + r = client.get('/halfapi/acls') + assert r.status_code == 200 + d_r = r.json() + assert isinstance(d_r, dict) + + ACLS = HalfDomain.acls(self.DOMAIN) + assert len(ACLS) == len(d_r.keys()) + for acl_name in ACLS: + assert acl_name in d_r.keys() + + diff --git a/tests/test_domain.py b/tests/test_domain.py index 8e47dc6..28b64aa 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -8,3 +8,6 @@ class TestDummyDomain(TestDomain): def test_domain(self): self.check_domain() + + def test_routes(self): + self.check_routes()