- Notifications
You must be signed in to change notification settings - Fork927
fix: only show editable orgs on deployment page#14193
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
14 commits Select commitHold shift + click to select a range
713b954
fix: only show editable orgs on deployment page
code-asher9f39257
canEditOrganization was lying
code-asher475f6e2
Limit the number of org permission checks
code-asher83bf388
fixup! fix: only show editable orgs on deployment page
code-ashere4f5bf7
Pass in ids instead of entire orgs
code-asher9e76ebc
Assign permission value in one spot
code-asherbfa0000
Improved sort
code-asherb86684d
Tuples-b-gone
code-asher6b42506
Destruct props
code-asher02e7e47
No need for extra routes with a bit of ?
code-asherddd2ea5
Add org IDs to permissions query
code-asher64631e1
Two is now one
code-asher9aca231
Merge remote-tracking branch 'github/main' into asher/show-editable-orgs
code-asherba22bc9
fixup! Merge remote-tracking branch 'github/main' into asher/show-edi…
code-asherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
126 changes: 98 additions & 28 deletionssite/src/api/queries/organizations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletionsite/src/pages/ManagementSettingsPage/ManagementSettingsLayout.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletionsite/src/pages/ManagementSettingsPage/OrganizationMembersPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletionssite/src/pages/ManagementSettingsPage/OrganizationSettingsPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { screen, within } from "@testing-library/react"; | ||
import { HttpResponse, http } from "msw"; | ||
import { | ||
MockDefaultOrganization, | ||
MockOrganization2, | ||
} from "testHelpers/entities"; | ||
import { | ||
renderWithManagementSettingsLayout, | ||
waitForLoaderToBeRemoved, | ||
} from "testHelpers/renderHelpers"; | ||
import { server } from "testHelpers/server"; | ||
import OrganizationSettingsPage from "./OrganizationSettingsPage"; | ||
jest.spyOn(console, "error").mockImplementation(() => {}); | ||
const renderRootPage = async () => { | ||
renderWithManagementSettingsLayout(<OrganizationSettingsPage />, { | ||
route: "/organizations", | ||
path: "/organizations/:organization?", | ||
}); | ||
await waitForLoaderToBeRemoved(); | ||
}; | ||
const renderPage = async (orgName: string) => { | ||
renderWithManagementSettingsLayout(<OrganizationSettingsPage />, { | ||
route: `/organizations/${orgName}`, | ||
path: "/organizations/:organization", | ||
}); | ||
await waitForLoaderToBeRemoved(); | ||
}; | ||
describe("OrganizationSettingsPage", () => { | ||
it("has no organizations", async () => { | ||
server.use( | ||
http.get("/api/v2/organizations", () => { | ||
return HttpResponse.json([]); | ||
}), | ||
http.post("/api/v2/authcheck", async () => { | ||
return HttpResponse.json({ | ||
[`${MockDefaultOrganization.id}.editOrganization`]: true, | ||
viewDeploymentValues: true, | ||
}); | ||
}), | ||
); | ||
await renderRootPage(); | ||
await screen.findByText("No organizations found"); | ||
}); | ||
it("has no editable organizations", async () => { | ||
server.use( | ||
http.get("/api/v2/organizations", () => { | ||
return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); | ||
}), | ||
http.post("/api/v2/authcheck", async () => { | ||
return HttpResponse.json({ | ||
viewDeploymentValues: true, | ||
}); | ||
}), | ||
); | ||
await renderRootPage(); | ||
await screen.findByText("No organizations found"); | ||
}); | ||
it("redirects to default organization", async () => { | ||
server.use( | ||
http.get("/api/v2/organizations", () => { | ||
// Default always preferred regardless of order. | ||
return HttpResponse.json([MockOrganization2, MockDefaultOrganization]); | ||
}), | ||
http.post("/api/v2/authcheck", async () => { | ||
return HttpResponse.json({ | ||
[`${MockDefaultOrganization.id}.editOrganization`]: true, | ||
[`${MockOrganization2.id}.editOrganization`]: true, | ||
viewDeploymentValues: true, | ||
}); | ||
}), | ||
); | ||
await renderRootPage(); | ||
const form = screen.getByTestId("org-settings-form"); | ||
expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue( | ||
MockDefaultOrganization.name, | ||
); | ||
}); | ||
it("redirects to non-default organization", async () => { | ||
server.use( | ||
http.get("/api/v2/organizations", () => { | ||
return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); | ||
}), | ||
http.post("/api/v2/authcheck", async () => { | ||
return HttpResponse.json({ | ||
[`${MockOrganization2.id}.editOrganization`]: true, | ||
viewDeploymentValues: true, | ||
}); | ||
}), | ||
); | ||
await renderRootPage(); | ||
const form = screen.getByTestId("org-settings-form"); | ||
expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue( | ||
MockOrganization2.name, | ||
); | ||
}); | ||
it("cannot find organization", async () => { | ||
server.use( | ||
http.get("/api/v2/organizations", () => { | ||
return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); | ||
}), | ||
http.post("/api/v2/authcheck", async () => { | ||
return HttpResponse.json({ | ||
[`${MockOrganization2.id}.editOrganization`]: true, | ||
viewDeploymentValues: true, | ||
}); | ||
}), | ||
); | ||
await renderPage("the-endless-void"); | ||
await screen.findByText("Organization not found"); | ||
}); | ||
}); |
43 changes: 24 additions & 19 deletionssite/src/pages/ManagementSettingsPage/OrganizationSettingsPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,13 +4,16 @@ import { Navigate, useNavigate, useParams } from "react-router-dom"; | ||
import { | ||
updateOrganization, | ||
deleteOrganization, | ||
organizationsPermissions, | ||
} from "api/queries/organizations"; | ||
import type { Organization } from "api/typesGenerated"; | ||
import { EmptyState } from "components/EmptyState/EmptyState"; | ||
import { displaySuccess } from "components/GlobalSnackbar/utils"; | ||
import { Loader } from "components/Loader/Loader"; | ||
import { | ||
canEditOrganization, | ||
useOrganizationSettings, | ||
} from "./ManagementSettingsLayout"; | ||
import { OrganizationSettingsPageView } from "./OrganizationSettingsPageView"; | ||
const OrganizationSettingsPage: FC = () => { | ||
@@ -32,37 +35,42 @@ const OrganizationSettingsPage: FC = () => { | ||
organizations && organizationName | ||
? getOrganizationByName(organizations, organizationName) | ||
: undefined; | ||
const permissionsQuery = useQuery( | ||
organizationsPermissions(organizations?.map((o) => o.id)), | ||
); | ||
const permissions = permissionsQuery.data; | ||
if (!organizations || !permissions) { | ||
return <Loader />; | ||
} | ||
// Redirect /organizations => /organizations/default-org, or if they cannot edit | ||
// the default org, then the first org they can edit, if any. | ||
if (!organizationName) { | ||
const editableOrg = organizations | ||
.sort((a, b) => { | ||
// Prefer default org (it may not be first). | ||
// JavaScript will happily subtract booleans, but use numbers to keep | ||
// the compiler happy. | ||
return (b.is_default ? 1 : 0) - (a.is_default ? 1 : 0); | ||
code-asher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
}) | ||
.find((org) => canEditOrganization(permissions[org.id])); | ||
if (editableOrg) { | ||
return <Navigate to={`/organizations/${editableOrg.name}`} replace />; | ||
} | ||
return <EmptyState message="No organizations found" />; | ||
} | ||
if (!organization) { | ||
return <EmptyState message="Organization not found" />; | ||
} | ||
const error = | ||
updateOrganizationMutation.error ?? deleteOrganizationMutation.error; | ||
return ( | ||
<OrganizationSettingsPageView | ||
canEdit={permissions[organization.id]?.editOrganization ?? false} | ||
organization={organization} | ||
error={error} | ||
onSubmit={async (values) => { | ||
@@ -85,8 +93,5 @@ const OrganizationSettingsPage: FC = () => { | ||
export default OrganizationSettingsPage; | ||
const getOrganizationByName = (organizations: Organization[], name: string) => | ||
organizations.find((org) => org.name === name); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.