2022-03-09 18:46:21 +01:00
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from django.utils import timezone
|
2022-02-14 13:55:16 +01:00
|
|
|
from rest_framework import status
|
|
|
|
|
|
|
|
from ee.api.test.base import APILicensedTest
|
2022-04-22 10:03:22 +02:00
|
|
|
from posthog.models import Dashboard, DashboardTile, Insight, OrganizationMembership, User
|
2022-02-14 13:55:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestInsightEnterpriseAPI(APILicensedTest):
|
|
|
|
def test_cannot_update_restricted_insight_as_other_user_who_is_project_member(self):
|
|
|
|
creator = User.objects.create_and_join(self.organization, "y@x.com", None)
|
|
|
|
self.organization_membership.level = OrganizationMembership.Level.MEMBER
|
|
|
|
self.organization_membership.save()
|
|
|
|
original_name = "Edit-restricted dashboard"
|
|
|
|
dashboard: Dashboard = Dashboard.objects.create(
|
|
|
|
team=self.team,
|
|
|
|
name=original_name,
|
|
|
|
created_by=creator,
|
|
|
|
restriction_level=Dashboard.RestrictionLevel.ONLY_COLLABORATORS_CAN_EDIT,
|
|
|
|
)
|
2022-04-22 10:03:22 +02:00
|
|
|
insight: Insight = Insight.objects.create(team=self.team, name="XYZ", created_by=self.user)
|
|
|
|
DashboardTile.objects.create(dashboard=dashboard, insight=insight)
|
2022-02-14 13:55:16 +01:00
|
|
|
|
|
|
|
response = self.client.patch(f"/api/projects/{self.team.id}/insights/{insight.id}", {"name": "ABC"})
|
|
|
|
response_data = response.json()
|
|
|
|
dashboard.refresh_from_db()
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
|
|
|
self.assertEquals(
|
|
|
|
response_data,
|
|
|
|
self.permission_denied_response(
|
|
|
|
"This insight is on a dashboard that can only be edited by its owner, team members invited to editing the dashboard, and project admins."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(dashboard.name, original_name)
|
|
|
|
|
2022-03-09 18:46:21 +01:00
|
|
|
def test_event_definition_no_duplicate_tags(self):
|
|
|
|
from ee.models.license import License, LicenseManager
|
|
|
|
|
|
|
|
super(LicenseManager, cast(LicenseManager, License.objects)).create(
|
|
|
|
key="key_123", plan="enterprise", valid_until=timezone.datetime(2038, 1, 19, 3, 14, 7), max_users=3,
|
|
|
|
)
|
|
|
|
dashboard = Dashboard.objects.create(team=self.team, name="Edit-restricted dashboard",)
|
2022-04-22 10:03:22 +02:00
|
|
|
insight = Insight.objects.create(team=self.team, name="XYZ", created_by=self.user)
|
|
|
|
DashboardTile.objects.create(dashboard=dashboard, insight=insight)
|
2022-03-09 18:46:21 +01:00
|
|
|
|
|
|
|
response = self.client.patch(f"/api/projects/{self.team.id}/insights/{insight.id}", {"tags": ["a", "b", "a"]})
|
|
|
|
|
|
|
|
self.assertListEqual(sorted(response.json()["tags"]), ["a", "b"])
|