From 7c32e27f94bedef1eee2992463cbefc68f4440d9 Mon Sep 17 00:00:00 2001 From: sua yoo Date: Tue, 27 May 2025 18:10:49 -0700 Subject: [PATCH] fix: Show 404 page for nonexistent org (#2620) Renders 404 page if org in URL doesn't exist. --- frontend/src/index.test.ts | 43 ++++++++++++++++++++++++++++++++++++-- frontend/src/index.ts | 16 ++++++++------ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/frontend/src/index.test.ts b/frontend/src/index.test.ts index 94daac59..3fdd863c 100644 --- a/frontend/src/index.test.ts +++ b/frontend/src/index.test.ts @@ -85,7 +85,7 @@ describe("browsertrix-app", () => { expect(el.shadowRoot?.childElementCount).to.not.equal(0); }); - it("renders org when authenticated", async () => { + it("renders 404 when not in org", async () => { stub(AuthService, "initSessionStorage").returns( Promise.resolve({ headers: { Authorization: "_fake_headers_" }, @@ -95,7 +95,46 @@ describe("browsertrix-app", () => { ); // @ts-expect-error checkFreshness is private stub(AuthService.prototype, "checkFreshness"); - AppStateService.updateOrgSlug("fake-org"); + + AppStateService.updateUser( + formatAPIUser({ + ...mockAPIUser, + }), + ); + AppStateService.updateOrgSlug("nonexistent-org"); + const el = await fixture( + html` `, + ); + await el.updateComplete; + expect(el.shadowRoot?.querySelector("btrix-not-found")).to.exist; + }); + + it("renders org when in org", async () => { + const id = self.crypto.randomUUID(); + const mockOrg = { + id: id, + name: "test org 2", + slug: "test-org-2", + role: 10, + }; + + stub(AuthService, "initSessionStorage").returns( + Promise.resolve({ + headers: { Authorization: "_fake_headers_" }, + tokenExpiresAt: 0, + username: "test-auth@example.com", + }), + ); + // @ts-expect-error checkFreshness is private + stub(AuthService.prototype, "checkFreshness"); + + AppStateService.updateUser( + formatAPIUser({ + ...mockAPIUser, + orgs: [...mockAPIUser.orgs, mockOrg], + }), + ); + AppStateService.updateOrgSlug("test-org-2"); const el = await fixture( html` `, ); diff --git a/frontend/src/index.ts b/frontend/src/index.ts index 16434977..a5b02462 100644 --- a/frontend/src/index.ts +++ b/frontend/src/index.ts @@ -521,7 +521,6 @@ export class App extends BtrixElement { > ` : nothing} - ${this.renderOrgs()} `, )} @@ -639,12 +638,12 @@ export class App extends BtrixElement { const selectedOption = this.orgSlugInPath ? orgs.find(({ slug }) => slug === this.orgSlugInPath) - : { slug: "", name: msg("All Organizations") }; + : { + slug: "", + name: msg("All Organizations"), + }; + if (!selectedOption) { - console.debug( - `Couldn't find organization with slug ${this.orgSlugInPath}`, - orgs, - ); return; } @@ -652,6 +651,7 @@ export class App extends BtrixElement { const orgNameLength = 50; return html` +
${selectedOption.slug ? html` @@ -869,6 +869,10 @@ export class App extends BtrixElement { return html``; case "org": { + if (!this.isUserInCurrentOrg) { + return this.renderNotFoundPage(); + } + const slug = this.viewState.params.slug; const orgPath = this.viewState.pathname; const pathname = this.getLocationPathname();