1
1
"""
2
2
Tests for the SAML frontend module src/frontends/saml2.py.
3
3
"""
4
+ import json
5
+ from unittest .mock import Mock
4
6
from urllib .parse import urlparse
5
7
6
8
import pytest
19
21
'attributes' : {"mail" : {"saml" : ["email" ], "openid" : ["email" ]}}
20
22
}
21
23
BASE_URL = "https://op.example.com"
24
+ CLIENT_ID = "client1"
22
25
23
26
24
27
class TestOpenIDConnectFrontend (object ):
@@ -31,16 +34,21 @@ def setup(self, signing_key_path):
31
34
32
35
@pytest .fixture
33
36
def authn_req (self ):
34
- client_id = "client1"
35
37
state = "my_state"
36
38
nonce = "nonce"
37
39
redirect_uri = "https://client.example.com"
38
40
claims_req = ClaimsRequest (id_token = Claims (email = None ))
39
- req = AuthorizationRequest (client_id = client_id , state = state , scope = "openid" ,
41
+ req = AuthorizationRequest (client_id = CLIENT_ID , state = state , scope = "openid" ,
40
42
response_type = "id_token" , redirect_uri = redirect_uri ,
41
43
nonce = nonce , claims = claims_req )
42
44
return req
43
45
46
+ def insert_client_in_client_db (self , redirect_uri ):
47
+ self .instance .provider .cdb = {
48
+ CLIENT_ID : {"response_types" : ["id_token" ],
49
+ "redirect_uris" : [(redirect_uri , None )],
50
+ "client_salt" : "salt" }}
51
+
44
52
def setup_for_authn_response (self , context , auth_req ):
45
53
context .state [self .instance .name ] = {"oidc_request" : auth_req .to_urlencoded ()}
46
54
@@ -49,11 +57,6 @@ def setup_for_authn_response(self, context, auth_req):
49
57
internal_response .attributes = AttributeMapper (INTERNAL_ATTRIBUTES ).to_internal ("saml" , USERS ["testuser1" ])
50
58
internal_response .user_id = USERS ["testuser1" ]["eduPersonTargetedID" ][0 ]
51
59
52
- self .instance .cdb = {
53
- "client1" : {"response_types" : ["id_token" ],
54
- "redirect_uris" : [(auth_req ["redirect_uri" ], None )],
55
- "client_salt" : "salt" }}
56
-
57
60
return internal_response
58
61
59
62
def test_handle_authn_response (self , context , authn_req ):
@@ -72,12 +75,11 @@ def test_handle_authn_response(self, context, authn_req):
72
75
assert self .instance .name not in context .state
73
76
74
77
def test_get_authn_response_query_encoded (self , context ):
75
- client_id = "client1"
76
78
state = "my_state"
77
79
nonce = "nonce"
78
80
redirect_uri = "https://client.example.com"
79
81
claims_req = ClaimsRequest (id_token = Claims (email = None ))
80
- req = AuthorizationRequest (client_id = client_id , state = state , scope = "openid" ,
82
+ req = AuthorizationRequest (client_id = CLIENT_ID , state = state , scope = "openid" ,
81
83
response_type = "id_token" ,
82
84
redirect_uri = redirect_uri ,
83
85
nonce = nonce ,
@@ -95,10 +97,23 @@ def test_get_authn_response_query_encoded(self, context):
95
97
assert id_token ["sub" ] == USERS ["testuser1" ]["eduPersonTargetedID" ][0 ]
96
98
assert id_token ["email" ] == USERS ["testuser1" ]["email" ][0 ]
97
99
100
+ def test_handle_authn_request (self , context , authn_req ):
101
+ mock_callback = Mock ()
102
+ self .instance .auth_req_callback_func = mock_callback
103
+ self .insert_client_in_client_db (authn_req ["redirect_uri" ])
104
+
105
+ context .request = authn_req .to_dict ()
106
+ context .request ["claims" ] = json .dumps (context .request ["claims" ])
107
+ self .instance .handle_authn_request (context )
108
+
109
+ assert mock_callback .call_count == 1
110
+ context , internal_req = mock_callback .call_args [0 ]
111
+ assert internal_req .requester == authn_req ["client_id" ]
112
+ assert internal_req .user_id_hash_type == UserIdHashType .pairwise
113
+
98
114
def test_handle_backend_error (self , context ):
99
- client_id = "client1"
100
115
redirect_uri = "https://client.example.com"
101
- areq = AuthorizationRequest (client_id = client_id , scope = "openid" , response_type = "id_token" ,
116
+ areq = AuthorizationRequest (client_id = CLIENT_ID , scope = "openid" , response_type = "id_token" ,
102
117
redirect_uri = redirect_uri )
103
118
context .state [self .instance .name ] = {"oidc_request" : areq .to_urlencoded ()}
104
119
@@ -122,7 +137,7 @@ def test_register_client(self, context):
122
137
123
138
reg_resp = RegistrationResponse ().deserialize (registration_response .message , "json" )
124
139
assert "client_id" in reg_resp
125
- assert reg_resp [ "client_id" ] in self . instance . provider . cdb
140
+
126
141
# no need to issue client secret since to token endpoint is published
127
142
assert "client_secret" not in reg_resp
128
143
assert reg_resp ["redirect_uris" ] == [redirect_uri ]
0 commit comments