Skip to content

Commit f367a69

Browse files
committed
use more appropriate exception name
1 parent 5a8f450 commit f367a69

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/sentry/models/apitoken.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def __init__(
4444
super().__init__(message)
4545

4646

47+
class NotSupported(Exception):
48+
def __init__(self, message="the method you called is not supported by this token type"):
49+
super().__init__(message)
50+
51+
4752
class ApiTokenManager(ControlOutboxProducingManager):
4853
def create(self, *args, **kwargs):
4954
token_type: AuthTokenType | None = kwargs.get("token_type", None)
@@ -168,7 +173,7 @@ def _plaintext_refresh_token(self):
168173
if self.refresh_token or self.hashed_refresh_token:
169174
return ApiToken.objects.plaintext_refresh_token
170175
else:
171-
raise NotImplementedError("This API token type does not support refresh tokens")
176+
raise NotSupported("This API token type does not support refresh tokens")
172177

173178
def save(self, *args: Any, **kwargs: Any) -> None:
174179
if options.get("apitoken.save-hash-on-create"):
@@ -241,7 +246,7 @@ def get_allowed_origins(self):
241246

242247
def refresh(self, expires_at=None):
243248
if self.token_type == AuthTokenType.USER:
244-
raise NotImplementedError("User auth tokens do not support refreshing the token")
249+
raise NotSupported("User auth tokens do not support refreshing the token")
245250

246251
if expires_at is None:
247252
expires_at = timezone.now() + DEFAULT_EXPIRATION

tests/sentry/models/test_apitoken.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from sentry.conf.server import SENTRY_SCOPE_HIERARCHY_MAPPING, SENTRY_SCOPES
88
from sentry.hybridcloud.models import ApiTokenReplica
9-
from sentry.models.apitoken import ApiToken, PlaintextSecretAlreadyRead
9+
from sentry.models.apitoken import ApiToken, NotSupported, PlaintextSecretAlreadyRead
1010
from sentry.models.integrations.sentry_app_installation import SentryAppInstallation
1111
from sentry.models.integrations.sentry_app_installation_token import SentryAppInstallationToken
1212
from sentry.silo import SiloMode
@@ -77,6 +77,13 @@ def test_last_chars_are_not_set(self):
7777
token = ApiToken.objects.create(user_id=user.id)
7878
assert token.token_last_characters is None
7979

80+
@override_options({"apitoken.save-hash-on-create": True})
81+
def test_hash_exists_on_token(self):
82+
user = self.create_user()
83+
token = ApiToken.objects.create(user_id=user.id)
84+
assert token.hashed_token is not None
85+
assert token.hashed_refresh_token is not None
86+
8087
@override_options({"apitoken.save-hash-on-create": True})
8188
def test_hash_exists_on_user_token(self):
8289
user = self.create_user()
@@ -130,11 +137,19 @@ def test_error_when_accessing_refresh_token_on_user_token(self):
130137
user = self.create_user()
131138
token = ApiToken.objects.create(user_id=user.id, token_type=AuthTokenType.USER)
132139

133-
with pytest.raises(NotImplementedError):
140+
with pytest.raises(NotSupported):
134141
assert token._plaintext_refresh_token is not None
135142

136143
@override_options({"apitoken.save-hash-on-create": True})
137-
def test_user_auth_token_hash(self):
144+
def test_user_auth_token_refresh_raises_error(self):
145+
user = self.create_user()
146+
token = ApiToken.objects.create(user_id=user.id, token_type=AuthTokenType.USER)
147+
148+
with pytest.raises(NotSupported):
149+
token.refresh()
150+
151+
@override_options({"apitoken.save-hash-on-create": True})
152+
def test_user_auth_token_sha256_hash(self):
138153
user = self.create_user()
139154
token = ApiToken.objects.create(user_id=user.id, token_type=AuthTokenType.USER)
140155
expected_hash = hashlib.sha256(token._plaintext_token.encode()).hexdigest()

0 commit comments

Comments
 (0)