def test_user_creation_failure(self, mock_get_or_create): """ Verify that IntegrityErrors beyond the configured retry limit are raised. """ authentication = JwtAuthentication() errors = [IntegrityError] * (MAX_RETRIES + 1) mock_get_or_create.side_effect = errors + [(User.objects.create(username=self.USERNAME), False)] with self.assertRaises(IntegrityError): authentication.authenticate_credentials({'preferred_username': self.USERNAME})
def test_no_preferred_username(self): """ Ensure the service gracefully handles an inability to extract a username from the id token. """ # with preferred_username: all good authentication = JwtAuthentication() user = authentication.authenticate_credentials({'preferred_username': self.USERNAME}) self.assertEqual(user.username, self.USERNAME) # missing preferred_username: exception authentication = JwtAuthentication() with self.assertRaises(AuthenticationFailed): authentication.authenticate_credentials({})
def test_user_creation_failure(self, mock_get_or_create): """ Verify that IntegrityErrors beyond the configured retry limit are raised. """ authentication = JwtAuthentication() errors = [IntegrityError] * (MAX_RETRIES + 1) mock_get_or_create.side_effect = errors + [ (User.objects.create(username=self.USERNAME), False) ] with self.assertRaises(IntegrityError): authentication.authenticate_credentials( {'preferred_username': self.USERNAME})
def test_no_preferred_username(self): """ Ensure the service gracefully handles an inability to extract a username from the id token. """ # with preferred_username: all good authentication = JwtAuthentication() user = authentication.authenticate_credentials( {'preferred_username': self.USERNAME}) self.assertEqual(user.username, self.USERNAME) # missing preferred_username: exception authentication = JwtAuthentication() with self.assertRaises(AuthenticationFailed): authentication.authenticate_credentials({})
def test_robust_user_creation(self, retries, mock_get_or_create): """ Verify that the service is robust to IntegrityErrors during user creation. """ authentication = JwtAuthentication() # If side_effect is an iterable, each call to the mock will return # the next value from the iterable. # See: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.side_effect. errors = [IntegrityError] * retries mock_get_or_create.side_effect = errors + [(User.objects.create(username=self.USERNAME), False)] user = authentication.authenticate_credentials({'preferred_username': self.USERNAME}) self.assertEqual(user.username, self.USERNAME)
def test_robust_user_creation(self, retries, mock_get_or_create): """ Verify that the service is robust to IntegrityErrors during user creation. """ authentication = JwtAuthentication() # If side_effect is an iterable, each call to the mock will return # the next value from the iterable. # See: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.side_effect. errors = [IntegrityError] * retries mock_get_or_create.side_effect = errors + [ (User.objects.create(username=self.USERNAME), False) ] user = authentication.authenticate_credentials( {'preferred_username': self.USERNAME}) self.assertEqual(user.username, self.USERNAME)