From cc4113fdc03fbc0d3ae099d83d7ccc7f51e651e6 Mon Sep 17 00:00:00 2001 From: Raquel Smith Date: Tue, 29 Oct 2024 20:31:06 -0700 Subject: [PATCH] add test for updating product intent --- posthog/api/team.py | 2 +- posthog/api/test/test_team.py | 45 +++++++++++++++++++ .../models/product_intent/product_intent.py | 8 ++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/posthog/api/team.py b/posthog/api/team.py index e260365d2d8e9..2d45d057b1496 100644 --- a/posthog/api/team.py +++ b/posthog/api/team.py @@ -216,7 +216,7 @@ def get_live_events_token(self, team: Team) -> Optional[str]: ) def get_product_intents(self, obj): - calculate_product_activation.delay(obj, only_calc_if_days_since_last_checked=1) + calculate_product_activation.delay(obj.id, only_calc_if_days_since_last_checked=1) return ProductIntent.objects.filter(team=obj).values( "product_type", "created_at", "onboarding_completed_at", "updated_at" ) diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py index 3a881facfea44..7b442c73c27d5 100644 --- a/posthog/api/test/test_team.py +++ b/posthog/api/test/test_team.py @@ -1051,6 +1051,51 @@ def test_can_add_product_intent( team=self.team, ) + @patch("posthog.api.team.calculate_product_activation.delay", MagicMock()) + @patch("posthog.models.product_intent.ProductIntent.check_and_update_activation") + @patch("posthog.api.project.report_user_action") + @patch("posthog.api.team.report_user_action") + @freeze_time("2024-01-01T00:00:00Z") + def test_can_update_product_intent_if_already_exists( + self, + mock_report_user_action: MagicMock, + mock_report_user_action_legacy_endpoint: MagicMock, + mock_check_and_update_activation: MagicMock, + ) -> None: + intent = ProductIntent.objects.create(team=self.team, product_type="product_analytics") + original_created_at = intent.created_at + assert original_created_at == datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC) + # change the time of the existing intent + with freeze_time("2024-01-02T00:00:00Z"): + if self.client_class is EnvironmentToProjectRewriteClient: + mock_report_user_action = mock_report_user_action_legacy_endpoint + response = self.client.patch( + f"/api/environments/{self.team.id}/add_product_intent/", + {"product_type": "product_analytics"}, + headers={"Referer": "https://posthogtest.com/my-url", "X-Posthog-Session-Id": "test_session_id"}, + ) + assert response.status_code == status.HTTP_201_CREATED + product_intent = ProductIntent.objects.get(team=self.team, product_type="product_analytics") + assert product_intent.updated_at == datetime(2024, 1, 2, 0, 0, 0, tzinfo=UTC) + assert product_intent.created_at == original_created_at + mock_check_and_update_activation.assert_called_once() + mock_report_user_action.assert_called_once_with( + self.user, + "user showed product intent", + { + "product_key": "product_analytics", + "$current_url": "https://posthogtest.com/my-url", + "$session_id": "test_session_id", + "intent_context": None, + "$set_once": {"first_onboarding_product_selected": "product_analytics"}, + "is_first_intent_for_product": False, + "intent_created_at": datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC), + "intent_updated_at": datetime(2024, 1, 2, 0, 0, 0, tzinfo=UTC), + "realm": get_instance_realm(), + }, + team=self.team, + ) + @patch("posthog.api.project.report_user_action") @patch("posthog.api.team.report_user_action") def test_can_complete_product_onboarding( diff --git a/posthog/models/product_intent/product_intent.py b/posthog/models/product_intent/product_intent.py index 07ca8229f6fbf..85ec938eb1129 100644 --- a/posthog/models/product_intent/product_intent.py +++ b/posthog/models/product_intent/product_intent.py @@ -3,7 +3,6 @@ from celery import shared_task from django.db import models -from posthog.event_usage import report_team_action from posthog.models.insight import Insight from posthog.models.team.team import Team from posthog.models.utils import UUIDModel @@ -83,8 +82,10 @@ def check_and_update_activation(self) -> None: self.report_activation("data_warehouse") def report_activation(self, product_key: str) -> None: + from posthog.event_usage import report_team_action + report_team_action( - self.team.organization, + self.team, "product intent marked activated", { "product_key": product_key, @@ -96,11 +97,12 @@ def report_activation(self, product_key: str) -> None: @shared_task(ignore_result=True) -def calculate_product_activation(team: Team, only_calc_if_days_since_last_checked: int = 1) -> None: +def calculate_product_activation(team_id: int, only_calc_if_days_since_last_checked: int = 1) -> None: """ Calculate product activation for a team. Only calculate if it's been more than `only_calc_if_days_since_last_checked` days since the last activation check. """ + team = Team.objects.get(id=team_id) product_intents = ProductIntent.objects.filter(team=team) for product_intent in product_intents: if product_intent.activated_at: