From ff90e591aa02e76819e0d073d3195865c3e22d95 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Fri, 5 Aug 2022 09:37:17 +0200 Subject: [PATCH] [test][fix] configuration in halfapi route argument --- CHANGELOG.md | 5 +++++ halfapi/lib/domain_middleware.py | 16 +++++++++++++++- tests/cli/test_cli_proj.py | 16 +++++++++++++++- tests/dummy_domain/routers/config/__init__.py | 15 ++++++++++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc92b77..0f07cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # HalfAPI +## 0.6.21 + +- Store only domain's config in halfapi['config'] +- Should run halfapi domain with config_file argument + ## 0.6.20 - Fix arguments handling diff --git a/halfapi/lib/domain_middleware.py b/halfapi/lib/domain_middleware.py index c1f0379..f152dd6 100644 --- a/halfapi/lib/domain_middleware.py +++ b/halfapi/lib/domain_middleware.py @@ -35,7 +35,21 @@ class DomainMiddleware(BaseHTTPMiddleware): request.scope['domain'] = self.domain['name'] if hasattr(request.app, 'config') \ and isinstance(request.app.config, dict): - request.scope['config'] = { **request.app.config } + # Set the config scope to the domain's config + request.scope['config'] = request.app.config.get( + 'domain', {} + ).get( + self.domain['name'], {} + ).copy() + + # TODO: Remove in 0.7.0 + config = request.scope['config'].copy() + request.scope['config']['domain'] = {} + request.scope['config']['domain'][self.domain['name']] = {} + request.scope['config']['domain'][self.domain['name']]['config'] = config + + + else: logger.debug('%s', request.app) logger.debug('%s', getattr(request.app, 'config', None)) diff --git a/tests/cli/test_cli_proj.py b/tests/cli/test_cli_proj.py index 2fa6ed2..7331dde 100644 --- a/tests/cli/test_cli_proj.py +++ b/tests/cli/test_cli_proj.py @@ -4,6 +4,7 @@ import subprocess import importlib import tempfile from unittest.mock import patch +import json import pytest from click.testing import CliRunner @@ -25,7 +26,20 @@ class TestCliProj(): r = project_runner('domain') print(r.stdout) assert r.exit_code == 1 - r = project_runner('domain dummy_domain') + _, tmp_conf = tempfile.mkstemp() + with open(tmp_conf, 'w') as fh: + fh.write( + json.dumps({ + 'domain': { + 'dummy_domain': { + 'name': 'dummy_domain', + 'enabled': True + } + } + }) + ) + + r = project_runner(f'domain dummy_domain {tmp_conf}') print(r.stdout) assert r.exit_code == 0 diff --git a/tests/dummy_domain/routers/config/__init__.py b/tests/dummy_domain/routers/config/__init__.py index 70e6a33..3adef6b 100644 --- a/tests/dummy_domain/routers/config/__init__.py +++ b/tests/dummy_domain/routers/config/__init__.py @@ -14,4 +14,17 @@ def get(halfapi): returns the configuration of the domain """ logger.error('%s', halfapi) - return halfapi['config']['domain']['dummy_domain']['config'] + # TODO: Remove in 0.7.0 + try: + assert 'test' in halfapi['config']['domain']['dummy_domain']['config'] + except AssertionError as exc: + logger.error('No TEST in halfapi[config][domain][dummy_domain][config]') + raise exc + + try: + assert 'test' in halfapi['config'] + except AssertionError as exc: + logger.error('No TEST in halfapi[config]') + raise exc + + return halfapi['config']