Ejemplo n.º 1
0
    def test_it_should_not_raise_token_error_if_token_has_wrong_type(self):
        token = RefreshToken()
        token[api_settings.TOKEN_TYPE_CLAIM] = 'wrong_type'

        s = TokenVerifySerializer(data={'token': str(token)})

        self.assertTrue(s.is_valid())
Ejemplo n.º 2
0
    def test_obtain_provides_valid_token_for_given_user(self):
        user = create_user()
        res = TokenStrategy.obtain(user)
        self.assertEqual(res["user"], user)

        data = {"token": res["access"]}
        serializer = TokenVerifySerializer(data=data)
        self.assertTrue(serializer.is_valid())
Ejemplo n.º 3
0
    def test_obtain_provides_valid_token_for_given_user(self):
        user = create_user()
        res = TokenStrategy.obtain(user)
        self.assertEqual(res['user'], user)

        data = {'token': res['access']}
        serializer = TokenVerifySerializer(data=data)
        self.assertTrue(serializer.is_valid())
Ejemplo n.º 4
0
    def test_token_verify_serializer_should_not_honour_blacklist_if_blacklisting_not_enabled(
        self,
    ):
        with override_api_settings(BLACKLIST_AFTER_ROTATION=False):
            refresh_token = RefreshToken.for_user(self.user)
            refresh_token.blacklist()

            serializer = TokenVerifySerializer(data={"token": str(refresh_token)})
            self.assertTrue(serializer.is_valid())
Ejemplo n.º 5
0
    def test_it_should_raise_token_error_if_token_invalid(self):
        token = RefreshToken()
        del token['exp']

        s = TokenVerifySerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn("has no 'exp' claim", e.exception.args[0])

        token.set_exp(lifetime=-timedelta(days=1))

        s = TokenVerifySerializer(data={'token': str(token)})

        with self.assertRaises(TokenError) as e:
            s.is_valid()

        self.assertIn('invalid or expired', e.exception.args[0])
Ejemplo n.º 6
0
    def test_it_should_return_given_token_if_everything_ok(self):
        refresh = RefreshToken()
        refresh['test_claim'] = 'arst'

        # Serializer validates
        s = TokenVerifySerializer(data={'token': str(refresh)})

        now = aware_utcnow() - api_settings.ACCESS_TOKEN_LIFETIME / 2

        with patch('rest_framework_simplejwt.tokens.aware_utcnow') as fake_aware_utcnow:
            fake_aware_utcnow.return_value = now
            self.assertTrue(s.is_valid())

        self.assertEqual(len(s.validated_data), 0)