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.

9.9 KB
Raw Blame
import os
import re
import time
import random
from collections.abc import Iterator
from typing import TYPE_CHECKING

import pytest
from unittest.mock import patch

import responses
from temporalio.testing import ActivityEnvironment

from posthog.constants import AvailableFeature
from posthog.models import Integration, OAuthApplication, Organization, OrganizationMembership, Team, User
from posthog.temporal.common.logger import configure_logger
from posthog.temporal.tests.utils.logger import restored_structlog_config

from products.tasks.backend.logic.services.sandbox import Sandbox, SandboxConfig, SandboxTemplate
from products.tasks.backend.models import SandboxSnapshot, Task, TaskRun
from products.tasks.backend.temporal.create_snapshot.activities.get_snapshot_context import SnapshotContext
from products.tasks.backend.temporal.oauth import ARRAY_APP_CLIENT_ID_DEV
from products.tasks.backend.temporal.process_task.activities.get_task_processing_context import TaskProcessingContext

if TYPE_CHECKING:
    from pytest_django.fixtures import Settings


def _runs_on_internal_pr() -> bool:
    value = os.getenv("RUNS_ON_INTERNAL_PR")
    if value is None:
        return True
    return value.lower() in {"1", "true"}


@pytest.fixture
def activity_environment():
    """Return a testing temporal ActivityEnvironment."""
    return ActivityEnvironment()


@pytest.fixture
def sandbox_ai_gateway(settings: "Settings", organization: Organization) -> Iterator[None]:
    settings.SANDBOX_AI_GATEWAY_URL = "https://ai-gateway.example.com"
    settings.SANDBOX_AI_GATEWAY_PRODUCTS = "posthog_code"
    settings.SANDBOX_AI_GATEWAY_MINT_KEY = "phs_test_mint"
    organization.available_product_features = [
        {"key": AvailableFeature.POSTHOG_CODE_USAGE, "name": "PostHog Code usage"}
    ]
    organization.save(update_fields=["available_product_features"])

    with (
        responses.RequestsMock(assert_all_requests_are_fired=False) as gateway,
        patch("posthoganalytics.get_feature_flag_payload", return_value=None),
    ):
        # The Modal integration tests still use real sandboxes; only the gateway is fake.
        gateway.add_passthru(re.compile(r"^(?!https://ai-gateway\.example\.com(?:/|$))"))
        gateway.post(
            "https://ai-gateway.example.com/v1/tokens",
            json={"token": "phe_test_sandbox"},
            status=201,
        )
        yield


@pytest.fixture
def assert_sandbox_shutdown():
    """Assert a sandbox reaches SHUTDOWN, polling because `Sandbox.destroy()` does not wait.

    `destroy()` fires Modal's terminate RPC and returns immediately, by design: the sandbox
    TTL is the backstop if termination never lands. So the shutdown a cleanup activity causes
    is only observable some time after the activity returns.
    """

    def _assert_sandbox_shutdown(sandbox_id: str, timeout_seconds: float = 60.0) -> None:
        deadline = time.monotonic() + timeout_seconds
        while Sandbox.get_by_id(sandbox_id).is_running():
            assert time.monotonic() < deadline, f"sandbox {sandbox_id} still running after {timeout_seconds}s"
            time.sleep(1)

    return _assert_sandbox_shutdown


@pytest.fixture(autouse=True)
def posthog_code_oauth_app(request):
    """Create the Array OAuth application for DB-backed tests.

    Skipped for tests without a `django_db` marker — those can't touch the
    ORM, so seeding the OAuth row would just raise.
    """
    has_db = "django_db" in {m.name for m in request.node.iter_markers()}
    if not has_db:
        yield None
        return

    if not _runs_on_internal_pr():
        pytest.skip("Skipping test that requires internal secrets on external PRs")
    app, _ = OAuthApplication.objects.get_or_create(
        client_id=ARRAY_APP_CLIENT_ID_DEV,
        defaults={
            "name": "Array Test App",
            "client_type": OAuthApplication.CLIENT_PUBLIC,
            "authorization_grant_type": OAuthApplication.GRANT_AUTHORIZATION_CODE,
            "redirect_uris": "https://app.posthog.com/callback",
            "algorithm": "RS256",
        },
    )
    yield app


@pytest.fixture
def organization():
    """A test organization."""
    name = f"TasksTestOrg-{random.randint(1, 99999)}"
    org = Organization.objects.create(name=name, is_ai_data_processing_approved=True)
    org.save()

    yield org

    org.delete()


@pytest.fixture
def team(organization):
    """A test team."""
    name = f"TasksTestTeam-{random.randint(1, 99999)}"
    team = Team.objects.create(organization=organization, name=name)
    team.save()

    yield team

    team.delete()


@pytest.fixture
def test_team(team):
    """Alias for team fixture."""
    return team


@pytest.fixture
def user(team):
    user = User.objects.create(
        email=f"test-{random.randint(1, 99999)}@example.com",
        password="testpassword123",
    )

    OrganizationMembership.objects.create(
        user=user,
        organization_id=team.organization_id,
    )

    yield user

    user.delete()


@pytest.fixture
def github_integration(team):
    """Create a test GitHub integration."""
    integration = Integration.objects.create(
        team=team,
        kind="github",
        sensitive_config={"access_token": "fake_token"},
    )

    # The fake installation must not call GitHub when minting scoped tokens either.
    with patch(
        "posthog.models.integration.GitHubIntegration.mint_scoped_installation_token", return_value="fake_token"
    ):
        yield integration

    integration.delete()


@pytest.fixture
def test_task(team, user, github_integration):
    """Create a test task."""

    task = Task.objects.create(
        team=team,
        created_by=user,
        title="Test Task for Temporal Activities",
        description="This is a test task for testing temporal activities",
        origin_product=Task.OriginProduct.USER_CREATED,
        github_integration=github_integration,
        repository="posthog/posthog-js",
    )

    yield task

    task.soft_delete()


@pytest.fixture
def test_task_run(test_task):
    """Create a test task run."""
    task_run = TaskRun.objects.create(
        task=test_task,
        team=test_task.team,
        status=TaskRun.Status.QUEUED,
    )

    yield task_run

    # NOTE: TaskRun does not get deleted


@pytest.fixture
def task_context(test_task, test_task_run) -> TaskProcessingContext:
    """Create a TaskProcessingContext for testing."""
    return TaskProcessingContext(
        task_id=str(test_task.id),
        run_id=str(test_task_run.id),
        team_id=test_task.team_id,
        team_uuid=str(test_task.team.uuid),
        organization_id=str(test_task.team.organization_id),
        github_integration_id=test_task.github_integration_id,
        repository=test_task.repository,
        distinct_id=test_task.created_by.distinct_id or "test-distinct-id",
        task_created_by_id=test_task.created_by_id,
    )


@pytest.fixture
def snapshot_context(github_integration, team) -> SnapshotContext:
    """Create a SnapshotContext for testing."""
    return SnapshotContext(
        github_integration_id=github_integration.id,
        repository="posthog/posthog-js",
        team_id=team.id,
    )


@pytest.fixture(autouse=True)
def configure_logger_auto() -> Iterator[None]:
    """Configure logger when running in a Temporal activity environment."""
    with restored_structlog_config():
        configure_logger(cache_logger_on_first_use=False)
        yield


def get_or_create_test_snapshots(github_integration):
    """Idempotently create or retrieve real snapshots for test repositories.

    Returns a dict with keys:
    - "single": snapshot with just posthog/posthog-js
    - "multi": snapshot with both posthog/posthog-js and posthog/posthog.com
    """
    if not os.environ.get("MODAL_TOKEN_ID") or not os.environ.get("MODAL_TOKEN_SECRET"):
        pytest.skip("MODAL_TOKEN_ID and MODAL_TOKEN_SECRET environment variables not set")

    existing_single = SandboxSnapshot.objects.filter(
        integration=github_integration,
        repos=["posthog/posthog-js"],
        status=SandboxSnapshot.Status.COMPLETE,
    ).first()

    existing_multi = SandboxSnapshot.objects.filter(
        integration=github_integration,
        repos__contains=["posthog/posthog-js", "posthog/posthog.com"],
        status=SandboxSnapshot.Status.COMPLETE,
    ).first()

    if existing_single and existing_multi:
        return {"single": existing_single, "multi": existing_multi}

    snapshots = {}
    sandbox = None

    try:
        config = SandboxConfig(
            name=f"test-snapshot-creator-{random.randint(1, 99999)}",
            template=SandboxTemplate.DEFAULT_BASE,
        )

        sandbox = Sandbox.create(config)

        if not existing_single:
            clone_result = sandbox.clone_repository("posthog/posthog-js", github_token="")
            if clone_result.exit_code == 0:
                single_snapshot_id = sandbox.create_snapshot()
                single_snapshot = SandboxSnapshot.objects.create(
                    integration=github_integration,
                    repos=["posthog/posthog-js"],
                    external_id=single_snapshot_id,
                    status=SandboxSnapshot.Status.COMPLETE,
                )
                snapshots["single"] = single_snapshot
        else:
            snapshots["single"] = existing_single

        if not existing_multi:
            clone_result = sandbox.clone_repository("posthog/posthog.com", github_token="")
            if clone_result.exit_code == 0:
                multi_snapshot_id = sandbox.create_snapshot()
                multi_snapshot = SandboxSnapshot.objects.create(
                    integration=github_integration,
                    repos=["posthog/posthog-js", "posthog/posthog.com"],
                    external_id=multi_snapshot_id,
                    status=SandboxSnapshot.Status.COMPLETE,
                )
                snapshots["multi"] = multi_snapshot
        else:
            snapshots["multi"] = existing_multi

        return snapshots

    finally:
        if sandbox:
            sandbox.destroy()