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.

refactor(messaging): tighten message category field validation (#57563)

Tom Piccirello28aef30176b5
diff --git a/products/messaging/backend/api/message_templates.py b/products/messaging/backend/api/message_templates.py
index 667515cf7546..4d335a367290 100644
--- a/products/messaging/backend/api/message_templates.py
+++ b/products/messaging/backend/api/message_templates.py
@@ -5,8 +5,10 @@ from rest_framework.permissions import IsAuthenticated
from posthog.api.forbid_destroy_model import ForbidDestroyModel
from posthog.api.routing import TeamAndOrgViewSetMixin
+from posthog.api.scoped_related_fields import TeamScopedPrimaryKeyRelatedField
from posthog.api.shared import UserBasicSerializer
+from products.messaging.backend.models.message_category import MessageCategory
from products.messaging.backend.models.message_template import MessageTemplate
@@ -25,6 +27,9 @@ class MessageTemplateContentSerializer(serializers.Serializer):
class MessageTemplateSerializer(serializers.ModelSerializer):
created_by = UserBasicSerializer(read_only=True)
content = MessageTemplateContentSerializer(required=False)
+ message_category = TeamScopedPrimaryKeyRelatedField(
+ queryset=MessageCategory.objects.all(), required=False, allow_null=True
+ )
class Meta:
model = MessageTemplate
diff --git a/products/messaging/backend/api/test/test_message_templates.py b/products/messaging/backend/api/test/test_message_templates.py
index 8d3e3ffeb904..1781b06fd037 100644
--- a/products/messaging/backend/api/test/test_message_templates.py
+++ b/products/messaging/backend/api/test/test_message_templates.py
@@ -4,6 +4,7 @@ from rest_framework import status
from posthog.models import Organization, Team
+from products.messaging.backend.models.message_category import MessageCategory
from products.messaging.backend.models.message_template import MessageTemplate
@@ -110,3 +111,32 @@ class TestMessageTemplatesAPI(APIBaseTest):
format="json",
)
assert response.status_code == status.HTTP_201_CREATED
+
+ def test_cannot_bind_template_to_other_teams_message_category_via_patch(self):
+ """Regression: PATCH must not accept a MessageCategory pk owned by a different team."""
+ foreign_category = MessageCategory.objects.create(team=self.other_team, key="foreign-key", name="Foreign")
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.id}/messaging_templates/{self.message_template.id}/",
+ data={"message_category": str(foreign_category.id)},
+ format="json",
+ )
+
+ # TeamScopedPrimaryKeyRelatedField restricts the queryset to the caller's
+ # team, so the foreign id is unknown — DRF returns 400.
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ self.message_template.refresh_from_db()
+ assert self.message_template.message_category_id is None
+
+ def test_can_bind_template_to_own_teams_message_category(self):
+ own_category = MessageCategory.objects.create(team=self.team, key="own-key", name="Own")
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.id}/messaging_templates/{self.message_template.id}/",
+ data={"message_category": str(own_category.id)},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_200_OK
+ self.message_template.refresh_from_db()
+ assert self.message_template.message_category_id == own_category.id