Skip to content

Commit d10b31c

Browse files
committed
fix: unit tests
1 parent 37ac1ab commit d10b31c

11 files changed

+102
-33
lines changed

spid_cie_oidc/authority/tests/settings.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@
107107

108108
TA_SUB = "http://testserver.it/"
109109

110-
RESOLVE_REQUEST = {
111-
"iss": rp_conf["sub"],
112-
"sub": rp_conf["sub"],
113-
"anchor" : TA_SUB,
114-
"format" :"json",
115-
}
116-
117110
FETCH_REQUEST = {
118111
"iss": rp_conf["sub"],
119112
"sub": rp_conf["sub"],
@@ -153,4 +146,4 @@
153146

154147
ADVANCED_LIST_REQUEST = {
155148
"page": 1,
156-
}
149+
}

spid_cie_oidc/authority/tests/test_08_schemas.py

-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
from spid_cie_oidc.authority.schemas.advanced_entity_list_endpoint import AdvancedEntityListRequest
44
from spid_cie_oidc.authority.schemas.fetch_endpoint_request import FetchRequest
55
from spid_cie_oidc.authority.schemas.list_endpoint import ListRequest
6-
from spid_cie_oidc.authority.schemas.resolve_endpoint import ResolveRequest
76
from spid_cie_oidc.authority.schemas.trust_mark_status_endpoint import TrustMarkRequest
87
from spid_cie_oidc.authority.tests.settings import (
9-
RESOLVE_REQUEST,
108
FETCH_REQUEST,
119
LIST_REQUEST,
1210
TRUST_MARK_REQUEST,
@@ -23,9 +21,6 @@ class SchemaTest(TestCase):
2321
def setUp(self) -> None:
2422
return super().setUp()
2523

26-
def test_resolve_request(self):
27-
ResolveRequest(**RESOLVE_REQUEST)
28-
2924
def test_fetch_request(self):
3025
FetchRequest(**FETCH_REQUEST)
3126

spid_cie_oidc/entity/tests/rp_metadata_settings.py

+45
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,48 @@
4646

4747
RP_METADATA_CIE_NOJWKS_NOJWKS_URI = deepcopy(RP_METADATA_CIE)
4848
RP_METADATA_CIE_NOJWKS_NOJWKS_URI.pop("jwks_uri")
49+
50+
51+
RP_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PRa31HU87Vm7I3NSZ34DZsCZJoc', 'kid': 'HIvo33-Km7n03ZqKDJfWVnlFudsW28YhQZx5eaXtAKA'}
52+
RP_METADATA_JWK1_pub = {'kty': 'RSA', 'n': 'w8H80eT2zrs2XQ-SApZG9TkuXDuIxANfCVHt4fFqNnOEZaCNWqlTQIo0JiSBE-QmzZ09TYP1BJpESuQf_PUeLRVPfYHsBVk5OYvhT27_nYlV7_1LsFGLxxsIa-hswMMzvW-1_huKLy6Fp0WP0ouUJAHsF_eYVtO1ApRhvlIVd5azM4k7t8Lh8lkCSdF1SfGHfXnXJRb-XensZ0cFSfe2Koq9mD7jpGLXlPpXxj8Ow0g7KYT5kVtWE5ULmNmO7BIN1Hx4HpggbbEGgC9FyjKw4GfFb-csnB-icBPf_60HomjrkFFt6vTjrcqQaHOj-sEjP36N8rMSBiMmiMSPnsHhMQ', 'e': 'AQAB', 'kid': 'HIvo33-Km7n03ZqKDJfWVnlFudsW28YhQZx5eaXtAKA'}
53+
54+
rp_onboarding_data = dict(
55+
name="RP Test",
56+
sub="http://rp-test.it/oidc/rp/",
57+
type="openid_relying_party",
58+
metadata_policy={"openid_relying_party": {"scope": {"value": ["openid"]}}},
59+
is_active=True,
60+
jwks = [RP_METADATA_JWK1_pub]
61+
)
62+
63+
rp_conf = {
64+
"sub": rp_onboarding_data["sub"],
65+
"jwks_fed" : [RP_METADATA_JWK1],
66+
"jwks_core" : [RP_METADATA_JWK1],
67+
"metadata": {
68+
"openid_relying_party": {
69+
"application_type": "web",
70+
"client_registration_types": ["automatic"],
71+
"client_name": "Name of this service called http://rp-test.it/oidc/rp/",
72+
"contacts": ["[email protected]"],
73+
"grant_types": ["refresh_token", "authorization_code"],
74+
"redirect_uris": ["http://rp-test.it/oidc/rp/callback/"],
75+
"response_types": ["code"],
76+
"subject_type": "pairwise",
77+
"client_id": "http://rp-test.it/oidc/rp/",
78+
"jwks": {"keys": [RP_METADATA_JWK1_pub]},
79+
}
80+
},
81+
"authority_hints": ["http://testserver/"],
82+
"is_active": True,
83+
}
84+
85+
RP_CONF_AS_JSON = {
86+
"iss": rp_conf["sub"],
87+
"sub": rp_conf["sub"],
88+
"jwks": {
89+
"keys": [RP_METADATA_JWK1_pub]
90+
},
91+
"metadata": rp_conf["metadata"],
92+
"authority_hints":rp_conf["authority_hints"]
93+
}

spid_cie_oidc/authority/tests/test_07_resolve_entity_statement.py spid_cie_oidc/entity/tests/test_07_resolve_entity_statement.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from copy import deepcopy
12
import json
23

34
from unittest.mock import patch
@@ -10,13 +11,13 @@
1011
override_settings
1112
)
1213
from django.urls import reverse
13-
from spid_cie_oidc.authority.models import StaffToken
14-
from spid_cie_oidc.authority.tests.settings import rp_conf
15-
from spid_cie_oidc.authority.views import resolve_entity_statement
14+
from spid_cie_oidc.entity.tests.rp_metadata_settings import rp_conf, rp_onboarding_data
15+
from spid_cie_oidc.entity.views import resolve_entity_statement
1616
from spid_cie_oidc.entity.models import (
1717
FederationEntityConfiguration,
1818
FetchedEntityStatement,
19-
TrustChain
19+
TrustChain,
20+
StaffToken
2021
)
2122
from spid_cie_oidc.entity.tests.settings import ta_conf_data
2223
from spid_cie_oidc.entity.utils import (
@@ -27,14 +28,16 @@
2728

2829

2930
def create_tc():
31+
32+
3033
TA_FES = FetchedEntityStatement.objects.create(
31-
sub="sub",
32-
iss="sub",
34+
sub="http://testserver/",
35+
iss="http://testserver/",
3336
exp=datetime_from_timestamp(exp_from_now(33)),
3437
iat=datetime_from_timestamp(iat_now()),
3538
)
3639
return TrustChain.objects.create(
37-
sub="sub",
40+
sub="http://rp-test.it/oidc/rp/",
3841
exp=datetime_from_timestamp(exp_from_now(33)),
3942
jwks = [],
4043
metadata=[],
@@ -70,6 +73,8 @@ def setUp(self):
7073
iat=datetime_from_timestamp(iat_now()),
7174
)
7275

76+
77+
7378
@override_settings(HTTP_CLIENT_SYNC=True)
7479
def test_resolve_entity_statement(self):
7580
client = Client()
@@ -78,7 +83,7 @@ def test_resolve_entity_statement(self):
7883
data = {"sub" : rp_conf["sub"], "anchor" : ta_conf_data["sub"]}
7984
request = self.factory.get(url, data, **{'HTTP_AUTHORIZATION': "secret-token"})
8085
self.patcher = patch(
81-
"spid_cie_oidc.authority.views.get_or_create_trust_chain",
86+
"spid_cie_oidc.entity.trust_chain_operations.get_or_create_trust_chain",
8287
return_value = create_tc()
8388
)
8489
self.patcher.start()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
from django.test import TestCase
3+
from spid_cie_oidc.entity.schemas.resolve_endpoint import ResolveRequest
4+
from .rp_metadata_settings import rp_conf
5+
6+
TA_SUB = "http://testserver.it/"
7+
8+
RESOLVE_REQUEST = {
9+
"iss": rp_conf["sub"],
10+
"sub": rp_conf["sub"],
11+
"anchor" : TA_SUB,
12+
"format" :"json",
13+
}
14+
15+
16+
class SchemaTest(TestCase):
17+
18+
def setUp(self) -> None:
19+
return super().setUp()
20+
21+
def test_resolve_request(self):
22+
ResolveRequest(**RESOLVE_REQUEST)

spid_cie_oidc/provider/tests/test_03_refresh_token.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ def setUp(self):
4848
trust_anchor=self.ta_fes,
4949
is_active=True,
5050
)
51+
self.jwt_auds = [op_conf["sub"], "http://testserver/oidc/op/", "http://testserver/oidc/op/token/"]
5152
CLIENT_ASSERTION = {
5253
"iss": RP_SUB,
5354
"sub": RP_SUB,
54-
"aud": [op_conf["sub"]],
55+
"aud": self.jwt_auds,
5556
"exp": exp_from_now(),
5657
"iat": iat_now(),
5758
"jti": "jti",
@@ -60,7 +61,7 @@ def setUp(self):
6061
refresh_token = {
6162
"iss": self.op_local_conf["sub"],
6263
"sub": RP_SUB,
63-
"aud": [op_conf["sub"]],
64+
"aud": self.jwt_auds,
6465
"client_id": RP_CLIENT_ID,
6566
"scope": "openid",
6667
}

spid_cie_oidc/provider/tests/test_05_introspection_endpoint.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class IntrospectionEndpointTest(TestCase):
3131
def setUp(self):
3232
self.RP_SUB = rp_conf["sub"]
3333
self.RP_CLIENT_ID = rp_conf["metadata"]["openid_relying_party"]["client_id"]
34+
self.jwt_auds = [op_conf["sub"], "http://testserver/oidc/op/", "http://testserver/oidc/op/introspection/"]
3435
CLIENT_ASSERTION = {
3536
"iss": self.RP_SUB,
3637
"sub": self.RP_SUB,
37-
"aud": [op_conf["sub"]],
38+
"aud": self.jwt_auds,
3839
"exp": exp_from_now(),
3940
"iat": iat_now(),
4041
"jti": "jti",
@@ -43,7 +44,7 @@ def setUp(self):
4344
token = {
4445
"iss": self.RP_SUB,
4546
"sub": op_conf["sub"],
46-
"aud": [op_conf["sub"]],
47+
"aud": self.jwt_auds,
4748
"client_id": self.RP_SUB,
4849
"scope": "openid",
4950
}
@@ -91,6 +92,7 @@ def test_introspection_endpoint(self):
9192
"token" : self.jwt_token
9293

9394
}
95+
9496
res = client.post(url, request)
9597
self.assertTrue(res.status_code == 200)
9698
self.assertTrue("openid" in res.content.decode())

spid_cie_oidc/provider/tests/test_08_token_endpoint.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,23 @@ def setUp(self):
5050
trust_anchor=self.ta_fes,
5151
is_active=True,
5252
)
53+
self.jwt_auds = [op_conf["sub"], "http://testserver/oidc/op/", "http://testserver/oidc/op/token/"]
5354
CLIENT_ASSERTION = {
5455
"iss": RP_SUB,
5556
"sub": RP_SUB,
56-
"aud": [op_conf["sub"]],
57+
"aud": self.jwt_auds,
5758
"exp": exp_from_now(),
5859
"iat": iat_now(),
5960
"jti": "jti",
6061
}
6162
self.ca_jws = create_jws(CLIENT_ASSERTION, RP_METADATA_JWK1)
63+
6264
self.refresh_token = {
6365
"iss": self.op_local_conf["sub"],
6466
"sub": RP_SUB,
6567
"exp": exp_from_now(),
6668
"iat": iat_now(),
67-
"aud": [op_conf["sub"]],
69+
"aud": self.jwt_auds,
6870
"client_id": RP_CLIENT_ID,
6971
"scope": "openid",
7072
}

spid_cie_oidc/provider/tests/test_09_revocation_endpoint.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ def setUp(self):
3838
)
3939
self.op_local_conf = deepcopy(op_conf)
4040
FederationEntityConfiguration.objects.create(**self.op_local_conf)
41-
41+
self.jwt_auds = [op_conf["sub"], "http://testserver/oidc/op/", "http://testserver/oidc/op/revocation/"]
4242
CLIENT_ASSERTION = {
4343
"iss": RP_SUB,
4444
"sub": RP_SUB,
45-
"aud": [op_conf["sub"]],
45+
"aud": self.jwt_auds,
4646
"exp": exp_from_now(),
4747
"iat": iat_now(),
4848
"jti": "jti",
@@ -51,7 +51,7 @@ def setUp(self):
5151
access_token = {
5252
"iss": self.op_local_conf["sub"],
5353
"sub": RP_SUB,
54-
"aud": [op_conf["sub"]],
54+
"aud": self.jwt_auds,
5555
"client_id": RP_CLIENT_ID,
5656
"scope": "openid",
5757
}

spid_cie_oidc/provider/views/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ def check_client_assertion(self, client_id: str, client_assertion: str) -> bool:
188188

189189
if _op_eid:
190190
_allowed_auds.append(_op_eid)
191-
191+
192192
if not _op_eid or self.request.build_absolute_uri() not in _allowed_auds:
193193
logger.warning(
194-
f"Client assertion failed, fake audience: {_op.sub} != {_op_eid}"
194+
"Client assertion failed, fake audience: "
195+
f"{self.request.build_absolute_uri()} not in {_allowed_auds}"
195196
)
196197
# TODO Specialize exceptions
197198
raise Exception()

spid_cie_oidc/provider/views/introspection_endpoint.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ def post(self, request, *args, **kwargs):
6363
},
6464
status = 400
6565
)
66-
except Exception: # pragma: no cover
66+
except Exception as e: # pragma: no cover
67+
logger.error(e)
6768
return HttpResponseForbidden()
69+
70+
6871
required_token = request.POST['token']
6972
# query con client_id, access token
7073
token = IssuedToken.objects.filter(

0 commit comments

Comments
 (0)