Skip to content

Commit

Permalink
access control capture and test
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhi-posthog committed Dec 18, 2024
1 parent fc19b77 commit f62b5ed
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
19 changes: 18 additions & 1 deletion posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from posthog.api.utils import action
from posthog.auth import PersonalAPIKeyAuthentication
from posthog.constants import AvailableFeature
from posthog.event_usage import report_user_action
from posthog.event_usage import report_user_action, groups
from posthog.geoip import get_geoip_properties
from posthog.jwt import PosthogJwtAudience, encode_jwt
from posthog.models import ProductIntent, Team, User
Expand Down Expand Up @@ -52,6 +52,7 @@
get_ip_address,
get_week_start_for_country_code,
)
import posthoganalytics


class PremiumMultiProjectPermissions(BasePermission): # TODO: Rename to include "Env" in name
Expand Down Expand Up @@ -373,6 +374,22 @@ def create(self, validated_data: dict[str, Any], **kwargs) -> Team:
def update(self, instance: Team, validated_data: dict[str, Any]) -> Team:
before_update = instance.__dict__.copy()

if "access_control" in validated_data and validated_data["access_control"] != instance.access_control:
user = cast(User, self.context["request"].user)
posthoganalytics.capture(
str(user.distinct_id),
"project access control toggled",
properties={
"enabled": validated_data["access_control"],
"project_id": str(instance.id),
"project_name": instance.name,
"organization_id": str(instance.organization_id),
"organization_name": instance.organization.name,
"user_role": user.organization_memberships.get(organization=instance.organization).level,
},
groups=groups(instance.organization),
)

if "survey_config" in validated_data:
if instance.survey_config is not None and validated_data.get("survey_config") is not None:
validated_data["survey_config"] = {
Expand Down
44 changes: 44 additions & 0 deletions posthog/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from posthog.temporal.common.schedule import describe_schedule
from posthog.test.base import APIBaseTest
from posthog.utils import get_instance_realm
from posthog.event_usage import groups


def team_api_test_factory():
Expand Down Expand Up @@ -1223,6 +1224,49 @@ def _patch_linked_flag_config(
assert response.status_code == expected_status, response.json()
return response

@patch("posthoganalytics.capture")
def test_access_control_toggle_capture(self, mock_capture):
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()

mock_capture.reset_mock()

response = self.client.patch(f"/api/environments/@current/", {"access_control": True})
self.assertEqual(response.status_code, status.HTTP_200_OK)

mock_capture.assert_called_with(
str(self.user.distinct_id),
"project access control toggled",
properties={
"enabled": True,
"project_id": str(self.team.id),
"project_name": self.team.name,
"organization_id": str(self.organization.id),
"organization_name": self.organization.name,
"user_role": OrganizationMembership.Level.ADMIN,
},
groups=groups(self.organization),
)

# Test toggling back to false
mock_capture.reset_mock()
response = self.client.patch(f"/api/environments/@current/", {"access_control": False})
self.assertEqual(response.status_code, status.HTTP_200_OK)

mock_capture.assert_called_with(
str(self.user.distinct_id),
"project access control toggled",
properties={
"enabled": False,
"project_id": str(self.team.id),
"project_name": self.team.name,
"organization_id": str(self.organization.id),
"organization_name": self.organization.name,
"user_role": OrganizationMembership.Level.ADMIN,
},
groups=groups(self.organization),
)

return TestTeamAPI


Expand Down

0 comments on commit f62b5ed

Please sign in to comment.