Skip to content

Commit

Permalink
test refresh tokens serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtro committed Apr 10, 2024
1 parent 04acd2c commit 3a86fa6
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/sentry/api/serializers/test_apitoken.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3a86fa6

Please sign in to comment.