Skip to content

feat: add support for name on api token endpoints #64493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sentry/api/endpoints/api_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from rest_framework import serializers
from rest_framework.fields import CharField
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
Expand All @@ -21,6 +22,7 @@


class ApiTokenSerializer(serializers.Serializer):
name = CharField(max_length=255, allow_blank=True, required=False)
scopes = MultipleChoiceField(required=True, choices=settings.SENTRY_SCOPES)


Expand Down Expand Up @@ -62,6 +64,7 @@ def post(self, request: Request) -> Response:

token = ApiToken.objects.create(
user_id=request.user.id,
name=result.get("name", None),
scope_list=result["scopes"],
refresh_token=None,
expires_at=None,
Expand Down
1 change: 1 addition & 0 deletions src/sentry/api/serializers/models/apitoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def serialize(self, obj, attrs, user, **kwargs):
data = {
"id": str(obj.id),
"scopes": obj.get_scopes(),
"name": obj.name,
"application": attrs["application"],
"expiresAt": obj.expires_at,
"dateCreated": obj.date_added,
Expand Down
36 changes: 36 additions & 0 deletions tests/sentry/api/endpoints/test_api_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ def test_invalid_choice(self):
assert response.status_code == 400
assert not ApiToken.objects.filter(user=self.user).exists()

def test_with_name(self):
self.login_as(self.user)
url = reverse("sentry-api-0-api-tokens")
response = self.client.post(
url,
data={"name": "testname1", "scopes": ["event:read"]},
)
assert response.status_code == 201

token = ApiToken.objects.get(user=self.user)
assert token.name == "testname1"

response = self.client.get(url)
assert response.status_code == 200, response.content
assert len(response.data) == 1

assert response.data[0]["name"] == "testname1"

def test_without_name(self):
self.login_as(self.user)
url = reverse("sentry-api-0-api-tokens")
response = self.client.post(
url,
data={"scopes": ["event:read"]},
)
assert response.status_code == 201

token = ApiToken.objects.get(user=self.user)
assert token.name is None

response = self.client.get(url)
assert response.status_code == 200, response.content
assert len(response.data) == 1

assert response.data[0]["name"] is None


@control_silo_test
class ApiTokensDeleteTest(APITestCase):
Expand Down