: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay-vision): give scanner scout create its serializer context (#94399)
Sean Malliad719e370f0ed
diff --git a/products/replay_vision/backend/api/scanner_scouts.py b/products/replay_vision/backend/api/scanner_scouts.pyindex e7b39f9b23f8..b46a843a1740 100644--- a/products/replay_vision/backend/api/scanner_scouts.py+++ b/products/replay_vision/backend/api/scanner_scouts.py@@ -86,13 +86,16 @@ class ScannerScoutViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):)def create(self, request: Any, **kwargs: Any) -> Response:scanner = self._scanner_for_writing()- payload = ScannerScoutCreateSerializer(data=request.data)- payload.is_valid(raise_exception=True)- validated = payload.validated_data-# Scouts live on the canonical team, so the skill-authoring bar is checked against that team# rather than the URL's: `create_scout_for_source` runs it on whichever team it is given.canonical_team = self.team.parent_team or self.team+ # The nested config checks a Slack destination against the project's integrations and the+ # model pin against the team's flag, so the body serializer needs the context the facade gets.+ serializer_context = {"project_id": self.team.project_id, "team": canonical_team, "request": request}+ payload = ScannerScoutCreateSerializer(data=request.data, context=serializer_context)+ payload.is_valid(raise_exception=True)+ validated = payload.validated_data+result = signals_facade.create_scout_for_source(team=canonical_team,user=request.user,@@ -102,7 +105,7 @@ class ScannerScoutViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):files=[],config_options=validated.get("config", {}),request=request,- serializer_context={"project_id": self.team.project_id, "request": request},+ serializer_context=serializer_context,# From the URL the caller's access was checked against, never from the body.source_product=SCOUT_SOURCE_PRODUCT,source_id=str(scanner.id),diff --git a/products/replay_vision/backend/tests/test_scanner_scouts_api.py b/products/replay_vision/backend/tests/test_scanner_scouts_api.pyindex 7bcb90f1270e..8f9b2109cfd4 100644--- a/products/replay_vision/backend/tests/test_scanner_scouts_api.py+++ b/products/replay_vision/backend/tests/test_scanner_scouts_api.py@@ -2,11 +2,14 @@ from unittest.mock import patchfrom parameterized import parameterized+from posthog.models.integration import Integrationfrom posthog.models.scoping import team_scope+from posthog.models.team import Teamfrom products.replay_vision.backend.scout_source import SCOUT_SOURCE_PRODUCTfrom products.replay_vision.backend.tests.test_api import _VisionAPITestCasefrom products.signals.backend.models import SignalScoutConfig+from products.signals.backend.scout_harness.model_selection import scout_model_pin_catalogclass TestScannerScoutCreate(_VisionAPITestCase):@@ -34,6 +37,63 @@ class TestScannerScoutCreate(_VisionAPITestCase):assert config.source_product == SCOUT_SOURCE_PRODUCTassert config.source_id == str(self.scanner.id)+ def test_a_scout_can_be_created_with_a_slack_destination(self) -> None:+ # The create modal offers a Slack channel, so the destination arrives on the create call+ # rather than a follow-up PATCH. Validating it needs the project and the request in the+ # body serializer's context.+ integration = Integration.objects.create(team=self.team, kind=Integration.IntegrationKind.SLACK)+ response = self.client.post(+ self._scouts_url(str(self.scanner.id)),+ data=self._payload(+ config={+ "output_destinations": {+ "slack": {"integration_id": integration.id, "channel": "CSCOUTS|#scout-findings"}+ }+ },+ ),+ format="json",+ )++ assert response.status_code == 201, response.json()+ with team_scope(self.team.id):+ config = SignalScoutConfig.objects.get(skill_name="signals-scout-daily-digest")+ assert config.output_destinations == {+ "slack": {"integration_id": integration.id, "channel": "CSCOUTS|#scout-findings", "thread_reports": False}+ }++ def test_a_scout_can_be_created_with_a_model_pin(self) -> None:+ # The model gate reads the team from the serializer context, so a create without it rejected+ # every pin as unavailable even where the flag was on.+ model = scout_model_pin_catalog()[0]+ with patch("products.signals.backend.scout_harness.serializers.scout_model_config_enabled", return_value=True):+ response = self.client.post(+ self._scouts_url(str(self.scanner.id)),+ data=self._payload(config={"model": model}),+ format="json",+ )++ assert response.status_code == 201, response.json()+ with team_scope(self.team.id):+ config = SignalScoutConfig.objects.get(skill_name="signals-scout-daily-digest")+ assert config.model == model++ def test_a_slack_destination_on_another_project_is_rejected_as_bad_input(self) -> None:+ # A destination the project does not own is the caller's mistake, so it must come back as a+ # 400 the modal can show — not a 500.+ other_team = Team.objects.create(organization=self.organization, name="other project")+ integration = Integration.objects.create(team=other_team, kind=Integration.IntegrationKind.SLACK)+ response = self.client.post(+ self._scouts_url(str(self.scanner.id)),+ data=self._payload(+ config={"output_destinations": {"slack": {"integration_id": integration.id, "channel": "C1|#c"}}},+ ),+ format="json",+ )++ assert response.status_code == 400, response.json()+ with team_scope(self.team.id):+ assert not SignalScoutConfig.objects.filter(skill_name="signals-scout-daily-digest").exists()+def test_a_source_supplied_in_the_body_cannot_claim_another_scanner(self) -> None:# The pair is the reports endpoint's ownership record, so it must come from the URL the# caller's access was checked against, never from input the caller controls.@@ -123,7 +183,6 @@ class TestScannerScoutCreate(_VisionAPITestCase):# environment clears the default scope check (URL team is the child) while the skill and# config it creates land on the parent.from posthog.models.personal_api_key import PersonalAPIKey- from posthog.models.team import Teamfrom posthog.models.utils import generate_random_token_personal, hash_key_valueenv = Team.objects.create(organization=self.organization, parent_team=self.team, name="env")