|
| 1 | +import os |
| 2 | +from typing import Dict |
| 3 | +from typing import List |
| 4 | +from typing import Optional |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from oidcmsg.configure import Base |
| 10 | +from oidcmsg.configure import Configuration |
| 11 | +from oidcmsg.configure import create_from_config_file |
| 12 | +from oidcmsg.configure import lower_or_upper |
| 13 | +from oidcmsg.configure import set_domain_and_port |
| 14 | +from oidcmsg.util import rndstr |
| 15 | + |
| 16 | +_dirname = os.path.dirname(os.path.abspath(__file__)) |
| 17 | + |
| 18 | +URIS = ["base_url"] |
| 19 | + |
| 20 | + |
| 21 | +class EntityConfiguration(Base): |
| 22 | + def __init__(self, |
| 23 | + conf: Dict, |
| 24 | + entity_conf: Optional[Any] = None, |
| 25 | + base_path: Optional[str] = '', |
| 26 | + domain: Optional[str] = "", |
| 27 | + port: Optional[int] = 0, |
| 28 | + file_attributes: Optional[List[str]] = None, |
| 29 | + uris: Optional[List[str]] = None |
| 30 | + ): |
| 31 | + |
| 32 | + Base.__init__(self, conf, base_path=base_path, file_attributes=file_attributes) |
| 33 | + |
| 34 | + self.keys = lower_or_upper(conf, 'keys') |
| 35 | + |
| 36 | + if not domain: |
| 37 | + domain = conf.get("domain", "127.0.0.1") |
| 38 | + |
| 39 | + if not port: |
| 40 | + port = conf.get("port", 80) |
| 41 | + |
| 42 | + if uris is None: |
| 43 | + uris = URIS |
| 44 | + conf = set_domain_and_port(conf, uris, domain, port) |
| 45 | + |
| 46 | + self.hash_seed = lower_or_upper(conf, 'hash_seed', rndstr(32)) |
| 47 | + self.base_url = conf.get("base_url") |
| 48 | + self.httpc_params = conf.get("httpc_params", {"verify": False}) |
| 49 | + |
| 50 | + |
| 51 | +def test_server_config(): |
| 52 | + c = create_from_config_file(Configuration, |
| 53 | + entity_conf=[{"class": EntityConfiguration, "attr": "entity"}], |
| 54 | + filename=os.path.join(_dirname, 'server_conf.json'), |
| 55 | + base_path=_dirname) |
| 56 | + assert c |
| 57 | + assert set(c.web_conf.keys()) == {'port', 'domain', 'server_cert', 'server_key', 'debug'} |
| 58 | + |
| 59 | + entity_config = c.entity |
| 60 | + assert entity_config.base_url == "https://127.0.0.1:8090" |
| 61 | + assert entity_config.httpc_params == {"verify": False} |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("filename", ['entity_conf.json', 'entity_conf.py']) |
| 65 | +def test_entity_config(filename): |
| 66 | + configuration = create_from_config_file(EntityConfiguration, |
| 67 | + filename=os.path.join(_dirname, filename), |
| 68 | + base_path=_dirname) |
| 69 | + assert configuration |
| 70 | + |
| 71 | + assert configuration.base_url == "https://127.0.0.1:8090" |
| 72 | + assert configuration.httpc_params == {"verify": False} |
| 73 | + assert configuration['keys'] |
| 74 | + ni = dict(configuration.items()) |
| 75 | + assert len(ni) == 4 |
| 76 | + assert set(ni.keys()) == {'keys', 'base_url', 'httpc_params', 'hash_seed'} |
| 77 | + |
0 commit comments