: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
from typing import get_args
from posthog.test.base import BaseTest
from django.test import SimpleTestCase
from parameterized import parameterized
from posthog.models import Organization, OrganizationMembership, User
from products.security.backend.logic.accounts import (
COUNT_CAP,
CountableType,
count_active_accounts,
count_active_org_members,
email_for_user,
has_posthog_account,
resolve_subject,
)
from products.security.backend.presentation.serializers import CountableTargetType
class TestCountableTargetTypes(SimpleTestCase):
def test_serializer_accepts_every_countable_type(self) -> None:
assert set(CountableTargetType.values) == set(get_args(CountableType))
class TestAccounts(BaseTest):
def _user(self, email: str, org: Organization | None = None, active: bool = True) -> User:
user = User.objects.create_and_join(org or self.organization, email, "password1234", is_active=active)
return user
def test_resolve_email_user_and_orgs(self) -> None:
user = self._user("[email protected]")
resolved = resolve_subject("[email protected]")
assert resolved.kind == "user"
assert resolved.user is not None and resolved.user.uuid == str(user.uuid)
assert resolved.organization_ids == (str(self.organization.id),)
@parameterized.expand([("[email protected]",), ("12345",), ("' OR 1=1 --",), ("",)])
def test_resolve_misses(self, query: str) -> None:
assert resolve_subject(query).kind == "none"
def test_resolve_uuid_user_then_organization(self) -> None:
user = self._user("[email protected]")
assert resolve_subject(str(user.uuid).upper()).kind == "user"
org_resolution = resolve_subject(str(self.organization.id))
assert org_resolution.kind == "organization"
assert org_resolution.organization_ids == (str(self.organization.id),)
def test_counts(self) -> None:
self._user("[email protected]")
self._user("[email protected]")
self._user("[email protected]", active=False)
self._user("[email protected]")
self._user("y@sub\u3002throwaway\uff0eexample")
self._user("z@throw\u200baway.example")
self._user("[email protected]")
self._user("farm\u200bbot@example\u3002com")
self._user("farm\uff0e[email protected]")
self._user("d\u3002o.t@gmail\uff0ecom")
self._user("[email protected]")
assert count_active_accounts("email_root", "[email protected]").count == 2
assert count_active_accounts("email", "[email protected]").count == 2
assert count_active_accounts("email_domain", "throwaway.example").count == 3
assert count_active_accounts("email_root", "[email protected]").count == 2
assert count_active_accounts("email_pattern", "farm.bot+*@example.com").count == 2
assert count_active_accounts("email_pattern", "far.*@*").count == 0
assert count_active_accounts("email_pattern", "*@*").count == 0
def test_counts_are_capped(self) -> None:
User.objects.bulk_create(
[
User(email=f"bulk{i}@capped.example", is_active=True, distinct_id=f"capped-{i}")
for i in range(COUNT_CAP + 2)
]
)
result = count_active_accounts("email_domain", "capped.example")
assert (result.count, result.capped) == (COUNT_CAP, True)
def test_org_member_count_and_posthog_membership(self) -> None:
other = Organization.objects.create(name="Bystander")
self._user("[email protected]", org=other)
exists, members = count_active_org_members(str(other.id))
assert exists and members.count == 1
assert count_active_org_members("9999aaaa-9999-4999-8999-99999999aaaa")[0] is False
assert has_posthog_account(organization_id=str(other.id)) is False
staff = self._user("[email protected]", org=other)
assert has_posthog_account(organization_id=str(other.id)) is True
assert has_posthog_account(user_uuid=str(staff.uuid)) is True
assert email_for_user(str(staff.uuid)) == "[email protected]"
assert email_for_user("9999aaaa-9999-4999-8999-99999999aaaa") is None
assert OrganizationMembership.objects.filter(organization=other).count() == 2