def test_user_invalid_scope(self, mock_cache, mock_request): mock_request.is_authorized_for.return_value = False mock_request.get_token_subject.lower.return_value = self.superuser.username mock_cache.get.return_value = None with self.assertRaises(AuthenticationFailed) as cm: JWTAuthBackend.authenticate(mock_request) e = cm.exception self.assertEqual(str(e), 'No token or required scope')
def test_user_does_not_exists(self, mock_cache, mock_request): mock_request.is_authorized_for.return_value = True mock_request.get_token_subject.lower.return_value = 'idonotexist' mock_cache.get.return_value = None with self.assertRaises(AuthenticationFailed) as cm: JWTAuthBackend.authenticate(mock_request) e = cm.exception self.assertEqual(str(e), 'User {} is not authorized'.format('idonotexist'))
def test_user_invalid_scope(self, mock_cache, mock_request, mock_token_data): mock_request.is_authorized_for.return_value = False settings = get_settings() claims = {settings['USER_ID_FIELD']: self.superuser.username} mock_token_data.return_value = claims, self.superuser.username mock_cache.get.return_value = None with self.assertRaises(AuthenticationFailed) as cm: JWTAuthBackend.authenticate(mock_request) e = cm.exception self.assertEqual(str(e), 'No token or required scope')
def test_user_does_not_exists(self, mock_cache, mock_request, mock_token_data): mock_request.is_authorized_for.return_value = True settings = get_settings() claims = {settings['USER_ID_FIELD']: 'idonotexist'} mock_token_data.return_value = claims, 'idonotexist' mock_cache.get.return_value = None with self.assertRaises(AuthenticationFailed) as cm: JWTAuthBackend.authenticate(mock_request) e = cm.exception self.assertEqual(str(e), 'User {} is not authorized'.format('idonotexist'))
def resolve(self, next, root, info, **args): try: info.context.user, _ = JWTAuthBackend.authenticate(info.context) except AuthenticationFailed: info.context.user = AnonymousUser promise = next(root, info, **args) return promise
def test_user_not_in_cache(self, mock_cache, mock_request): mock_request.is_authorized_for.return_value = True mock_request.get_token_subject.lower.return_value = self.superuser.username mock_cache.get.return_value = None user, scope = JWTAuthBackend.authenticate(mock_request) self.assertEqual(user.username, self.superuser.username)
def test_user_not_in_cache(self, mock_cache, mock_request, mock_token_data): mock_request.is_authorized_for.return_value = True settings = get_settings() claims = {settings['USER_ID_FIELD']: self.superuser.username} mock_token_data.return_value = claims, self.superuser.username mock_cache.get.return_value = None user, scope = JWTAuthBackend.authenticate(mock_request) self.assertEqual(user.username, self.superuser.username)