Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix: scope create message (#61422)

Alex V6df9549f78be
diff --git a/products/conversations/backend/api/tests/test_tickets.py b/products/conversations/backend/api/tests/test_tickets.py
index 1ff8ef816808..f6647b3754f8 100644
--- a/products/conversations/backend/api/tests/test_tickets.py
+++ b/products/conversations/backend/api/tests/test_tickets.py
@@ -1598,3 +1598,60 @@ class TestComposeTicketAPI(APIBaseTest):
assert response.status_code == expected_status
if expected_detail:
assert expected_detail in response.json()["detail"]
+
+
+class TestTicketPersonalAPIKeyScopes(APIBaseTest):
+ def _auth_with_pak(self, scopes: list[str]) -> None:
+ key = self.create_personal_api_key_with_scopes(scopes)
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {key}")
+
+ def setUp(self):
+ super().setUp()
+ self.ticket = Ticket.objects.create_with_number(
+ team=self.team,
+ channel_source=Channel.WIDGET,
+ widget_session_id="test-session",
+ distinct_id="user-1",
+ status=Status.NEW,
+ )
+
+ @parameterized.expand(
+ [
+ ("list_with_read", "list", "get", None, ["ticket:read"], status.HTTP_200_OK),
+ ("list_with_write", "list", "get", None, ["ticket:write"], status.HTTP_200_OK),
+ ("retrieve_with_read", "retrieve", "get", True, ["ticket:read"], status.HTTP_200_OK),
+ ("retrieve_with_write", "retrieve", "get", True, ["ticket:write"], status.HTTP_200_OK),
+ ("unread_count_with_read", "unread_count", "get", None, ["ticket:read"], status.HTTP_200_OK),
+ ("unread_count_with_write", "unread_count", "get", None, ["ticket:write"], status.HTTP_200_OK),
+ ("list_wrong_scope", "list", "get", None, ["insight:read"], status.HTTP_403_FORBIDDEN),
+ ("retrieve_wrong_scope", "retrieve", "get", True, ["insight:read"], status.HTTP_403_FORBIDDEN),
+ ("unread_count_wrong_scope", "unread_count", "get", None, ["insight:read"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_read_actions(self, _name, action, method, use_detail, scopes, expected_status):
+ self._auth_with_pak(scopes)
+
+ base = f"/api/projects/{self.team.id}/conversations/tickets/"
+ if use_detail:
+ url = f"{base}{self.ticket.id}/"
+ elif action in ("list",):
+ url = base
+ else:
+ url = f"{base}{action}/"
+
+ response = getattr(self.client, method)(url)
+ assert response.status_code == expected_status, f"{_name}: {response.status_code} != {expected_status}"
+
+ @parameterized.expand(
+ [
+ ("compose_with_write", "compose", ["ticket:write"], status.HTTP_400_BAD_REQUEST),
+ ("compose_with_read", "compose", ["ticket:read"], status.HTTP_403_FORBIDDEN),
+ ("compose_wrong_scope", "compose", ["insight:write"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_write_actions(self, _name, action, scopes, expected_status):
+ self._auth_with_pak(scopes)
+ url = f"/api/projects/{self.team.id}/conversations/tickets/{action}/"
+ response = self.client.post(url, {}, format="json")
+ assert response.status_code == expected_status, f"{_name}: {response.status_code} != {expected_status}"
diff --git a/products/conversations/backend/api/tickets.py b/products/conversations/backend/api/tickets.py
index f453eeb322d0..955a49b9edc4 100644
--- a/products/conversations/backend/api/tickets.py
+++ b/products/conversations/backend/api/tickets.py
@@ -247,6 +247,8 @@ class TicketSerializer(TaggedItemSerializerMixin, serializers.ModelSerializer):
class TicketViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
scope_object = "ticket"
+ scope_object_read_actions = ["list", "retrieve", "unread_count"]
+ scope_object_write_actions = ["create", "update", "partial_update", "patch", "compose"]
queryset = Ticket.objects.all()
serializer_class = TicketSerializer
permission_classes = [IsAuthenticated, APIScopePermission, PostHogFeatureFlagPermission]