Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [2/2] Tests For Access Control Capture #27142

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion posthog/api/team.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import posthoganalytics

from datetime import UTC, datetime, timedelta
from functools import cached_property
from typing import Any, Optional, cast
Expand All @@ -14,7 +16,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 @@ -373,6 +375,25 @@ 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()

print("access_control check", validated_data) # noqa: T201
if "access_control" in validated_data and validated_data["access_control"] != instance.access_control:
print("in access control toggle") # noqa: T201
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),
)
print("after access control toggle") # noqa: T201

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
36 changes: 36 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,41 @@ 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.get("/api/environments/@current/")
assert response.status_code == status.HTTP_200_OK

current_access_control = response.json()["access_control"]
new_setting = not current_access_control
# response = self.client.patch(f"/api/environments/@current/", {"access_control": new_setting})
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# print("response", response.json()) # noqa: T201

with patch("posthog.api.team.TeamSerializer.update") as mock_update:
response = self.client.patch(f"/api/environments/@current/", {"access_control": new_setting})
print("Update method called:", mock_update.called) # noqa: T201
print("PATCH response:", response.json()) # noqa: T201
print("Mock update calls:", mock_update.call_args_list if mock_update.called else "No calls") # noqa: T201

mock_capture.assert_called_with(
str(self.user.distinct_id),
"project access control toggled",
properties={
"enabled": new_setting,
"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
Loading