Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit d293cd1

Browse files
committed
Added configuration tests.
1 parent 617b986 commit d293cd1

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

tests/entity_conf.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"port": 8090,
3+
"domain": "127.0.0.1",
4+
"base_url": "https://{domain}:{port}",
5+
"httpc_params": {
6+
"verify": false
7+
},
8+
"keys": {
9+
"private_path": "private/jwks.json",
10+
"key_defs": [
11+
{
12+
"type": "RSA",
13+
"key": "",
14+
"use": [
15+
"sig"
16+
]
17+
},
18+
{
19+
"type": "EC",
20+
"crv": "P-256",
21+
"use": [
22+
"sig"
23+
]
24+
}
25+
],
26+
"public_path": "static/jwks.json",
27+
"read_only": false
28+
}
29+
}

tests/entity_conf.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
CONFIG = {
2+
"port": 8090,
3+
"domain": "127.0.0.1",
4+
"base_url": "https://{domain}:{port}",
5+
"httpc_params": {
6+
"verify": False
7+
},
8+
"keys": {
9+
"private_path": "private/jwks.json",
10+
"key_defs": [
11+
{
12+
"type": "RSA",
13+
"key": "",
14+
"use": [
15+
"sig"
16+
]
17+
},
18+
{
19+
"type": "EC",
20+
"crv": "P-256",
21+
"use": [
22+
"sig"
23+
]
24+
}
25+
],
26+
"public_path": "static/jwks.json",
27+
"read_only": False
28+
}
29+
}

tests/server_conf.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"logging": {
3+
"version": 1,
4+
"disable_existing_loggers": false,
5+
"root": {
6+
"handlers": [
7+
"console",
8+
"file"
9+
],
10+
"level": "DEBUG"
11+
},
12+
"loggers": {
13+
"idp": {
14+
"level": "DEBUG"
15+
}
16+
},
17+
"handlers": {
18+
"console": {
19+
"class": "logging.StreamHandler",
20+
"stream": "ext://sys.stdout",
21+
"formatter": "default"
22+
},
23+
"file": {
24+
"class": "logging.FileHandler",
25+
"filename": "debug.log",
26+
"formatter": "default"
27+
}
28+
},
29+
"formatters": {
30+
"default": {
31+
"format": "%(asctime)s %(name)s %(levelname)s %(message)s"
32+
}
33+
}
34+
},
35+
"port": 8090,
36+
"domain": "127.0.0.1",
37+
"base_url": "https://{domain}:{port}",
38+
"httpc_params": {
39+
"verify": false
40+
},
41+
"keys": {
42+
"private_path": "private/jwks.json",
43+
"key_defs": [
44+
{
45+
"type": "RSA",
46+
"key": "",
47+
"use": [
48+
"sig"
49+
]
50+
},
51+
{
52+
"type": "EC",
53+
"crv": "P-256",
54+
"use": [
55+
"sig"
56+
]
57+
}
58+
],
59+
"public_path": "static/jwks.json",
60+
"read_only": false
61+
},
62+
"webserver": {
63+
"port": 8090,
64+
"domain": "127.0.0.1",
65+
"server_cert": "certs/cert.pem",
66+
"server_key": "certs/key.pem",
67+
"debug": true
68+
}
69+
}

tests/test_20_config.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)