0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/ee/api/test/test_insight.py

51 lines
2.4 KiB
Python
Raw Normal View History

from typing import cast
from django.utils import timezone
from rest_framework import status
from ee.api.test.base import APILicensedTest
feat(insights): dashboards insights many-to-many another attempt (#9416) * dashboards and tiles, view mode, basic frontend typing * update types, many-to-many relations * update insight dashboards * duplication works * mypy * fix migrations * fix duplication bug * add default filters * fix caching * deprecation nonsense * remove things * add nplus1 tests * make nplus1 test need no explanation * don't need to prefetch and select related * move layouts API onto dashboard and store updates on DashboardTile * apply dashboard filters to insights when loading dashboard * don't need to update cached result in a cached function * ugh, filter caching * update basic serializer test * add defaults when getting layout and colour from insights * caching insights depends on filters hash changing when the insight is saved * caching insights depends on filters hash changing when the insight is saved * can't constantly resave insight filter hashes any more * prettify the skip comment * fix patch reference * fix test assertion * need to save dashboards to update insight filters_hash *until we fix filtering* * add comment explaining why the test is failing - so I remember later * blunt solution to not overwriting dashboard aware filter hash * explode by 7 instead of 9 queries per insight added to dashboard * mark some learning as a TODO * only update dashboard layout when in edit mode * insight logic no longer deals with saving layout * skip the nplus1 test _for now_ * update the dashboard snapshots * enrich insights with extra props when loaded in dashboard context * update color via dashboard not insight * create empty insight with a dashboards array, not a single dashboard * remove empty TODO * avoid initialising dashboardsmodel for dashboards logic tests * no need to select and prefetch related * order migration by last modified at * remove insight layouts endpoint * Add new from_dashboard parameter to API docs * typo * move deprecated fields on insight * fix syntax error in migration file * add permissions tests * add help text to dashboards serializer field * fix test with silent server errors * down to 3 extra queries per insight on each dasboard * down to 1 extra queries per insight on each dashboard * set dashboard on context when retrieving an insight, so that filters are reported correctly * explicitly fail to add insight to another team's dashboard * update insight cache in correct context when refreshing * add migration test * satisfy the type checker * Revert "satisfy the type checker" This reverts commit 4d48a16cf4a33886a8928fe6c5e395c2913dc56c. * satisfy the type checker * really satisfy the type checker * address two nits * can ignore conflicts when inserting dashboard tiles Co-authored-by: Paul D'Ambra <paul.dambra@gmail.com>
2022-04-22 10:03:22 +02:00
from posthog.models import Dashboard, DashboardTile, Insight, OrganizationMembership, User
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,
)
feat(insights): dashboards insights many-to-many another attempt (#9416) * dashboards and tiles, view mode, basic frontend typing * update types, many-to-many relations * update insight dashboards * duplication works * mypy * fix migrations * fix duplication bug * add default filters * fix caching * deprecation nonsense * remove things * add nplus1 tests * make nplus1 test need no explanation * don't need to prefetch and select related * move layouts API onto dashboard and store updates on DashboardTile * apply dashboard filters to insights when loading dashboard * don't need to update cached result in a cached function * ugh, filter caching * update basic serializer test * add defaults when getting layout and colour from insights * caching insights depends on filters hash changing when the insight is saved * caching insights depends on filters hash changing when the insight is saved * can't constantly resave insight filter hashes any more * prettify the skip comment * fix patch reference * fix test assertion * need to save dashboards to update insight filters_hash *until we fix filtering* * add comment explaining why the test is failing - so I remember later * blunt solution to not overwriting dashboard aware filter hash * explode by 7 instead of 9 queries per insight added to dashboard * mark some learning as a TODO * only update dashboard layout when in edit mode * insight logic no longer deals with saving layout * skip the nplus1 test _for now_ * update the dashboard snapshots * enrich insights with extra props when loaded in dashboard context * update color via dashboard not insight * create empty insight with a dashboards array, not a single dashboard * remove empty TODO * avoid initialising dashboardsmodel for dashboards logic tests * no need to select and prefetch related * order migration by last modified at * remove insight layouts endpoint * Add new from_dashboard parameter to API docs * typo * move deprecated fields on insight * fix syntax error in migration file * add permissions tests * add help text to dashboards serializer field * fix test with silent server errors * down to 3 extra queries per insight on each dasboard * down to 1 extra queries per insight on each dashboard * set dashboard on context when retrieving an insight, so that filters are reported correctly * explicitly fail to add insight to another team's dashboard * update insight cache in correct context when refreshing * add migration test * satisfy the type checker * Revert "satisfy the type checker" This reverts commit 4d48a16cf4a33886a8928fe6c5e395c2913dc56c. * satisfy the type checker * really satisfy the type checker * address two nits * can ignore conflicts when inserting dashboard tiles Co-authored-by: Paul D'Ambra <paul.dambra@gmail.com>
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)
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)
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",)
feat(insights): dashboards insights many-to-many another attempt (#9416) * dashboards and tiles, view mode, basic frontend typing * update types, many-to-many relations * update insight dashboards * duplication works * mypy * fix migrations * fix duplication bug * add default filters * fix caching * deprecation nonsense * remove things * add nplus1 tests * make nplus1 test need no explanation * don't need to prefetch and select related * move layouts API onto dashboard and store updates on DashboardTile * apply dashboard filters to insights when loading dashboard * don't need to update cached result in a cached function * ugh, filter caching * update basic serializer test * add defaults when getting layout and colour from insights * caching insights depends on filters hash changing when the insight is saved * caching insights depends on filters hash changing when the insight is saved * can't constantly resave insight filter hashes any more * prettify the skip comment * fix patch reference * fix test assertion * need to save dashboards to update insight filters_hash *until we fix filtering* * add comment explaining why the test is failing - so I remember later * blunt solution to not overwriting dashboard aware filter hash * explode by 7 instead of 9 queries per insight added to dashboard * mark some learning as a TODO * only update dashboard layout when in edit mode * insight logic no longer deals with saving layout * skip the nplus1 test _for now_ * update the dashboard snapshots * enrich insights with extra props when loaded in dashboard context * update color via dashboard not insight * create empty insight with a dashboards array, not a single dashboard * remove empty TODO * avoid initialising dashboardsmodel for dashboards logic tests * no need to select and prefetch related * order migration by last modified at * remove insight layouts endpoint * Add new from_dashboard parameter to API docs * typo * move deprecated fields on insight * fix syntax error in migration file * add permissions tests * add help text to dashboards serializer field * fix test with silent server errors * down to 3 extra queries per insight on each dasboard * down to 1 extra queries per insight on each dashboard * set dashboard on context when retrieving an insight, so that filters are reported correctly * explicitly fail to add insight to another team's dashboard * update insight cache in correct context when refreshing * add migration test * satisfy the type checker * Revert "satisfy the type checker" This reverts commit 4d48a16cf4a33886a8928fe6c5e395c2913dc56c. * satisfy the type checker * really satisfy the type checker * address two nits * can ignore conflicts when inserting dashboard tiles Co-authored-by: Paul D'Ambra <paul.dambra@gmail.com>
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)
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"])