1- from mock import patch
1+ from mock import Mock , call , patch
22
33from django .contrib .auth import get_user_model
4- from django .core .urlresolvers import reverse
54from django .test import TestCase , override_settings
65
76from mozilla_django_oidc .auth import OIDCAuthenticationBackend
@@ -26,14 +25,18 @@ def test_invalid_token(self, request_mock, token_mock):
2625 """Test authentication with an invalid token."""
2726
2827 token_mock .return_value = None
29- request_mock .get .return_value = {
30- 'username' : 'username' ,
31- 'verified_email' :
'[email protected] ' 28+ get_json_mock = Mock ()
29+ get_json_mock .json .return_value = {
30+ 'nickname' : 'username' ,
31+ 3232 }
33- request_mock .post .return_value = {
33+ request_mock .get .return_value = get_json_mock
34+ post_json_mock = Mock ()
35+ post_json_mock .json .return_value = {
3436 'id_token' : 'id_token' ,
3537 'accesss_token' : 'access_token'
3638 }
39+ request_mock .post .return_value = post_json_mock
3740 self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ), None )
3841
3942 def test_get_user (self ):
@@ -49,56 +52,67 @@ def test_get_invalid_user(self):
4952
5053 @patch ('mozilla_django_oidc.auth.requests' )
5154 @patch ('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token' )
55+ @override_settings (SITE_URL = 'http://site-url.com' )
5256 def test_successful_authentication_existing_user (self , token_mock , request_mock ):
5357 """Test successful authentication for existing user."""
5458
5559 user = User .objects .create_user (username = 'a_username' ,
56605761 token_mock .return_value = True
58- request_mock .get .return_value = {
59- 'username' : 'a_username' ,
60- 'verified_email' :
'[email protected] ' 62+ get_json_mock = Mock ()
63+ get_json_mock .json .return_value = {
64+ 'nickname' : 'a_username' ,
65+ 6166 }
62- request_mock .post .return_value = {
67+ request_mock .get .return_value = get_json_mock
68+ post_json_mock = Mock ()
69+ post_json_mock .json .return_value = {
6370 'id_token' : 'id_token' ,
6471 'access_token' : 'access_granted'
6572 }
73+ request_mock .post .return_value = post_json_mock
74+
6675 post_data = {
6776 'client_id' : 'example_id' ,
6877 'client_secret' : 'example_secret' ,
69- 'grand_type ' : 'authorization_code' ,
78+ 'grant_type ' : 'authorization_code' ,
7079 'code' : 'foo' ,
71- 'redirect_url ' : reverse ( 'oidc_authentication_callback' )
80+ 'redirect_uri ' : 'http://site-url.com/oidc/authentication_callback/'
7281 }
7382 self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ), user )
7483 token_mock .assert_called_once_with ('id_token' )
7584 request_mock .post .assert_called_once_with ('https://server.example.com/token' ,
76- data = post_data ,
85+ json = post_data ,
7786 verify = True )
7887 request_mock .get .assert_called_once_with (
7988 'https://server.example.com/user?access_token=access_granted'
8089 )
8190
8291 @patch ('mozilla_django_oidc.auth.requests' )
8392 @patch ('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token' )
93+ @override_settings (SITE_URL = 'http://site-url.com' )
8494 def test_successful_authentication_new_user (self , token_mock , request_mock ):
8595 """Test successful authentication and user creation."""
8696
8797 token_mock .return_value = True
88- request_mock .get .return_value = {
89- 'username' : 'a_username' ,
90- 'verified_email' :
'[email protected] ' 98+ get_json_mock = Mock ()
99+ get_json_mock .json .return_value = {
100+ 'nickname' : 'a_username' ,
101+ 91102 }
92- request_mock .post .return_value = {
103+ request_mock .get .return_value = get_json_mock
104+ post_json_mock = Mock ()
105+ post_json_mock .json .return_value = {
93106 'id_token' : 'id_token' ,
94107 'access_token' : 'access_granted'
95108 }
109+ request_mock .post .return_value = post_json_mock
96110 post_data = {
97111 'client_id' : 'example_id' ,
98112 'client_secret' : 'example_secret' ,
99- 'grand_type ' : 'authorization_code' ,
113+ 'grant_type ' : 'authorization_code' ,
100114 'code' : 'foo' ,
101- 'redirect_url ' : reverse ( 'oidc_authentication_callback' )
115+ 'redirect_uri ' : 'http://site-url.com/oidc/authentication_callback/' ,
102116 }
103117 self .assertEqual (User .objects .all ().count (), 0 )
104118 self .backend .authenticate (code = 'foo' , state = 'bar' )
@@ -109,7 +123,7 @@ def test_successful_authentication_new_user(self, token_mock, request_mock):
109123
110124 token_mock .assert_called_once_with ('id_token' )
111125 request_mock .post .assert_called_once_with ('https://server.example.com/token' ,
112- data = post_data ,
126+ json = post_data ,
113127 verify = True )
114128 request_mock .get .assert_called_once_with (
115129 'https://server.example.com/user?access_token=access_granted'
@@ -125,30 +139,53 @@ def test_authenticate_no_code_no_state(self):
125139 def test_jwt_decode_params (self , request_mock , jwt_mock ):
126140 """Test jwt verification signature."""
127141
128- request_mock .get .return_value = {
129- 'username' : 'username' ,
130- 'verified_email' :
'[email protected] ' 142+ jwt_mock .decode .return_value = {
143+ 'aud' : 'audience'
144+ }
145+ get_json_mock = Mock ()
146+ get_json_mock .json .return_value = {
147+ 'nickname' : 'username' ,
148+ 131149 }
132- request_mock .post .return_value = {
150+ request_mock .get .return_value = get_json_mock
151+ post_json_mock = Mock ()
152+ post_json_mock .json .return_value = {
133153 'id_token' : 'token' ,
134154 'access_token' : 'access_token'
135155 }
156+ request_mock .post .return_value = post_json_mock
136157 self .backend .authenticate (code = 'foo' , state = 'bar' )
137- jwt_mock .decode .assert_called_once_with ('token' , 'example_secret' , verify = True )
158+ calls = [
159+ call ('token' , verify = False ),
160+ call ('token' , 'example_secret' , verify = True , audience = 'audience' )
161+ ]
162+ jwt_mock .decode .assert_has_calls (calls )
138163
139164 @override_settings (OIDC_VERIFY_JWT = False )
140165 @patch ('mozilla_django_oidc.auth.jwt' )
141166 @patch ('mozilla_django_oidc.auth.requests' )
142167 def test_jwt_decode_params_verify_false (self , request_mock , jwt_mock ):
143168 """Test jwt verification signature with verify False"""
144169
145- request_mock .get .return_value = {
146- 'username' : 'username' ,
147- 'verified_email' :
'[email protected] ' 170+ jwt_mock .decode .return_value = {
171+ 'aud' : 'audience'
148172 }
149- request_mock .post .return_value = {
173+ get_json_mock = Mock ()
174+ get_json_mock .json .return_value = {
175+ 'nickname' : 'username' ,
176+ 177+ }
178+ request_mock .get .return_value = get_json_mock
179+ post_json_mock = Mock ()
180+ post_json_mock .json .return_value = {
150181 'id_token' : 'token' ,
151182 'access_token' : 'access_token'
152183 }
184+ request_mock .post .return_value = post_json_mock
185+ calls = [
186+ call ('token' , verify = False ),
187+ call ('token' , 'example_secret' , verify = False , audience = 'audience' )
188+ ]
189+
153190 self .backend .authenticate (code = 'foo' , state = 'bar' )
154- jwt_mock .decode .assert_called_once_with ( 'token' , 'example_secret' , verify = False )
191+ jwt_mock .decode .assert_has_calls ( calls )
0 commit comments