|
| 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