Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+280
@@ -0,0 +1,280 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
function portalRequest(input: string | URL, init: RequestInit = {}): Request {
|
||||
const url = input instanceof URL ? input : new URL(input);
|
||||
const headers = new Headers(init.headers);
|
||||
headers.set("x-hivegrid-portal-tenant", "acme");
|
||||
headers.set("x-hivegrid-portal-origin", url.origin);
|
||||
return new Request(url, { ...init, headers });
|
||||
}
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
requirePortalTenantFromRequest: vi.fn(async () => ({
|
||||
tenantId: "tenant_1",
|
||||
portalId: "portal_1",
|
||||
tenantSlug: "acme",
|
||||
tenantName: "Acme GmbH",
|
||||
portalTitle: "Acme Portal",
|
||||
tenantActive: true,
|
||||
tenantFrozen: false,
|
||||
imprintUrl: "https://acme.example.test/imprint",
|
||||
privacyUrl: "https://acme.example.test/privacy",
|
||||
termsUrl: null,
|
||||
})),
|
||||
consumePortalCapability: vi.fn(),
|
||||
requestPortalSignInLink: vi.fn(),
|
||||
signInPortalWithPassword: vi.fn(),
|
||||
validatePortalCsrfToken: vi.fn(),
|
||||
serializePortalSessionCookie: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@hivegrid/config", () => ({
|
||||
loadRequiredWebConfig: () => ({
|
||||
supportEmail: "support@example.test",
|
||||
captcha: {
|
||||
provider: "mcaptcha",
|
||||
instanceUrl: "https://captcha.example.test",
|
||||
loginSiteKey: "site-key",
|
||||
},
|
||||
}),
|
||||
}));
|
||||
vi.mock("~/components/captcha/Captcha", () => ({ default: () => null }));
|
||||
vi.mock("~/components/screens/customer-portal/CustomerPortalInterimPages", () => ({
|
||||
CustomerPortalAuthPage: () => null,
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-auth-flow.server", () => ({
|
||||
consumePortalCapability: (...args: unknown[]) => mocks.consumePortalCapability(...args),
|
||||
requestPortalPasswordReset: vi.fn(),
|
||||
requestPortalSignInLink: (...args: unknown[]) => mocks.requestPortalSignInLink(...args),
|
||||
signInPortalWithPassword: (...args: unknown[]) => mocks.signInPortalWithPassword(...args),
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-csrf.server", () => ({
|
||||
validatePortalCsrfToken: (...args: unknown[]) => mocks.validatePortalCsrfToken(...args),
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-data.server", () => ({
|
||||
portalNotFound: () => new Response(null, { status: 404 }),
|
||||
requirePortalTenantFromRequest: mocks.requirePortalTenantFromRequest,
|
||||
}));
|
||||
vi.mock("~/services/customer-portal/portal-session-cookie.server", () => ({
|
||||
serializePortalSessionCookie: (...args: unknown[]) => mocks.serializePortalSessionCookie(...args),
|
||||
}));
|
||||
|
||||
import { resolvePortalCapabilityContinuePath } from "~/services/customer-portal/portal-capability-continue.server";
|
||||
|
||||
import { action, loader } from "./auth";
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("customer portal auth loader", () => {
|
||||
it("never serializes a raw capability token into hydration data", async () => {
|
||||
const rawToken = "t".repeat(43);
|
||||
const response = await loader({
|
||||
request: portalRequest(`https://portal.example.test/__portal/acme/reset-password/${rawToken}`),
|
||||
params: { tenant: "acme", "*": `reset-password/${rawToken}` },
|
||||
context: {},
|
||||
} as never);
|
||||
const payload = await response.json();
|
||||
|
||||
expect(JSON.stringify(payload)).not.toContain(rawToken);
|
||||
expect(payload.vm.status).toBe("ready");
|
||||
expect(payload.vm.data).not.toHaveProperty("formAction");
|
||||
expect(payload.vm.data.legalLinks).toEqual({
|
||||
imprintUrl: "https://acme.example.test/imprint",
|
||||
privacyUrl: "https://acme.example.test/privacy",
|
||||
termsUrl: null,
|
||||
});
|
||||
expect(response.headers.get("Referrer-Policy")).toBe("no-referrer");
|
||||
});
|
||||
|
||||
it("leaves ordinary login pages on the server's safe default policy", async () => {
|
||||
const response = await loader({
|
||||
request: portalRequest("https://portal.example.test/__portal/acme/login"),
|
||||
params: { tenant: "acme", "*": "login" },
|
||||
context: {},
|
||||
} as never);
|
||||
|
||||
expect(response.headers.get("Referrer-Policy")).toBeNull();
|
||||
});
|
||||
|
||||
it("parses a slugless custom-host data request as the same login route", async () => {
|
||||
const request = portalRequest("https://kunden.example.org/login.data");
|
||||
request.headers.set("x-hivegrid-portal-custom-host", "kunden.example.org");
|
||||
const response = await loader({ request, params: {}, context: {} } as never);
|
||||
const payload = await response.json();
|
||||
|
||||
expect(payload.vm.status).toBe("ready");
|
||||
expect(payload.vm.data.mode).toBe("login");
|
||||
expect(payload.vm.data.tenantSlug).toBe("acme");
|
||||
expect(payload.vm.data.loginHref).toBe("/login");
|
||||
expect(payload.vm.data.passwordHref).toBe("/forgot-password");
|
||||
});
|
||||
});
|
||||
|
||||
describe("customer portal capability continuation", () => {
|
||||
it("accepts only an exact same-tenant project-detail path", () => {
|
||||
const valid = portalRequest("https://portal.example.test/__portal/acme/login/magic/token?continue=%2Facme%2Fprojects%2Fproject_123");
|
||||
expect(resolvePortalCapabilityContinuePath(valid, "acme")).toBe("/acme/projects/project_123");
|
||||
});
|
||||
|
||||
it.each([
|
||||
"https://evil.example/projects/project_123",
|
||||
"//evil.example/projects/project_123",
|
||||
"/other/projects/project_123",
|
||||
"/acme/projects/project_123/extra",
|
||||
"/acme/projects/../settings",
|
||||
"%2Facme%2Fprojects%2Fproject_123",
|
||||
])("falls back to the dashboard for rejected continue value %s", (continueValue) => {
|
||||
const url = new URL("https://portal.example.test/__portal/acme/login/magic/token");
|
||||
url.searchParams.set("continue", continueValue);
|
||||
expect(resolvePortalCapabilityContinuePath(portalRequest(url), "acme")).toBe("/acme");
|
||||
});
|
||||
|
||||
it("rejects ambiguous repeated continuation parameters", () => {
|
||||
const request = portalRequest(
|
||||
"https://portal.example.test/__portal/acme/login/magic/token?continue=%2Facme%2Fprojects%2Fone&continue=%2Facme%2Fprojects%2Ftwo"
|
||||
);
|
||||
expect(resolvePortalCapabilityContinuePath(request, "acme")).toBe("/acme");
|
||||
});
|
||||
|
||||
it("redirects a successfully consumed magic capability to its validated project", async () => {
|
||||
const rawToken = "t".repeat(43);
|
||||
mocks.consumePortalCapability.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
kind: "session",
|
||||
rawSessionToken: "opaque-session",
|
||||
expiresAt: new Date("2026-08-15T10:00:00.000Z"),
|
||||
});
|
||||
mocks.serializePortalSessionCookie.mockResolvedValueOnce("__Host-hivegrid_portal_session=signed");
|
||||
const request = portalRequest(`https://portal.example.test/__portal/acme/login/magic/${rawToken}?continue=%2Facme%2Fprojects%2Fproject_123`, {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf", captchaToken: "captcha" }),
|
||||
});
|
||||
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
await action({
|
||||
request,
|
||||
params: { tenant: "acme", "*": `login/magic/${rawToken}` },
|
||||
context: {},
|
||||
} as never);
|
||||
} catch (error) {
|
||||
response = error as Response;
|
||||
}
|
||||
|
||||
expect(response?.status).toBe(302);
|
||||
expect(response?.headers.get("Location")).toBe("/acme/projects/project_123");
|
||||
expect(response?.headers.get("Set-Cookie")).toBe("__Host-hivegrid_portal_session=signed");
|
||||
});
|
||||
|
||||
it("carries a validated project continuation through regular magic-link request and magic-sent", async () => {
|
||||
mocks.requestPortalSignInLink.mockResolvedValueOnce({ ok: true, kind: "sent" });
|
||||
const request = portalRequest("https://portal.example.test/__portal/acme/login?continue=%2Facme%2Fprojects%2Fproject_123", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf", captchaToken: "captcha", intent: "magic_link", email: "customer@example.test" }),
|
||||
});
|
||||
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
await action({ request, params: { tenant: "acme", "*": "login" }, context: {} } as never);
|
||||
} catch (error) {
|
||||
response = error as Response;
|
||||
}
|
||||
|
||||
expect(mocks.requestPortalSignInLink).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ email: "customer@example.test", continuePath: "/acme/projects/project_123" })
|
||||
);
|
||||
expect(response?.status).toBe(302);
|
||||
expect(response?.headers.get("Location")).toBe("/acme/login/magic-sent?continue=%2Facme%2Fprojects%2Fproject_123");
|
||||
});
|
||||
|
||||
it("redirects regular password login to the same validated project continuation", async () => {
|
||||
mocks.signInPortalWithPassword.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
kind: "session",
|
||||
rawSessionToken: "opaque-session",
|
||||
expiresAt: new Date("2026-08-15T10:00:00.000Z"),
|
||||
});
|
||||
mocks.serializePortalSessionCookie.mockResolvedValueOnce("__Host-hivegrid_portal_session=signed");
|
||||
const request = portalRequest("https://portal.example.test/__portal/acme/login?continue=%2Facme%2Fprojects%2Fproject_123", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf", captchaToken: "captcha", intent: "password", email: "customer@example.test", password: "secret" }),
|
||||
});
|
||||
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
await action({ request, params: { tenant: "acme", "*": "login" }, context: {} } as never);
|
||||
} catch (error) {
|
||||
response = error as Response;
|
||||
}
|
||||
|
||||
expect(response?.status).toBe(302);
|
||||
expect(response?.headers.get("Location")).toBe("/acme/projects/project_123");
|
||||
});
|
||||
|
||||
it.each(["/other/projects/project_123", "//evil.example/projects/project_123", "https://evil.example/projects/project_123"])(
|
||||
"drops manipulated or foreign-tenant continuation from regular auth: %s",
|
||||
async (continueValue) => {
|
||||
mocks.signInPortalWithPassword.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
kind: "session",
|
||||
rawSessionToken: "opaque-session",
|
||||
expiresAt: new Date("2026-08-15T10:00:00.000Z"),
|
||||
});
|
||||
mocks.serializePortalSessionCookie.mockResolvedValueOnce("__Host-hivegrid_portal_session=signed");
|
||||
const url = new URL("https://portal.example.test/__portal/acme/login");
|
||||
url.searchParams.set("continue", continueValue);
|
||||
const request = portalRequest(url, {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf", captchaToken: "captcha", intent: "password", email: "customer@example.test", password: "secret" }),
|
||||
});
|
||||
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
await action({ request, params: { tenant: "acme", "*": "login" }, context: {} } as never);
|
||||
} catch (error) {
|
||||
response = error as Response;
|
||||
}
|
||||
|
||||
expect(response?.headers.get("Location")).toBe("/acme");
|
||||
}
|
||||
);
|
||||
|
||||
it("passes an omitted password through first-time setup without inventing credentials", async () => {
|
||||
const rawToken = "t".repeat(43);
|
||||
mocks.consumePortalCapability.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
kind: "session",
|
||||
rawSessionToken: "opaque-session",
|
||||
expiresAt: new Date("2026-08-15T10:00:00.000Z"),
|
||||
});
|
||||
mocks.serializePortalSessionCookie.mockResolvedValueOnce("__Host-hivegrid_portal_session=signed");
|
||||
const request = portalRequest(`https://portal.example.test/__portal/acme/setup/${rawToken}`, {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
csrf: "csrf",
|
||||
captchaToken: "captcha",
|
||||
privacyNoticeAcknowledged: "true",
|
||||
privacyNoticeUrl: "https://acme.example.test/privacy",
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
await action({ request, params: { tenant: "acme", "*": `setup/${rawToken}` }, context: {} } as never);
|
||||
} catch (error) {
|
||||
expect((error as Response).status).toBe(302);
|
||||
}
|
||||
|
||||
expect(mocks.consumePortalCapability).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
rawToken,
|
||||
purpose: "setup",
|
||||
password: undefined,
|
||||
passwordConfirmation: undefined,
|
||||
privacyNoticeAcknowledged: true,
|
||||
privacyNoticeUrl: "https://acme.example.test/privacy",
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user