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

Commit 1bc575f

Browse files
committed
Basic class to provide the dump, flush and load methods.
Bumped version.
1 parent 2995ba8 commit 1bc575f

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

src/oidcmsg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = 'Roland Hedberg'
2-
__version__ = '1.1.5'
2+
__version__ = '1.2.0'
33

44
import os
55

src/oidcmsg/impexp.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from typing import List
23
from typing import Optional
34
from urllib.parse import quote_plus
45

@@ -26,32 +27,32 @@ class ImpExp:
2627
def __init__(self):
2728
pass
2829

29-
def _dump(self, cls, item, cutoff: Optional[list] = None):
30+
def _dump(self, cls, item, exclude_attribute: Optional[list] = None):
3031
if cls in [None, "", [], {}]:
3132
val = item
3233
elif isinstance(item, Message):
3334
val = {qualified_name(item.__class__): item.to_dict()}
3435
elif cls == object:
3536
val = qualified_name(item)
3637
elif isinstance(cls, list):
37-
val = [self._dump(cls[0], v, cutoff) for v in item]
38+
val = [self._dump(cls[0], v, exclude_attribute) for v in item]
3839
else:
39-
val = item.dump(cutoff=cutoff)
40+
val = item.dump(exclude_attribute=exclude_attribute)
4041

4142
return val
4243

43-
def dump(self, cutoff: Optional[list] = None) -> dict:
44-
_cutoff = cutoff or []
44+
def dump(self, exclude_attribute: Optional[List[str]] = None) -> dict:
45+
_exclude_attribute = exclude_attribute or []
4546
info = {}
4647
for attr, cls in self.parameter.items():
47-
if attr in _cutoff:
48+
if attr in _exclude_attribute:
4849
continue
4950

5051
item = getattr(self, attr, None)
5152
if item is None:
5253
continue
5354

54-
info[attr] = self._dump(cls, item, cutoff)
55+
info[attr] = self._dump(cls, item, exclude_attribute)
5556

5657
return info
5758

tests/test_11_impexp.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from cryptojwt import KeyBundle
2+
from cryptojwt.key_bundle import build_key_bundle
3+
4+
from oidcmsg.impexp import ImpExp
5+
from oidcmsg.oauth2 import AuthorizationResponse
6+
from oidcmsg.oidc import AuthorizationRequest
7+
8+
KEYSPEC = [
9+
{"type": "RSA", "use": ["sig"]},
10+
{"type": "EC", "crv": "P-256", "use": ["sig"]},
11+
]
12+
13+
14+
class ImpExpTest(ImpExp):
15+
parameter = {
16+
"string": "",
17+
"list": [],
18+
"dict": {},
19+
"message": AuthorizationRequest,
20+
"response_class": object,
21+
"key_bundle": KeyBundle,
22+
"bundles": [KeyBundle]
23+
}
24+
25+
26+
def test_dump_load():
27+
b = ImpExpTest()
28+
b.string = "foo"
29+
b.list = ["a", "b", "c"]
30+
b.dict = {"a": 1, "b": 2}
31+
b.message = AuthorizationRequest(scope="openid", redirect_uri="https://example.com/cb",
32+
response_type="code", client_id="abcdefg")
33+
b.response_class = AuthorizationResponse
34+
b.key_bundle = build_key_bundle(key_conf=KEYSPEC)
35+
b.bundles = [build_key_bundle(key_conf=KEYSPEC)]
36+
b.bundles.append(build_key_bundle(key_conf=KEYSPEC))
37+
38+
dump = b.dump()
39+
40+
b_copy = ImpExpTest().load(dump)
41+
assert b_copy
42+
assert b_copy.list == b.list
43+
assert b_copy.dict == b.dict
44+
# Message doesn't implement __eq__
45+
assert b_copy.message.__class__ == b.message.__class__
46+
assert b_copy.response_class == b.response_class
47+
# KeyBundle doesn't implement __eq__
48+
assert b_copy.key_bundle.keys() == b.key_bundle.keys()
49+
assert len(b_copy.bundles) == 2
50+
for kb in b_copy.bundles:
51+
assert isinstance(kb, KeyBundle)
52+
53+
54+
def test_flush():
55+
b = ImpExpTest()
56+
b.string = "foo"
57+
b.list = ["a", "b", "c"]
58+
b.dict = {"a": 1, "b": 2}
59+
b.message = AuthorizationRequest(scope="openid", redirect_uri="https://example.com/cb",
60+
response_type="code", client_id="abcdefg")
61+
b.response_class = AuthorizationResponse
62+
b.key_bundle = build_key_bundle(key_conf=KEYSPEC)
63+
b.bundles = [build_key_bundle(key_conf=KEYSPEC)]
64+
b.bundles.append(build_key_bundle(key_conf=KEYSPEC))
65+
66+
dump = b.dump()
67+
68+
b.flush()
69+
70+
assert b.string == ""
71+
assert b.list == []
72+
assert b.dict == {}
73+
assert b.message is None
74+
assert b.response_class is None
75+
assert b.key_bundle is None
76+
assert b.bundles is None
77+
78+
b.load(dump)
79+
80+
assert b.string == "foo"
81+
assert b.list == ["a", "b", "c"]
82+
assert b.dict == {"a": 1, "b": 2}
83+
assert isinstance(b.message, AuthorizationRequest)
84+
assert b.response_class == AuthorizationResponse
85+
assert isinstance(b.key_bundle, KeyBundle)
86+
assert len(b.bundles) == 2
87+
for kb in b.bundles:
88+
assert isinstance(kb, KeyBundle)

0 commit comments

Comments
 (0)