From e82c0bd693e1d0391f377eca32d30156f19ace89 Mon Sep 17 00:00:00 2001 From: mdtro <20070360+mdtro@users.noreply.github.com> Date: Fri, 5 Apr 2024 12:43:47 -0500 Subject: [PATCH] test refresh tokens serialization --- tests/sentry/api/serializers/test_apitoken.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/sentry/api/serializers/test_apitoken.py b/tests/sentry/api/serializers/test_apitoken.py index 29d9827a283e1a..5ba411da47f194 100644 --- a/tests/sentry/api/serializers/test_apitoken.py +++ b/tests/sentry/api/serializers/test_apitoken.py @@ -1,5 +1,9 @@ from sentry.api.serializers import ApiTokenSerializer +from sentry.models.apitoken import ApiToken +from sentry.silo.base import SiloMode from sentry.testutils.cases import TestCase +from sentry.testutils.helpers.options import override_options +from sentry.testutils.silo import assume_test_silo_mode class TestApiTokenSerializer(TestCase): @@ -38,6 +42,33 @@ def test_when_flag_is_false(self) -> None: assert "token" not in serialized_object +class TestRefreshTokens(TestApiTokenSerializer): + def setUp(self) -> None: + super().setUp() + attrs = self._serializer.get_attrs(item_list=[self._token], user=self._user) + attrs["application"] = None + self._attrs = attrs + + def test_no_refresh_token_on_user_token(self) -> None: + serialized_object = self._serializer.serialize( + obj=self._token, user=self._user, attrs=self._attrs + ) + + assert "refreshToken" not in serialized_object + + @override_options({"apitoken.save-hash-on-create": True}) + def test_refresh_token_on_non_user_token(self) -> None: + with assume_test_silo_mode(SiloMode.CONTROL): + token = ApiToken.objects.create(user=self._user) + assert token.hashed_refresh_token is not None + + serialized_object = self._serializer.serialize( + obj=token, user=self._user, attrs=self._attrs + ) + + assert "refreshToken" in serialized_object + + class TestLastTokenCharacters(TestApiTokenSerializer): def test_field_is_returned(self) -> None: attrs = self._serializer.get_attrs(item_list=[self._token], user=self._user)