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(environments): Add new /environments endpoint and move /projects to Project #24154

Merged
merged 37 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e3db08a
Refactor away legacy "Clickhouse" viewset prefix
Twixes Jul 12, 2024
453b02d
Disambiguate API endpoints basenames
Twixes Jul 12, 2024
7291d98
Remove router `_register` override hack
Twixes Jul 12, 2024
64719f8
Add `register_grandfathered_environment_nested_viewset`
Twixes Jul 15, 2024
2f63d47
Add new ProjectViewSet
Twixes Aug 1, 2024
81815d7
Simplify `TeamMemberLightManagementPermission`
Twixes Aug 1, 2024
9bab8b0
Add current project to app context
Twixes Aug 1, 2024
358ecb2
Fix minor issues
Twixes Aug 2, 2024
dd662b3
Add `TeamAndOrgViewSetMixin` tests
Twixes Aug 19, 2024
f078273
Fix code quality points
Twixes Aug 21, 2024
780f163
Make mypy happy
Twixes Aug 22, 2024
7d71ea4
Add test coverage across /projects AND /environments endpoints
Twixes Aug 28, 2024
eeca422
Make mypy happy
Twixes Aug 28, 2024
a6171db
Make mypy happier
Twixes Aug 31, 2024
08cc286
Update mypy-baseline.txt
Twixes Sep 2, 2024
3f6d2d9
More DRY
Twixes Sep 3, 2024
b397a27
Fix undefined var
Twixes Sep 4, 2024
1b349e4
Update query snapshots
github-actions[bot] Sep 4, 2024
083b86c
Fix missing kwarg
Twixes Sep 4, 2024
5af9ab3
Merge branch 'master' into environments-endpoint
Twixes Sep 5, 2024
68e6c50
Merge branch 'master' into environments-endpoint
Twixes Sep 5, 2024
1343645
Update projects API tests
Twixes Sep 9, 2024
b5b9d35
Update query snapshots
github-actions[bot] Sep 9, 2024
53848e7
Update UI snapshots for `chromium` (1)
github-actions[bot] Sep 9, 2024
c3ab0ed
Merge branch 'master' into environments-endpoint
Twixes Sep 10, 2024
2cdf008
Update test_external_data_source.py
Twixes Sep 10, 2024
48dce4b
Merge branch 'master' into environments-endpoint
Twixes Sep 10, 2024
cf886a6
Restore `is_demo` support and test
Twixes Sep 10, 2024
910e133
Update query snapshots
github-actions[bot] Sep 10, 2024
f85cbbc
Fix a couple assertions
Twixes Sep 10, 2024
2b4a18a
Add `autocapture_web_vitals_allowed_metrics`
Twixes Sep 10, 2024
9b47e03
Update UI snapshots for `chromium` (2)
github-actions[bot] Sep 10, 2024
6436b98
Update query snapshots
github-actions[bot] Sep 10, 2024
83ab9f8
Add `autocapture_web_vitals_allowed_metrics` bis
Twixes Sep 10, 2024
4c080a0
Update query snapshots
github-actions[bot] Sep 11, 2024
cc4a5f0
Merge branch 'master' into environments-endpoint
Twixes Sep 11, 2024
205cb6b
Update query snapshots
github-actions[bot] Sep 11, 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
3 changes: 2 additions & 1 deletion .storybook/app-context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'
import { MOCK_DEFAULT_TEAM, MOCK_DEFAULT_PROJECT } from 'lib/api.mock'
import { AppContext } from '~/types'

export const getStorybookAppContext = (): AppContext => ({
anonymous: false,
// Ideally we wouldn't set `current_team` here, the same way we don't set `current_user`, but unfortunately
// as of March 2024, a bunch of logics make the assumption that this is set, via `AppConfig`
current_team: MOCK_DEFAULT_TEAM,
current_project: MOCK_DEFAULT_PROJECT,
current_user: undefined as any, // `undefined` triggers a fetch and lets us mock the data
default_event_name: '$pageview',
persisted_feature_flags: [],
Expand Down
1 change: 1 addition & 0 deletions ee/api/test/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ def mock_implementation(url: str, headers: Any = None, params: Any = None) -> Ma
# Create a demo project
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()
self.assertEqual(Team.objects.count(), 1)
response = self.client.post("/api/projects/", {"name": "Test", "is_demo": True})
self.assertEqual(response.status_code, 201)
self.assertEqual(Team.objects.count(), 3)
Expand Down
2 changes: 1 addition & 1 deletion ee/api/test/test_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_create_organization(self):
OrganizationMembership.Level.OWNER,
)

@patch("secrets.choice", return_value="Y")
@patch("posthog.models.utils.generate_random_short_suffix", return_value="YYYY")
def test_create_two_similarly_named_organizations(self, mock_choice):
response = self.client.post(
"/api/organizations/",
Expand Down
48 changes: 48 additions & 0 deletions ee/api/test/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from ee.api.test.test_team import team_enterprise_api_test_factory
from posthog.api.test.test_team import EnvironmentToProjectRewriteClient
from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.project import Project
from posthog.models.team.team import Team


class TestProjectEnterpriseAPI(team_enterprise_api_test_factory()):
"""
We inherit from TestTeamEnterpriseAPI, as previously /api/projects/ referred to the Team model, which used to mean "project".
Now as Team means "environment" and Project is separate, we must ensure backward compatibility of /api/projects/.
At the same time, this class is where we can continue adding `Project`-specific API tests.
"""

client_class = EnvironmentToProjectRewriteClient

def test_user_create_project_for_org_via_url(self):
# Set both current and new org to high enough membership level
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()

current_org, _, _ = Organization.objects.bootstrap(self.user, name="other_org")
other_org = self.organization # Bootstrapping above sets it to the current org
assert Team.objects.count() == 2
assert Project.objects.count() == 2

assert current_org.id == self.user.current_organization_id
response = self.client.post(f"/api/organizations/{current_org.id}/projects/", {"name": "Via current org"})
self.assertEqual(response.status_code, 201)
assert response.json()["organization"] == str(current_org.id)
assert Team.objects.count() == 3
assert Project.objects.count() == 3

assert other_org.id != self.user.current_organization_id
response = self.client.post(f"/api/organizations/{other_org.id}/projects/", {"name": "Via path org"})
self.assertEqual(response.status_code, 201, msg=response.json())
assert response.json()["organization"] == str(other_org.id)
assert Team.objects.count() == 4
assert Project.objects.count() == 4

def test_user_cannot_create_project_in_org_without_access(self):
_, _, _ = Organization.objects.bootstrap(self.user, name="other_org")
other_org = self.organization # Bootstrapping above sets it to the current org

assert other_org.id != self.user.current_organization_id
response = self.client.post(f"/api/organizations/{other_org.id}/projects/", {"name": "Via path org"})
self.assertEqual(response.status_code, 403, msg=response.json())
assert response.json() == self.permission_denied_response("Your organization access level is insufficient.")
Loading
Loading