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(frontend): implement project-based routing #13474

Merged
merged 52 commits into from
Jan 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
864b814
feat routs
mariusandra Dec 23, 2022
a4e24d9
transform path in actions
mariusandra Dec 23, 2022
23ed95e
pnpm
mariusandra Apr 3, 2023
17fab78
exclude /me, /instance, /organization
mariusandra Apr 3, 2023
404f4de
make it all work better
mariusandra Apr 3, 2023
68f02af
Update UI snapshots for `chromium` (2)
github-actions[bot] Apr 3, 2023
a02fe3b
Update UI snapshots for `webkit` (2)
github-actions[bot] Apr 3, 2023
2417cbc
Update UI snapshots for `chromium` (2)
github-actions[bot] Apr 3, 2023
3c4bf81
Update UI snapshots for `webkit` (2)
github-actions[bot] Apr 3, 2023
d2dc7a7
Update UI snapshots for `webkit` (2)
github-actions[bot] Apr 3, 2023
a4d8d8e
Update UI snapshots for `webkit` (2)
github-actions[bot] Apr 3, 2023
2b28588
Merge branch 'master' into kea-router-projects
mariusandra Jun 19, 2023
3ba1d0f
Merge branch 'master' into kea-router-projects
mariusandra Aug 7, 2023
e13c5aa
pnpm
mariusandra Aug 7, 2023
b93fd09
frontend updates
mariusandra Aug 7, 2023
5efac89
update team from url
mariusandra Aug 7, 2023
350a8bd
fixes
mariusandra Aug 7, 2023
8bc52cd
tests
mariusandra Aug 7, 2023
e77d213
mypy
mariusandra Aug 7, 2023
8608566
Update UI snapshots for `chromium` (2)
github-actions[bot] Aug 7, 2023
991f1eb
Merge branch 'master' into kea-router-projects
mariusandra Aug 7, 2023
1eacf51
Merge branch 'kea-router-projects' of github.com:PostHog/posthog into…
mariusandra Aug 7, 2023
802bf0d
Update UI snapshots for `chromium` (2)
github-actions[bot] Aug 7, 2023
9729550
Merge master
benjackwhite Jan 3, 2024
f11a0fd
Update UI snapshots for `chromium` (1)
github-actions[bot] Jan 3, 2024
2333cbb
Fixed api routes
benjackwhite Jan 4, 2024
82c5288
Fixes
benjackwhite Jan 4, 2024
ce321f4
Update query snapshots
github-actions[bot] Jan 4, 2024
4fd446e
Update query snapshots
github-actions[bot] Jan 4, 2024
809599f
Fixed some more issues around switching back
benjackwhite Jan 4, 2024
ad14a5f
Update query snapshots
github-actions[bot] Jan 4, 2024
4bb7b52
Simplify path checker
benjackwhite Jan 4, 2024
34f84b6
Merge branch 'kea-router-projects' of github.com:PostHog/posthog into…
benjackwhite Jan 4, 2024
508e446
Update query snapshots
github-actions[bot] Jan 4, 2024
7de8f8b
Fix
benjackwhite Jan 4, 2024
9b011a8
Moved to utils
benjackwhite Jan 4, 2024
8f50e4f
Fix issues with circular imports
benjackwhite Jan 4, 2024
c73aba9
Fix test
benjackwhite Jan 4, 2024
5bf7460
More fixes
benjackwhite Jan 4, 2024
11e0ffa
Fix
benjackwhite Jan 4, 2024
7cd6cf1
Update UI snapshots for `chromium` (1)
github-actions[bot] Jan 4, 2024
b2d9d58
Fix project switcher url
benjackwhite Jan 4, 2024
d527946
Merge branch 'kea-router-projects' of github.com:PostHog/posthog into…
benjackwhite Jan 4, 2024
4e5e53c
Update UI snapshots for `chromium` (1)
github-actions[bot] Jan 4, 2024
dd6d5c7
Merge branch 'kea-router-projects' of github.com:PostHog/posthog into…
benjackwhite Jan 4, 2024
ddbba41
Fix tests
benjackwhite Jan 4, 2024
decc3ac
Fixes
benjackwhite Jan 4, 2024
22a30a5
Fix
benjackwhite Jan 4, 2024
05cbfaf
Update query snapshots
github-actions[bot] Jan 4, 2024
516633f
Update query snapshots
github-actions[bot] Jan 4, 2024
56129a0
Fixed middleware for missing team
benjackwhite Jan 8, 2024
4534531
Merge branch 'master' into kea-router-projects
benjackwhite Jan 15, 2024
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
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
benjackwhite committed Jan 4, 2024
commit ddbba41b385c06836a38f40e060ee7ff2381fee2
2 changes: 1 addition & 1 deletion posthog/middleware.py
Original file line number Diff line number Diff line change
@@ -213,7 +213,7 @@ def switch_team_if_needed_and_allowed(self, request: HttpRequest, target_queryse
if current_team is not None and not target_queryset.filter(team=current_team).exists():
actual_item = target_queryset.only("team").select_related("team").first()
if actual_item is not None:
self.switch_team(actual_item.team, request)
self.switch_team_if_allowed(actual_item.team, request)

def switch_team_if_allowed(self, new_team: Team, request: HttpRequest):
user = cast(User, request.user)
46 changes: 22 additions & 24 deletions posthog/test/test_middleware.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@

from django.test.client import Client
from rest_framework import status
from posthog.api.test.test_organization import create_organization
from posthog.api.test.test_team import create_team

from posthog.models import Action, Cohort, Dashboard, FeatureFlag, Insight
from posthog.models.organization import Organization
@@ -111,14 +113,17 @@ class TestAutoProjectMiddleware(APIBaseTest):
# How many queries are made in the base app
# On Cloud there's an additional multi_tenancy_organizationbilling query
second_team: Team
no_access_team: Team
base_app_num_queries: int

@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.base_app_num_queries = 41
# Create another team that the user does have access to
cls.second_team = Team.objects.create(organization=cls.organization, name="Second Life")
cls.second_team = create_team(organization=cls.organization, name="Second Life")
other_org = create_organization(name="test org")
cls.no_access_team = create_team(organization=other_org)

def setUp(self):
super().setUp()
@@ -272,34 +277,27 @@ def test_project_unchanged_when_creating_feature_flag(self):
self.assertEqual(response_users_api.status_code, 200)
self.assertEqual(response_users_api_data.get("team", {}).get("id"), self.team.id)

def test_project_switched_when_accessing_another_project(self):
action = Action.objects.create(team=self.second_team)

response_app = self.client.get(f"/project/{self.second_team.pk}/home")
def test_project_switched_when_accessing_another_project_by_id(self):
project_1_request = self.client.get(f"/project/{self.team.pk}/home")
response_users_api = self.client.get(f"/api/users/@me/")
response_users_api_data = response_users_api.json()
self.user.refresh_from_db()
response_actions_api = self.client.get(f"/api/projects/@current/actions/{action.id}/")
assert project_1_request.status_code == 200
assert response_users_api.json().get("team", {}).get("id") == self.team.id

self.assertEqual(response_app.status_code, 200)
self.assertEqual(response_users_api.status_code, 200)
self.assertEqual(response_users_api_data.get("team", {}).get("id"), self.second_team.id)
self.assertEqual(response_actions_api.status_code, 200)
project_2_request = self.client.get(f"/project/{self.second_team.pk}/home")
response_users_api = self.client.get(f"/api/users/@me/")
assert project_2_request.status_code == 200
assert response_users_api.json().get("team", {}).get("id") == self.second_team.id

def test_project_not_switched_when_accessing_action_from_other_project_with_project_url(self):
action = Action.objects.create(team=self.second_team)
# action from a different team than self.team
response_app = self.client.get(f"/project/{self.team.pk}/actions/{action.id}")
def test_project_unchanged_when_accessing_inaccessible_project_by_id(self):
project_1_request = self.client.get(f"/project/{self.team.pk}/home")
response_users_api = self.client.get(f"/api/users/@me/")
response_users_api_data = response_users_api.json()
self.user.refresh_from_db()
# does not switch to the team from the action, uses the team in the URL
response_actions_api = self.client.get(f"/api/projects/@current/actions/{action.id}/")
assert project_1_request.status_code == 200
assert response_users_api.json().get("team", {}).get("id") == self.team.id

self.assertEqual(response_app.status_code, 200)
self.assertEqual(response_users_api.status_code, 200)
self.assertEqual(response_users_api_data.get("team", {}).get("id"), self.team.id)
self.assertEqual(response_actions_api.status_code, 404)
project_2_request = self.client.get(f"/project/{self.no_access_team.pk}/home")
response_users_api = self.client.get(f"/api/users/@me/")
assert project_2_request.status_code == 200
assert response_users_api.json().get("team", {}).get("id") == self.team.id


class TestPostHogTokenCookieMiddleware(APIBaseTest):
Loading