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.

4.2 KB
Raw Blame
import datetime
from typing import cast

import pytest
from posthog.test.base import APIBaseTest

from rest_framework import status

from posthog.cloud_utils import is_cloud
from posthog.models import Tag, TaggedItem
from posthog.models.tagged_item_registry import content_type_id_for_legacy_field

from products.actions.backend.facade.testing import create_action

# Testing enterprise properties of actions here (i.e., tagging).


@pytest.mark.ee
class TestActionApi(APIBaseTest):
    def test_create_action_update_delete_tags(self):
        from ee.models.license import License, LicenseManager

        super(LicenseManager, cast(LicenseManager, License.objects)).create(
            key="key_123",
            plan="enterprise",
            valid_until=datetime.datetime(2038, 1, 19, 3, 14, 7),
        )

        response = self.client.post(f"/api/projects/{self.team.id}/actions/", data={"name": "user signed up"})
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.json()["tags"], [])

        response = self.client.patch(
            f"/api/projects/{self.team.id}/actions/{response.json()['id']}",
            data={"name": "user signed up", "tags": ["hello", "random"]},
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(set(response.json()["tags"]), {"hello", "random"})

        response = self.client.patch(
            f"/api/projects/{self.team.id}/actions/{response.json()['id']}",
            data={"name": "user signed up", "tags": []},
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()["tags"], [])

    def test_create_action_with_tags(self):
        from ee.models.license import License, LicenseManager

        super(LicenseManager, cast(LicenseManager, License.objects)).create(
            key="key_123",
            plan="enterprise",
            valid_until=datetime.datetime(2038, 1, 19, 3, 14, 7),
        )

        response = self.client.post(
            f"/api/projects/{self.team.id}/actions/",
            data={
                "name": "user signed up",
                "tags": ["nightly", "is", "a", "good", "girl"],
            },
        )
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(set(response.json()["tags"]), {"nightly", "is", "a", "good", "girl"})

    def test_actions_does_not_nplus1(self):
        from ee.models.license import License, LicenseManager

        super(LicenseManager, cast(LicenseManager, License.objects)).create(
            key="key_123",
            plan="enterprise",
            valid_until=datetime.datetime(2038, 1, 19, 3, 14, 7),
        )

        # Ensure the cloud check is cached to not affect the number of queries
        assert not is_cloud()

        tag = Tag.objects.create(name="tag", team=self.team)
        action_content_type_id = content_type_id_for_legacy_field("action")
        for i in range(20):
            action = create_action(team=self.team, name=f"action_{i}")
            TaggedItem.objects.create(tag=tag, content_type_id=action_content_type_id, object_id=action.id)

        # base request queries + the single bulk access-control preload (one query covering all
        # actions, replacing the previous per-action access-control lookups)
        with self.assertNumQueries(12):
            response = self.client.get(f"/api/projects/{self.team.id}/actions")
        self.assertEqual(response.json()["results"][0]["tags"][0], "tag")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.json()["results"]), 20)

    def test_actions_no_duplicate_tags(self):
        from ee.models.license import License, LicenseManager

        super(LicenseManager, cast(LicenseManager, License.objects)).create(
            key="key_123",
            plan="enterprise",
            valid_until=datetime.datetime(2038, 1, 19, 3, 14, 7),
        )
        response = self.client.post(
            f"/api/projects/{self.team.id}/actions/",
            data={"name": "user signed up", "tags": ["a", "b", "a"]},
        )

        self.assertListEqual(sorted(response.json()["tags"]), ["a", "b"])