@@ -198,9 +198,11 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
198198 'redirect_uri' : 'http://testserver/callback/' ,
199199 }
200200 self .assertEqual (User .objects .all ().count (), 0 )
201- self .backend .authenticate (request = auth_request )
201+ result = self .backend .authenticate (request = auth_request )
202202 self .assertEqual (User .objects .all ().count (), 1 )
203203 user = User .objects .all ()[0 ]
204+ self .assertEquals (user , result )
205+
204206 self .
assertEquals (
user .
email ,
'[email protected] ' )
205207 self .assertEquals (user .username , 'username_algo' )
206208
@@ -213,6 +215,31 @@ def test_successful_authentication_new_user(self, token_mock, request_mock, algo
213215 headers = {'Authorization' : 'Bearer access_granted' }
214216 )
215217
218+ @patch ('mozilla_django_oidc.auth.requests' )
219+ @patch ('mozilla_django_oidc.auth.OIDCAuthenticationBackend.verify_token' )
220+ def test_successful_authentication_no_email (self , token_mock , request_mock ):
221+ """What happens if the auth "works" but it doesn't have an email?"""
222+ auth_request = RequestFactory ().get ('/foo' , {'code' : 'foo' ,
223+ 'state' : 'bar' })
224+ auth_request .session = {}
225+
226+ token_mock .return_value = True
227+ get_json_mock = Mock ()
228+ get_json_mock .json .return_value = {
229+ 'nickname' : 'a_username' ,
230+ 'email' : ''
231+ }
232+ request_mock .get .return_value = get_json_mock
233+ post_json_mock = Mock ()
234+ post_json_mock .json .return_value = {
235+ 'id_token' : 'id_token' ,
236+ 'access_token' : 'access_granted'
237+ }
238+ request_mock .post .return_value = post_json_mock
239+ result = self .backend .authenticate (request = auth_request )
240+ assert result is None
241+ self .assertEqual (User .objects .all ().count (), 0 )
242+
216243 def test_authenticate_no_code_no_state (self ):
217244 """Test authenticate with wrong parameters."""
218245
0 commit comments