11from mock import Mock , call , patch
22
3+ from django .conf import settings
34from django .contrib .auth import get_user_model
45from django .test import TestCase , override_settings
56
@@ -88,12 +89,14 @@ def test_successful_authentication_existing_user(self, token_mock, request_mock)
8889 'https://server.example.com/user?access_token=access_granted'
8990 )
9091
92+ @patch .object (settings , 'OIDC_USERNAME_ALGO' )
9193 @patch ('mozilla_django_oidc.auth.requests' )
9294 @patch ('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token' )
9395 @override_settings (SITE_URL = 'http://site-url.com' )
94- def test_successful_authentication_new_user (self , token_mock , request_mock ):
96+ def test_successful_authentication_new_user (self , token_mock , request_mock , algo_mock ):
9597 """Test successful authentication and user creation."""
9698
99+ algo_mock .return_value = 'username_algo'
97100 token_mock .return_value = True
98101 get_json_mock = Mock ()
99102 get_json_mock .json .return_value = {
@@ -119,7 +122,7 @@ def test_successful_authentication_new_user(self, token_mock, request_mock):
119122 self .assertEqual (User .objects .all ().count (), 1 )
120123 user = User .objects .all ()[0 ]
121124 self .
assertEquals (
user .
email ,
'[email protected] ' )
122- self .assertEquals (user .username , 'a_username ' )
125+ self .assertEquals (user .username , 'username_algo ' )
123126
124127 token_mock .assert_called_once_with ('id_token' )
125128 request_mock .post .assert_called_once_with ('https://server.example.com/token' ,
@@ -189,3 +192,92 @@ def test_jwt_decode_params_verify_false(self, request_mock, jwt_mock):
189192
190193 self .backend .authenticate (code = 'foo' , state = 'bar' )
191194 jwt_mock .decode .assert_has_calls (calls )
195+
196+ @override_settings (OIDC_CREATE_USER = False )
197+ @patch ('mozilla_django_oidc.auth.jwt' )
198+ @patch ('mozilla_django_oidc.auth.requests' )
199+ def test_create_user_disabled (self , request_mock , jwt_mock ):
200+ """Test with user creation disabled and no user found."""
201+
202+ jwt_mock .return_value = True
203+ get_json_mock = Mock ()
204+ get_json_mock .json .return_value = {
205+ 'nickname' : 'a_username' ,
206+ 207+ }
208+ request_mock .get .return_value = get_json_mock
209+ post_json_mock = Mock ()
210+ post_json_mock .json .return_value = {
211+ 'id_token' : 'id_token' ,
212+ 'access_token' : 'access_granted'
213+ }
214+ request_mock .post .return_value = post_json_mock
215+ self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ), None )
216+
217+ @patch ('mozilla_django_oidc.auth.jwt' )
218+ @patch ('mozilla_django_oidc.auth.requests' )
219+ def test_create_user_enabled (self , request_mock , jwt_mock ):
220+ """Test with user creation enabled and no user found."""
221+
222+ self .
assertEqual (
User .
objects .
filter (
email = '[email protected] ' ).
exists (),
False )
223+ jwt_mock .return_value = True
224+ get_json_mock = Mock ()
225+ get_json_mock .json .return_value = {
226+ 'nickname' : 'a_username' ,
227+ 228+ }
229+ request_mock .get .return_value = get_json_mock
230+ post_json_mock = Mock ()
231+ post_json_mock .json .return_value = {
232+ 'id_token' : 'id_token' ,
233+ 'access_token' : 'access_granted'
234+ }
235+ request_mock .post .return_value = post_json_mock
236+ self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ),
237+ User .
objects .
get (
email = '[email protected] ' ))
238+
239+ @patch .object (settings , 'OIDC_USERNAME_ALGO' )
240+ @patch ('mozilla_django_oidc.auth.jwt' )
241+ @patch ('mozilla_django_oidc.auth.requests' )
242+ def test_custom_username_algo (self , request_mock , jwt_mock , algo_mock ):
243+ """Test user creation with custom username algorithm."""
244+
245+ self .
assertEqual (
User .
objects .
filter (
email = '[email protected] ' ).
exists (),
False )
246+ algo_mock .return_value = 'username_algo'
247+ jwt_mock .return_value = True
248+ get_json_mock = Mock ()
249+ get_json_mock .json .return_value = {
250+ 'nickname' : 'a_username' ,
251+ 252+ }
253+ request_mock .get .return_value = get_json_mock
254+ post_json_mock = Mock ()
255+ post_json_mock .json .return_value = {
256+ 'id_token' : 'id_token' ,
257+ 'access_token' : 'access_granted'
258+ }
259+ request_mock .post .return_value = post_json_mock
260+ self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ),
261+ User .objects .get (username = 'username_algo' ))
262+
263+ @patch ('mozilla_django_oidc.auth.jwt' )
264+ @patch ('mozilla_django_oidc.auth.requests' )
265+ def test_duplicate_emails (self , request_mock , jwt_mock ):
266+ """Test auth with two users having the same email."""
267+
268+ User .
objects .
create (
username = 'user1' ,
email = '[email protected] ' )
269+ User .
objects .
create (
username = 'user2' ,
email = '[email protected] ' )
270+ jwt_mock .return_value = True
271+ get_json_mock = Mock ()
272+ get_json_mock .json .return_value = {
273+ 'nickname' : 'a_username' ,
274+ 275+ }
276+ request_mock .get .return_value = get_json_mock
277+ post_json_mock = Mock ()
278+ post_json_mock .json .return_value = {
279+ 'id_token' : 'id_token' ,
280+ 'access_token' : 'access_granted'
281+ }
282+ request_mock .post .return_value = post_json_mock
283+ self .assertEqual (self .backend .authenticate (code = 'foo' , state = 'bar' ), None )
0 commit comments