예제 #1
0
    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')
예제 #2
0
    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'))
예제 #3
0
    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')
예제 #4
0
    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'))
예제 #5
0
    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
예제 #6
0
    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)
예제 #7
0
    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)