: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 posthog.test.base import FuzzyInt
from posthog.api.test.test_team import EnvironmentToProjectRewriteClient
from posthog.constants import AvailableFeature
from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.project import Project
from posthog.models.team.team import Team
from posthog.models.user import User
from products.access_control.backend.models.access_control import AccessControl
from ee.api.test.test_team import team_enterprise_api_test_factory
class TestProjectEnterpriseAPI(team_enterprise_api_test_factory()): # type: ignore[misc]
"""
We inherit from TestTeamEnterpriseAPI, as previously /api/projects/ referred to the Team model, which used to mean "project".
Now as Team means "environment" and Project is separate, we must ensure backward compatibility of /api/projects/.
At the same time, this class is where we can continue adding `Project`-specific API tests.
"""
client_class = EnvironmentToProjectRewriteClient
def test_create_team(self):
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()
# Ensure project-creation limits don't block this test
self.organization.available_product_features = [
{
"key": AvailableFeature.ORGANIZATIONS_PROJECTS,
"name": AvailableFeature.ORGANIZATIONS_PROJECTS,
"limit": 10,
}
]
self.organization.save()
self.assertEqual(Team.objects.count(), 1)
self.assertEqual(Project.objects.count(), 1)
response = self.client.post("/api/projects/@current/environments/", {"name": "Test"})
self.assertEqual(response.status_code, 201)
self.assertEqual(Team.objects.count(), 2)
self.assertEqual(Project.objects.count(), 2)
response_data = response.json()
self.assertLessEqual(
{
"name": "Test",
"access_control": False,
"effective_membership_level": OrganizationMembership.Level.ADMIN,
}.items(),
response_data.items(),
)
self.assertEqual(self.organization.teams.count(), 2)
def test_user_create_project_for_org_via_url(self):
# Set both current and new org to high enough membership level
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()
current_org, _, _ = Organization.objects.bootstrap(self.user, name="other_org")
other_org = self.organization # Bootstrapping above sets it to the current org
# Ensure project-creation limits don't block this test for both orgs we create under
for org in (current_org, other_org):
org.available_product_features = [
{
"key": AvailableFeature.ORGANIZATIONS_PROJECTS,
"name": AvailableFeature.ORGANIZATIONS_PROJECTS,
"limit": 10,
}
]
org.save()
assert Team.objects.count() == 2
assert Project.objects.count() == 2
assert current_org.id == self.user.current_organization_id
response = self.client.post(f"/api/organizations/{current_org.id}/projects/", {"name": "Via current org"})
self.assertEqual(response.status_code, 201)
assert response.json()["organization"] == str(current_org.id)
assert Team.objects.count() == 3
assert Project.objects.count() == 3
assert other_org.id != self.user.current_organization_id
response = self.client.post(f"/api/organizations/{other_org.id}/projects/", {"name": "Via path org"})
self.assertEqual(response.status_code, 201, msg=response.json())
assert response.json()["organization"] == str(other_org.id)
assert Team.objects.count() == 4
assert Project.objects.count() == 4
def test_user_cannot_create_project_in_org_without_access(self):
_, _, _ = Organization.objects.bootstrap(self.user, name="other_org")
other_org = self.organization # Bootstrapping above sets it to the current org
# Ensure project-creation limits don't block this test (we want to assert on access control)
other_org.available_product_features = [
{
"key": AvailableFeature.ORGANIZATIONS_PROJECTS,
"name": AvailableFeature.ORGANIZATIONS_PROJECTS,
"limit": 10,
}
]
other_org.save()
assert other_org.id != self.user.current_organization_id
response = self.client.post(f"/api/organizations/{other_org.id}/projects/", {"name": "Via path org"})
self.assertEqual(response.status_code, 403, msg=response.json())
assert response.json() == self.permission_denied_response(
"You need to be an organization admin or above to create new projects."
)
def test_user_that_does_not_belong_to_an_org_cannot_create_a_projec(self):
user = User.objects.create(email="[email protected]")
self.client.force_login(user)
response = self.client.post("/api/projects/", {"name": "Test"})
self.assertEqual(response.status_code, 404, response.content)
self.assertEqual(
response.json(),
{
"type": "invalid_request",
"code": "not_found",
"detail": "You need to belong to an organization.",
"attr": None,
},
)
def test_rename_project_as_org_member_forbidden(self):
# Renaming is admin-only (mirrors the settings UI, which gates rename behind admin access).
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
# In an access-control org, a member with no explicit project access defaults to effective admin;
# set default member access so the admin-only rename gate actually applies.
self._set_project_default_member_access(self.team)
response = self.client.patch(f"/api/projects/@current/", {"name": "Erinaceus europaeus"})
self.project.refresh_from_db()
self.assertEqual(response.status_code, 403)
self.assertNotEqual(self.project.name, "Erinaceus europaeus")
def test_list_projects_restricted_ones_hidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
other_team = Team.objects.create(
organization=self.organization,
name="Other",
)
# Set up new access control system - restrict project to no default access
AccessControl.objects.create(
team=other_team,
access_level="none",
resource="project",
resource_id=str(other_team.id),
)
# The other team should not be returned as it's restricted for the logged-in user
projects_response = self.client.get(f"/api/environments/")
# 9 (above):
with self.assertNumQueries(FuzzyInt(14, 17)):
current_org_response = self.client.get(f"/api/organizations/{self.organization.id}/")
self.assertEqual(projects_response.status_code, 200)
self.assertEqual(
projects_response.json().get("results"),
[
{
"id": self.team.id,
"uuid": str(self.team.uuid),
"organization": str(self.organization.id),
"project_id": self.team.project.id,
"api_token": self.team.api_token,
"name": self.team.name,
"completed_snippet_onboarding": False,
"has_completed_onboarding_for": {"product_analytics": True},
"ingested_event": False,
"is_demo": False,
"timezone": "UTC",
"access_control": False,
"tags": [],
}
],
)
self.assertEqual(current_org_response.status_code, 200)
self.assertEqual(
current_org_response.json().get("teams"),
[
{
"id": self.team.id,
"uuid": str(self.team.uuid),
"organization": str(self.organization.id),
"project_id": self.team.project.id,
"api_token": self.team.api_token,
"name": self.team.name,
"completed_snippet_onboarding": False,
"has_completed_onboarding_for": {"product_analytics": True},
"ingested_event": False,
"is_demo": False,
"timezone": "UTC",
"access_control": False,
"project_group": None,
}
],
)
def test_cannot_create_project_in_org_without_access(self):
self.organization_membership.delete()
# Ensure project-creation limits don't block this test (we want to assert on access control)
self.organization.available_product_features = [
{
"key": AvailableFeature.ORGANIZATIONS_PROJECTS,
"name": AvailableFeature.ORGANIZATIONS_PROJECTS,
"limit": 10,
}
]
self.organization.save()
response = self.client.post(f"/api/organizations/{self.organization.id}/projects/", {"name": "Test"})
self.assertEqual(response.status_code, 404, response.json())
self.assertEqual(response.json(), self.not_found_response("Organization not found."))
def test_team_update_is_in_activity_log(self):
# Skip: This test uses /api/environments/{id}/activity which gets rewritten to
# /api/projects/{id}/activity by EnvironmentToProjectRewriteClient. The Project
# activity log is separate from Team activity log, so the test doesn't apply here.
pass