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.

15.2 KB
Raw Blame
import pytest
from posthog.test.base import BaseTest
from unittest.mock import Mock, patch

from django.apps import apps

from asgiref.sync import async_to_sync
from parameterized import parameterized

from posthog.constants import AvailableFeature
from posthog.models import OrganizationMembership
from posthog.models.sharing_configuration import SharingConfiguration

from products.access_control.backend.facade.user_access_control import UserAccessControl
from products.access_control.backend.models.access_control import AccessControl
from products.data_modeling.backend.facade.models import DataWarehouseSavedQuery
from products.notebooks.backend import logic
from products.notebooks.backend.analytics import NotebookCreationSource
from products.notebooks.backend.facade import api, content
from products.notebooks.backend.facade.contracts import NotebookCellLimitExceeded, NotebookContentEditNotAllowed
from products.notebooks.backend.models import Notebook, ResourceNotebook
from products.notebooks.backend.sql_v2_state import MAX_NOTEBOOK_CELLS


def _create_account(team):
    # apps.get_model avoids a notebooks -> customer_analytics import edge (the FK is a string ref).
    Account = apps.get_model("customer_analytics", "Account")
    return Account.objects.unscoped().create(team=team, name="Acme")


class TestNotebooksFacade(BaseTest):
    def _doc(self, text: str = "hello") -> dict:
        return {"type": "doc", "content": [content.create_paragraph_with_text(text)]}

    @patch("products.notebooks.backend.facade.api.capture_notebook_created")
    def test_create_notebook_returns_contract(self, capture):
        with self.captureOnCommitCallbacks(execute=True):
            data = api.create_notebook(
                self.team.id,
                title="My notebook",
                content=self._doc(),
                created_by_id=self.user.id,
                last_modified_by_id=self.user.id,
            )
            capture.assert_not_called()
        capture.assert_called_once()
        self.assertEqual(data.title, "My notebook")
        self.assertEqual(data.visibility, Notebook.Visibility.DEFAULT)
        self.assertEqual(data.created_by_id, self.user.id)
        stored = Notebook.objects.get(id=data.id)
        self.assertEqual(stored.team_id, self.team.id)
        self.assertFalse(stored.deleted)

    def test_get_notebook_round_trips_fields(self):
        notebook = Notebook.objects.create(team=self.team, title="T", content=self._doc("body"), text_content="body")
        data = api.get_notebook(self.team.id, notebook.short_id)
        assert data is not None
        self.assertEqual(data.short_id, notebook.short_id)
        self.assertEqual(data.title, "T")
        self.assertEqual(data.text_content, "body")
        self.assertEqual(data.content, self._doc("body"))

    def test_get_notebook_excludes_deleted_by_default(self):
        notebook = Notebook.objects.create(team=self.team, title="gone", deleted=True)
        self.assertIsNone(api.get_notebook(self.team.id, notebook.short_id))
        self.assertIsNotNone(api.get_notebook(self.team.id, notebook.short_id, include_deleted=True))

    def test_notebook_exists_is_team_scoped(self):
        from posthog.models import Team

        notebook = Notebook.objects.create(team=self.team, title="t")
        other_team = Team.objects.create(organization=self.organization, name="other")
        self.assertTrue(api.notebook_exists(self.team.id, notebook.short_id))
        self.assertFalse(api.notebook_exists(other_team.id, notebook.short_id))

    def test_get_notebook_short_ids_for_creator(self):
        mine = Notebook.objects.create(team=self.team, title="mine", created_by=self.user)
        Notebook.objects.create(team=self.team, title="theirs")
        short_ids = api.get_notebook_short_ids_for_creator(self.team.project_id, self.user.id)
        self.assertEqual(short_ids, [mine.short_id])

    def test_activity_summary_orders_by_recency(self):
        Notebook.objects.create(team=self.team, title="old")
        newer = Notebook.objects.create(team=self.team, title="new")
        summary = api.get_notebook_activity_summary(self.team.id, limit=1)
        self.assertEqual(summary.total_count, 2)
        self.assertEqual(len(summary.recent), 1)
        self.assertEqual(summary.recent[0].short_id, newer.short_id)

    @patch("products.notebooks.backend.facade.api.capture_notebook_created")
    def test_create_group_notebook_links_internal_notebook(self, mock_capture):
        data = api.create_group_notebook(self.team.id, group_id=42, title="Notes", content=self._doc())
        self.assertEqual(data.visibility, Notebook.Visibility.INTERNAL)
        self.assertTrue(api.group_has_notebook(42))
        self.assertEqual(api.get_group_notebook_short_id(42), data.short_id)
        self.assertFalse(api.group_has_notebook(43))
        mock_capture.assert_called_once()
        self.assertEqual(mock_capture.call_args.kwargs["creation_source"], NotebookCreationSource.GROUP)

    def test_create_account_notebook_and_list_notes(self):
        account = _create_account(self.team)
        data = api.create_account_notebook(
            self.team.id, account.id, title="Recap", content=self._doc(), created_by_id=self.user.id
        )
        self.assertEqual(data.visibility, Notebook.Visibility.INTERNAL)
        self.assertTrue(ResourceNotebook.objects.filter(account=account, notebook_id=data.id).exists())
        notes = api.list_account_internal_notes(account.id)
        self.assertEqual([n.short_id for n in notes], [data.short_id])
        self.assertEqual(notes[0].title, "Recap")

    def test_list_account_notes_excludes_deleted_and_non_internal(self):
        account = _create_account(self.team)
        deleted = Notebook.objects.create(
            team=self.team, title="d", deleted=True, visibility=Notebook.Visibility.INTERNAL
        )
        external = Notebook.objects.create(team=self.team, title="e", visibility=Notebook.Visibility.DEFAULT)
        ResourceNotebook.objects.create(notebook=deleted, account=account)
        ResourceNotebook.objects.create(notebook=external, account=account)
        self.assertEqual(api.list_account_internal_notes(account.id), [])

    def test_account_notebook_crud_surface(self):
        account = _create_account(self.team)
        created = api.create_account_notebook(
            self.team.id, str(account.id), title="Q3", content=self._doc(), created_by_id=self.user.id
        )

        listed = api.list_account_notebooks(str(account.id))
        self.assertEqual([n.short_id for n in listed], [created.short_id])
        self.assertEqual(listed[0].title, "Q3")
        assert listed[0].created_by is not None
        self.assertEqual(listed[0].created_by.id, self.user.id)
        self.assertEqual(listed[0].created_by.email, self.user.email)

        fetched = api.get_account_notebook(str(account.id), created.short_id)
        assert fetched is not None
        self.assertEqual(fetched.short_id, created.short_id)

        self.assertTrue(api.delete_account_notebook(str(account.id), created.short_id))
        self.assertIsNone(api.get_account_notebook(str(account.id), created.short_id))
        self.assertFalse(api.delete_account_notebook(str(account.id), "missing"))

    def test_get_account_notebook_excludes_non_internal(self):
        account = _create_account(self.team)
        external = Notebook.objects.create(team=self.team, title="e", visibility=Notebook.Visibility.DEFAULT)
        ResourceNotebook.objects.create(notebook=external, account=account)
        self.assertEqual(api.list_account_notebooks(str(account.id)), [])
        self.assertIsNone(api.get_account_notebook(str(account.id), external.short_id))


class TestNotebooksFacadeAsync(BaseTest):
    def _doc(self, text: str = "hi") -> dict:
        return {"type": "doc", "content": [content.create_paragraph_with_text(text)]}

    @parameterized.expand([("update",), ("upsert",)])
    @patch("posthoganalytics.feature_enabled", return_value=True)
    def test_shared_content_validation_preserves_notebook(self, writer: str, _feature_enabled: Mock) -> None:
        self.organization.available_product_features = [
            {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}
        ]
        self.organization.save()
        membership = OrganizationMembership.objects.get(user=self.user, organization=self.organization)
        membership.level = OrganizationMembership.Level.MEMBER
        membership.save()
        DataWarehouseSavedQuery.objects.create(
            team=self.team,
            name="daily_summary",
            query={"kind": "HogQLQuery", "query": "SELECT 5 AS total"},
            columns={"total": "Integer"},
        )
        AccessControl.objects.create(team=self.team, resource="warehouse_objects", access_level="none")
        notebook = Notebook.objects.create(team=self.team, title="Original", content=self._doc())
        SharingConfiguration.objects.create(team=self.team, notebook=notebook, enabled=True)
        new_content = {
            "type": "doc",
            "content": [
                {
                    "type": "ph-query",
                    "attrs": {
                        "nodeId": "daily",
                        "query": {"kind": "HogQLQuery", "query": "SELECT total FROM daily_summary"},
                    },
                }
            ],
        }

        with self.assertRaisesRegex(NotebookContentEditNotAllowed, "daily_summary"):
            if writer == "update":
                async_to_sync(api.aupdate_notebook_content)(
                    self.team.id,
                    notebook.short_id,
                    user=self.user,
                    title="Updated",
                    content=new_content,
                    text_content="Updated",
                )
            else:
                async_to_sync(api.aupsert_notebook)(
                    self.team.id,
                    notebook.short_id,
                    user=self.user,
                    created_by_id=self.user.id,
                    title="Updated",
                    content=new_content,
                    text_content="Updated",
                )
        original_version = notebook.version
        notebook.refresh_from_db()
        self.assertEqual(notebook.content, self._doc())
        self.assertEqual(notebook.title, "Original")
        self.assertEqual(notebook.version, original_version)

    @pytest.mark.asyncio
    async def test_anotebook_exists_and_aget(self):
        notebook = await Notebook.objects.acreate(team=self.team, title="async", content=self._doc())
        self.assertTrue(await api.anotebook_exists(self.team.id, notebook.short_id))
        data = await api.aget_notebook(self.team.id, notebook.short_id)
        assert data is not None
        self.assertEqual(data.short_id, notebook.short_id)

    @pytest.mark.asyncio
    @patch("products.notebooks.backend.facade.api.capture_notebook_created")
    async def test_aupsert_creates_then_overwrites_and_bumps_version(self, mock_capture):
        data, created = await api.aupsert_notebook(
            self.team.id,
            "abc123",
            created_by_id=self.user.id,
            user=self.user,
            title="first",
            content=self._doc("first"),
        )
        self.assertTrue(created)
        self.assertEqual(data.version, 0)
        # Emits `notebook created` once, on the create, labelled max_ai.
        mock_capture.assert_called_once()
        self.assertEqual(mock_capture.call_args.kwargs["creation_source"], NotebookCreationSource.MAX_AI)

        data2, created2 = await api.aupsert_notebook(
            self.team.id,
            "abc123",
            created_by_id=self.user.id,
            user=self.user,
            title="second",
            content=self._doc("second"),
        )
        self.assertFalse(created2)
        self.assertEqual(data2.id, data.id)
        self.assertEqual(data2.title, "second")
        self.assertEqual(data2.version, 1)
        # The overwrite is an update, not a create — no second event (guards double-counting).
        mock_capture.assert_called_once()

    @pytest.mark.asyncio
    async def test_aupdate_notebook_content_bumps_version(self):
        notebook = await Notebook.objects.acreate(team=self.team, short_id="upd123", title="t", content=self._doc())
        data = await api.aupdate_notebook_content(
            self.team.id,
            "upd123",
            content=self._doc("updated"),
            title="t2",
            text_content="updated",
            user=self.user,
        )
        assert data is not None
        self.assertEqual(data.title, "t2")
        self.assertEqual(data.text_content, "updated")
        self.assertEqual(data.version, notebook.version + 1)

    def _cells_doc(self, count: int) -> dict:
        markdown = "\n\n".join(
            f'<SQLV2 nodeId="s{index}" code="select {index}" returnVariable="df{index}" />' for index in range(count)
        )
        return {
            "type": "doc",
            "content": [{"type": "ph-markdown-notebook", "attrs": {"nodeId": "md", "markdown": markdown}}],
        }

    @pytest.mark.asyncio
    async def test_aupdate_notebook_content_refuses_growth_past_the_cell_ceiling(self):
        # This writer is a queryset update, so it fires no serializer, no save() hook, and no
        # signal. Max saves notebooks through it, so a ceiling enforced only at the API layer
        # would not apply to anything Max writes.
        await Notebook.objects.acreate(team=self.team, short_id="cap001", title="t", content=self._cells_doc(0))
        with self.assertRaises(NotebookCellLimitExceeded):
            await api.aupdate_notebook_content(
                self.team.id,
                "cap001",
                content=self._cells_doc(MAX_NOTEBOOK_CELLS + 1),
                title="t",
                text_content="",
                user=self.user,
            )

    @pytest.mark.asyncio
    async def test_aupsert_notebook_refuses_the_ceiling_without_writing_a_partial_row(self):
        # The check has to run before aget_or_create: after it, the over-limit notebook is
        # already stored by the time the ceiling rejects it.
        with self.assertRaises(NotebookCellLimitExceeded):
            await api.aupsert_notebook(
                self.team.id,
                "cap002",
                created_by_id=self.user.id,
                user=self.user,
                title="t",
                content=self._cells_doc(MAX_NOTEBOOK_CELLS + 1),
            )
        self.assertFalse(await Notebook.objects.filter(team=self.team, short_id="cap002").aexists())

    @pytest.mark.asyncio
    async def test_acan_user_edit_notebook_creator(self):
        notebook = await Notebook.objects.acreate(team=self.team, title="t", created_by=self.user)
        uac = UserAccessControl(user=self.user, team=self.team)
        self.assertTrue(await api.acan_user_edit_notebook(self.team.id, notebook.short_id, user_access_control=uac))

    @pytest.mark.asyncio
    async def test_acan_user_edit_missing_notebook(self):
        uac = UserAccessControl(user=self.user, team=self.team)
        self.assertFalse(await api.acan_user_edit_notebook(self.team.id, "missing", user_access_control=uac))


def test_logic_is_internal_only():
    """The facade is the public surface; logic stays an implementation detail."""
    assert hasattr(logic, "create_notebook")
    assert hasattr(api, "create_notebook")