Skip to content

Commit 7c32e27

Browse files
authored
fix: Show 404 page for nonexistent org (#2620)
Renders 404 page if org in URL doesn't exist.
1 parent 5b0f851 commit 7c32e27

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

frontend/src/index.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe("browsertrix-app", () => {
8585
expect(el.shadowRoot?.childElementCount).to.not.equal(0);
8686
});
8787

88-
it("renders org when authenticated", async () => {
88+
it("renders 404 when not in org", async () => {
8989
stub(AuthService, "initSessionStorage").returns(
9090
Promise.resolve({
9191
headers: { Authorization: "_fake_headers_" },
@@ -95,7 +95,46 @@ describe("browsertrix-app", () => {
9595
);
9696
// @ts-expect-error checkFreshness is private
9797
stub(AuthService.prototype, "checkFreshness");
98-
AppStateService.updateOrgSlug("fake-org");
98+
99+
AppStateService.updateUser(
100+
formatAPIUser({
101+
...mockAPIUser,
102+
}),
103+
);
104+
AppStateService.updateOrgSlug("nonexistent-org");
105+
const el = await fixture<App>(
106+
html` <browsertrix-app .settings=${mockAppSettings}></browsertrix-app>`,
107+
);
108+
await el.updateComplete;
109+
expect(el.shadowRoot?.querySelector("btrix-not-found")).to.exist;
110+
});
111+
112+
it("renders org when in org", async () => {
113+
const id = self.crypto.randomUUID();
114+
const mockOrg = {
115+
id: id,
116+
name: "test org 2",
117+
slug: "test-org-2",
118+
role: 10,
119+
};
120+
121+
stub(AuthService, "initSessionStorage").returns(
122+
Promise.resolve({
123+
headers: { Authorization: "_fake_headers_" },
124+
tokenExpiresAt: 0,
125+
username: "[email protected]",
126+
}),
127+
);
128+
// @ts-expect-error checkFreshness is private
129+
stub(AuthService.prototype, "checkFreshness");
130+
131+
AppStateService.updateUser(
132+
formatAPIUser({
133+
...mockAPIUser,
134+
orgs: [...mockAPIUser.orgs, mockOrg],
135+
}),
136+
);
137+
AppStateService.updateOrgSlug("test-org-2");
99138
const el = await fixture<App>(
100139
html` <browsertrix-app .settings=${mockAppSettings}></browsertrix-app>`,
101140
);

frontend/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,6 @@ export class App extends BtrixElement {
521521
>
522522
`
523523
: nothing}
524-
<div role="separator" class="mx-2.5 h-7 w-0 border-l"></div>
525524
${this.renderOrgs()}
526525
`,
527526
)}
@@ -639,19 +638,20 @@ export class App extends BtrixElement {
639638

640639
const selectedOption = this.orgSlugInPath
641640
? orgs.find(({ slug }) => slug === this.orgSlugInPath)
642-
: { slug: "", name: msg("All Organizations") };
641+
: {
642+
slug: "",
643+
name: msg("All Organizations"),
644+
};
645+
643646
if (!selectedOption) {
644-
console.debug(
645-
`Couldn't find organization with slug ${this.orgSlugInPath}`,
646-
orgs,
647-
);
648647
return;
649648
}
650649

651650
// Limit org name display for orgs created before org name max length restriction
652651
const orgNameLength = 50;
653652

654653
return html`
654+
<div role="separator" class="mx-2.5 h-7 w-0 border-l"></div>
655655
<div class="max-w-32 truncate sm:max-w-52 md:max-w-none">
656656
${selectedOption.slug
657657
? html`
@@ -869,6 +869,10 @@ export class App extends BtrixElement {
869869
return html`<btrix-orgs class="w-full md:bg-neutral-50"></btrix-orgs>`;
870870

871871
case "org": {
872+
if (!this.isUserInCurrentOrg) {
873+
return this.renderNotFoundPage();
874+
}
875+
872876
const slug = this.viewState.params.slug;
873877
const orgPath = this.viewState.pathname;
874878
const pathname = this.getLocationPathname();

0 commit comments

Comments
 (0)