Skip to content

Commit

Permalink
debugging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhi-posthog committed Dec 23, 2024
1 parent f62b5ed commit 0785c04
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
19 changes: 1 addition & 18 deletions 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, groups
from posthog.event_usage import report_user_action
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,7 +52,6 @@
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 @@ -374,22 +373,6 @@ 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
33 changes: 14 additions & 19 deletions posthog/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,38 +1226,33 @@ def _patch_linked_flag_config(

@patch("posthoganalytics.capture")
def test_access_control_toggle_capture(self, mock_capture):
# Get current access control setting

self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()

# Reset the mock to clear any setup calls
mock_capture.reset_mock()

response = self.client.patch(f"/api/environments/@current/", {"access_control": True})
response = self.client.get("/api/environments/@current/")
assert response.status_code == status.HTTP_200_OK
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)

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),
)
response_data = response.json()

# 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)
self.team.refresh_from_db()
self.assertEqual(response_data["access_control"], new_setting)

mock_capture.assert_called_with(
str(self.user.distinct_id),
"project access control toggled",
properties={
"enabled": False,
"enabled": new_setting,
"project_id": str(self.team.id),
"project_name": self.team.name,
"organization_id": str(self.organization.id),
Expand Down

0 comments on commit 0785c04

Please sign in to comment.