Skip to content

Commit

Permalink
fix(environments): Fixes around project/env creation (#26520)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Twixes and github-actions[bot] authored Nov 28, 2024
1 parent 3ab5e4e commit 0c761f4
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 97 deletions.
10 changes: 10 additions & 0 deletions ee/api/test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def test_user_that_does_not_belong_to_an_org_cannot_create_a_projec(self):
},
)

def test_rename_project_as_org_member_allowed(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()

response = self.client.patch(f"/api/projects/@current/", {"name": "Erinaceus europaeus"})
self.project.refresh_from_db()

self.assertEqual(response.status_code, 200)
self.assertEqual(self.project.name, "Erinaceus europaeus")

def test_list_projects_restricted_ones_hidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
Expand Down
110 changes: 55 additions & 55 deletions ee/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,61 +184,6 @@ def test_no_delete_team_not_belonging_to_organization(self):

# Updating projects

def test_rename_team_as_org_member_allowed(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()

response = self.client.patch(f"/api/environments/@current/", {"name": "Erinaceus europaeus"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(self.team.name, "Erinaceus europaeus")

def test_rename_private_team_as_org_member_forbidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
self.team.access_control = True
self.team.save()

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.assertEqual(self.team.name, "Default project")

def test_rename_private_team_current_as_org_outsider_forbidden(self):
self.organization_membership.delete()

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_404_NOT_FOUND)

def test_rename_private_team_id_as_org_outsider_forbidden(self):
self.organization_membership.delete()

response = self.client.patch(f"/api/environments/{self.team.id}/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_404_NOT_FOUND)

def test_rename_private_team_as_org_member_and_team_member_allowed(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
self.team.access_control = True
self.team.save()
ExplicitTeamMembership.objects.create(
team=self.team,
parent_membership=self.organization_membership,
level=ExplicitTeamMembership.Level.MEMBER,
)

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(self.team.name, "Acherontia atropos")

def test_enable_access_control_as_org_member_forbidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
Expand Down Expand Up @@ -608,6 +553,61 @@ def test_cannot_create_team_in_project_without_org_access(self):
self.not_found_response("Organization not found."),
)

def test_rename_team_as_org_member_allowed(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()

response = self.client.patch(f"/api/environments/@current/", {"name": "Erinaceus europaeus"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(self.team.name, "Erinaceus europaeus")

def test_rename_private_team_as_org_member_forbidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
self.team.access_control = True
self.team.save()

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.assertEqual(self.team.name, "Default project")

def test_rename_private_team_current_as_org_outsider_forbidden(self):
self.organization_membership.delete()

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_404_NOT_FOUND)

def test_rename_private_team_id_as_org_outsider_forbidden(self):
self.organization_membership.delete()

response = self.client.patch(f"/api/environments/{self.team.id}/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_404_NOT_FOUND)

def test_rename_private_team_as_org_member_and_team_member_allowed(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
self.team.access_control = True
self.team.save()
ExplicitTeamMembership.objects.create(
team=self.team,
parent_membership=self.organization_membership,
level=ExplicitTeamMembership.Level.MEMBER,
)

response = self.client.patch(f"/api/environments/@current/", {"name": "Acherontia atropos"})
self.team.refresh_from_db()

self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(self.team.name, "Acherontia atropos")

def test_list_teams_restricted_ones_hidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions frontend/src/scenes/projectLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getAppContext } from 'lib/utils/getAppContext'

import { ProjectType } from '~/types'

import { organizationLogic } from './organizationLogic'
import type { projectLogicType } from './projectLogicType'
import { userLogic } from './userLogic'

Expand All @@ -19,7 +20,7 @@ export const projectLogic = kea<projectLogicType>([
deleteProjectFailure: true,
}),
connect(() => ({
actions: [userLogic, ['loadUser', 'switchTeam']],
actions: [userLogic, ['loadUser', 'switchTeam'], organizationLogic, ['loadCurrentOrganization']],
})),
reducers({
projectBeingDeleted: [
Expand Down Expand Up @@ -57,6 +58,8 @@ export const projectLogic = kea<projectLogicType>([
)) as ProjectType
breakpoint()

// We need to reload current org (which lists its projects) in organizationLogic AND in userLogic
actions.loadCurrentOrganization()
actions.loadUser()

Object.keys(payload).map((property) => {
Expand Down Expand Up @@ -88,7 +91,7 @@ export const projectLogic = kea<projectLogicType>([
selectors({
currentProjectId: [(s) => [s.currentProject], (currentProject) => currentProject?.id || null],
}),
listeners(({ actions, values }) => ({
listeners(({ actions }) => ({
loadCurrentProjectSuccess: ({ currentProject }) => {
if (currentProject) {
ApiConfig.setCurrentProjectId(currentProject.id)
Expand All @@ -107,7 +110,7 @@ export const projectLogic = kea<projectLogicType>([
lemonToast.success('Project has been deleted')
},
createProjectSuccess: ({ currentProject }) => {
if (currentProject && currentProject.id !== values.currentProject?.id) {
if (currentProject) {
actions.switchTeam(currentProject.id)
}
},
Expand Down
8 changes: 0 additions & 8 deletions frontend/src/scenes/settings/environment/TeamSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export function TeamDisplayName(): JSX.Element {

const displayNoun = featureFlags[FEATURE_FLAGS.ENVIRONMENTS] ? 'environment' : 'project'

if (currentTeam?.is_demo) {
return (
<p>
<i>The demo {displayNoun} cannot be renamed.</i>
</p>
)
}

return (
<div className="space-y-4 max-w-160">
<LemonInput value={name} onChange={setName} disabled={currentTeamLoading} />
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/scenes/teamLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface FrequentMistakeAdvice {
export const teamLogic = kea<teamLogicType>([
path(['scenes', 'teamLogic']),
connect(() => ({
actions: [userLogic, ['loadUser', 'switchTeam']],
actions: [userLogic, ['loadUser', 'switchTeam'], organizationLogic, ['loadCurrentOrganization']],
values: [projectLogic, ['currentProject'], featureFlagLogic, ['featureFlags']],
})),
actions({
Expand Down Expand Up @@ -103,6 +103,8 @@ export const teamLogic = kea<teamLogicType>([
const [patchedTeam] = await Promise.all(promises)
breakpoint()

// We need to reload current org (which lists its teams) in organizationLogic AND in userLogic
actions.loadCurrentOrganization()
actions.loadUser()

/* Notify user the update was successful */
Expand Down
11 changes: 5 additions & 6 deletions posthog/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,13 @@ def update(self, instance: Project, validated_data: dict[str, Any]) -> Project:

should_team_be_saved_too = False
for attr, value in validated_data.items():
if attr in self.Meta.team_passthrough_fields:
if attr not in self.Meta.team_passthrough_fields:
# This attr is a Project field
setattr(instance, attr, value)
else:
# This attr is actually on the Project's passthrough Team
should_team_be_saved_too = True
setattr(team, attr, value)
else:
if attr == "name": # `name` should be updated on _both_ the Project and Team
should_team_be_saved_too = True
setattr(team, attr, value)
setattr(instance, attr, value)

instance.save()
if should_team_be_saved_too:
Expand Down
Loading

0 comments on commit 0c761f4

Please sign in to comment.