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())
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())
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())
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())
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)
def verify_jwt_token(token): try: TokenVerifySerializer().validate({"token": token}) except TokenError as e: raise e else: return token
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])
def deff(): TokenVerifySerializer().validate()
def validate(self, attrs): verify = TokenVerifySerializer.validate(self, attrs) return verify