Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
9.2 kB ยท 305 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306import { expect } from "@std/expect";import { describe, test } from "@std/testing/bdd";
import type { ResolvedRoute } from "../resolve-route";import { resolvePasswordAction } from "./resolve-password-action";
const pathnameRoute: ResolvedRoute = { type: "pathname", prefix: "acme", locale: "en", localeExplicit: true, rewritePath: "/acme/en",};
const hostnameRoute: ResolvedRoute = { type: "hostname", prefix: "acme", locale: "en", localeExplicit: true, rewritePath: "/acme/en",};
const passwordPage = { slug: "acme", customDomain: "", password: "secret", accessType: "password" as const,};
const customDomainPage = { slug: "acme", customDomain: "status.acme.com", password: "secret", accessType: "password" as const,};
describe("resolvePasswordAction", () => { test("non-password page: passes (null)", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: { ...passwordPage, accessType: "public" as const }, pathname: "/acme/en", host: "localhost:3000", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action).toBeNull(); });
test("gate-in pathname: wrong password, not on /login โ redirect to /acme/login", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/en", host: "localhost:3000", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.type).toBe("redirect"); expect(action?.reason).toBe("password-gate-in"); expect(action?.url?.pathname).toBe("/acme/login"); expect(action?.url?.searchParams.get("redirect")).toBe("/acme/en"); });
test("gate-in hostname: wrong password, not on /login โ redirect to /login (no prefix)", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: passwordPage, pathname: "/en", host: "acme.localhost:3000", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://acme.localhost:3000", }); expect(action?.reason).toBe("password-gate-in"); expect(action?.url?.pathname).toBe("/login"); });
test("gate-in custom-domain: redirects to https://{customDomain}/login", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: customDomainPage, pathname: "/status.acme.com/en", host: "status.acme.com", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.reason).toBe("password-gate-in-custom-domain"); expect(action?.url?.toString()).toBe( "https://status.acme.com/login?redirect=%2Fen", ); });
test("gate-in custom-domain: only leading customDomain segment is stripped", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: customDomainPage, pathname: "/status.acme.com/en/status.acme.com/nested", host: "status.acme.com", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.url?.searchParams.get("redirect")).toBe( "/en/status.acme.com/nested", ); });
test("gate-in custom-domain skipped in self-hosted", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: customDomainPage, pathname: "/acme/en", host: "status.acme.com", isSelfHosted: true, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://self.hosted", }); expect(action?.reason).toBe("password-gate-in"); expect(action?.url?.host).toBe("self.hosted"); });
test("gate-in custom-domain skipped when host is {slug}.stpg.dev", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: customDomainPage, pathname: "/en", host: "acme.stpg.dev", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "https://acme.stpg.dev", }); expect(action?.reason).toBe("password-gate-in"); expect(action?.url?.host).toBe("acme.stpg.dev"); });
test("cookie password matches: on /login โ gate-out", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/login", host: "localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.reason).toBe("password-gate-out"); expect(action?.type).toBe("redirect"); });
test("query password overrides cookie", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/login", host: "localhost:3000", isSelfHosted: false, cookiePassword: "wrong-cookie", queryPassword: "secret", redirectParam: null, origin: "http://localhost:3000", }); expect(action?.reason).toBe("password-gate-out"); });
test("gate-out custom-domain: redirects to https://{customDomain}/", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: customDomainPage, pathname: "/status.acme.com/login", host: "status.acme.com", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.reason).toBe("password-gate-out-custom-domain"); expect(action?.url?.toString()).toBe("https://status.acme.com/"); });
test("gate-out custom-domain: honours redirectParam", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: customDomainPage, pathname: "/status.acme.com/login", host: "status.acme.com", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: "/events", origin: "http://localhost:3000", }); expect(action?.url?.toString()).toBe("https://status.acme.com/events"); });
test("gate-out pathname with redirectParam: honours the redirect target", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/login", host: "localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: "/acme/en/events", origin: "http://localhost:3000", }); expect(action?.url?.pathname).toBe("/acme/en/events"); });
test("gate-out hostname with redirectParam: honours the redirect target", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: passwordPage, pathname: "/login", host: "acme.localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: "/en/events", origin: "http://acme.localhost:3000", }); expect(action?.url?.pathname).toBe("/en/events"); });
test("gate-out pathname, no redirectParam: โ /acme", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/login", host: "localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action?.url?.pathname).toBe("/acme"); });
test("gate-out hostname, no redirectParam: โ /", () => { const action = resolvePasswordAction({ route: hostnameRoute, page: passwordPage, pathname: "/login", host: "acme.localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: null, origin: "http://acme.localhost:3000", }); expect(action?.url?.pathname).toBe("/"); });
test("wrong password on /login: passes (null) โ user already on gate", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/login", host: "localhost:3000", isSelfHosted: false, cookiePassword: undefined, queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action).toBeNull(); });
test("correct password off /login: passes (null) โ nothing to gate", () => { const action = resolvePasswordAction({ route: pathnameRoute, page: passwordPage, pathname: "/acme/en", host: "localhost:3000", isSelfHosted: false, cookiePassword: "secret", queryPassword: null, redirectParam: null, origin: "http://localhost:3000", }); expect(action).toBeNull(); });});