Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,174 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { MemoryRouter, useLoaderData } from "react-router";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { consumeMagicLinkToken } from "~/services/auth/magic-link.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import { createUserSession, getUserInfo, setLoggedUser } from "~/utils/session.server";
|
||||
import * as MagicLinkRoute from "./auth.magic-link";
|
||||
|
||||
vi.mock("react-router", async () => {
|
||||
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
||||
return {
|
||||
...actual,
|
||||
useLoaderData: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => translations[key] ?? key,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("~/services/auth/magic-link.server", () => ({
|
||||
consumeMagicLinkToken: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/users.db.server", () => ({
|
||||
getUser: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
createUserSession: vi.fn(),
|
||||
getUserInfo: vi.fn(),
|
||||
setLoggedUser: vi.fn(),
|
||||
}));
|
||||
|
||||
const translations: Record<string, string> = {
|
||||
"auth:magicLink.invalid.title": "Sign-in link is invalid",
|
||||
"auth:magicLink.invalid.description": "This link could not be verified. Request a new one.",
|
||||
"auth:magicLink.expired.title": "Sign-in link has expired",
|
||||
"auth:magicLink.expired.description": "Magic links are valid for 15 minutes. Request a new one and use it right away.",
|
||||
"auth:magicLink.replayed.title": "Sign-in link already used",
|
||||
"auth:magicLink.replayed.description": "Each magic link works only once. Request a new one to sign in.",
|
||||
"auth:magicLink.purposeMismatch.title": "Sign-in link is invalid",
|
||||
"auth:magicLink.purposeMismatch.description": "This link cannot be used for sign-in. Request a new one.",
|
||||
"auth:magicLink.requestNewLink": "Request a new sign-in link",
|
||||
// D-D0a — Davids Invalid-Link-Screen nutzt die account.login.* Aktions-Labels.
|
||||
"account.login.magicLinkRequestNew": "Request new link",
|
||||
"account.login.backToLogin": "Back to sign in",
|
||||
};
|
||||
|
||||
const consumeMagicLinkTokenMock = vi.mocked(consumeMagicLinkToken);
|
||||
const getUserMock = vi.mocked(getUser);
|
||||
const getUserInfoMock = vi.mocked(getUserInfo);
|
||||
const setLoggedUserMock = vi.mocked(setLoggedUser);
|
||||
const createUserSessionMock = vi.mocked(createUserSession);
|
||||
|
||||
function renderMagicLinkStatus(status: "invalid" | "expired" | "replayed" | "not-found" | "purpose-mismatch") {
|
||||
vi.mocked(useLoaderData).mockReturnValue({ status });
|
||||
|
||||
return render(
|
||||
<MemoryRouter>
|
||||
<MagicLinkRoute.default />
|
||||
</MemoryRouter>
|
||||
);
|
||||
}
|
||||
|
||||
function callLoader(url: string, init?: RequestInit) {
|
||||
return MagicLinkRoute.loader({
|
||||
request: new Request(url, init),
|
||||
params: {},
|
||||
context: {},
|
||||
});
|
||||
}
|
||||
|
||||
describe("auth magic-link route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
getUserInfoMock.mockResolvedValue({ lng: "en", userId: "existing-user" } as Awaited<ReturnType<typeof getUserInfo>>);
|
||||
setLoggedUserMock.mockResolvedValue({ userId: "user_1" } as Awaited<ReturnType<typeof setLoggedUser>>);
|
||||
createUserSessionMock.mockResolvedValue(new Response(null, { status: 302, headers: { Location: "/" } }) as Awaited<ReturnType<typeof createUserSession>>);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["invalid", "Sign-in link is invalid", "This link could not be verified. Request a new one."],
|
||||
["expired", "Sign-in link has expired", "Magic links are valid for 15 minutes. Request a new one and use it right away."],
|
||||
["replayed", "Sign-in link already used", "Each magic link works only once. Request a new one to sign in."],
|
||||
["not-found", "Sign-in link is invalid", "This link could not be verified. Request a new one."],
|
||||
["purpose-mismatch", "Sign-in link is invalid", "This link cannot be used for sign-in. Request a new one."],
|
||||
] as const)("renders translated copy for %s magic-link failures", (status, title, description) => {
|
||||
renderMagicLinkStatus(status);
|
||||
|
||||
expect(screen.getByRole("heading", { name: title })).toBeInTheDocument();
|
||||
expect(screen.getByText(description)).toBeInTheDocument();
|
||||
// D-D0a — Davids "Invalid Link"-Screen: "Neuen Link anfordern" + "Zurueck zur Anmeldung" (Buttons -> /login,
|
||||
// dort liegt der Magic-Link-Request). Keine Forgot-Password-Verlinkung mehr.
|
||||
expect(screen.getByRole("button", { name: "Request new link" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "Back to sign in" })).toBeInTheDocument();
|
||||
expect(screen.queryByText(/^auth[.:]magicLink/u)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("rejects missing tokens without consuming tokens or creating sessions", async () => {
|
||||
await expect(callLoader("https://example.test/auth/magic-link")).resolves.toEqual({ status: "invalid" });
|
||||
|
||||
expect(consumeMagicLinkTokenMock).not.toHaveBeenCalled();
|
||||
expect(getUserMock).not.toHaveBeenCalled();
|
||||
expect(getUserInfoMock).not.toHaveBeenCalled();
|
||||
expect(setLoggedUserMock).not.toHaveBeenCalled();
|
||||
expect(createUserSessionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(["invalid", "expired", "replayed", "not-found", "purpose-mismatch"] as const)(
|
||||
"returns a generic failure state for %s tokens without creating a session",
|
||||
async (reason) => {
|
||||
consumeMagicLinkTokenMock.mockResolvedValue({ ok: false, reason });
|
||||
|
||||
await expect(callLoader("https://example.test/auth/magic-link?token=bad-token")).resolves.toEqual({ status: reason });
|
||||
|
||||
expect(consumeMagicLinkTokenMock).toHaveBeenCalledWith("bad-token", { expectedPurpose: "login" });
|
||||
expect(getUserMock).not.toHaveBeenCalled();
|
||||
expect(getUserInfoMock).not.toHaveBeenCalled();
|
||||
expect(setLoggedUserMock).not.toHaveBeenCalled();
|
||||
expect(createUserSessionMock).not.toHaveBeenCalled();
|
||||
}
|
||||
);
|
||||
|
||||
it("does not create a session when the consumed token points at a missing user", async () => {
|
||||
consumeMagicLinkTokenMock.mockResolvedValue({ ok: true, userId: "missing-user", tokenId: "token_1", purpose: "login" });
|
||||
getUserMock.mockResolvedValue(null);
|
||||
|
||||
await expect(callLoader("https://example.test/auth/magic-link?token=valid-token")).resolves.toEqual({ status: "not-found" });
|
||||
|
||||
expect(getUserMock).toHaveBeenCalledWith("missing-user");
|
||||
expect(getUserInfoMock).not.toHaveBeenCalled();
|
||||
expect(setLoggedUserMock).not.toHaveBeenCalled();
|
||||
expect(createUserSessionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("creates a fresh session only after a valid login-purpose token resolves to a user", async () => {
|
||||
consumeMagicLinkTokenMock.mockResolvedValue({ ok: true, userId: "user_1", tokenId: "token_1", purpose: "login" });
|
||||
getUserMock.mockResolvedValue({ id: "user_1", email: "ada@example.test", defaultTenantId: "tenant_1", locale: "de" } as Awaited<ReturnType<typeof getUser>>);
|
||||
|
||||
const response = await callLoader("https://example.test/auth/magic-link?token=valid-token");
|
||||
|
||||
expect(response).toBeInstanceOf(Response);
|
||||
expect((response as Response).status).toBe(302);
|
||||
expect(consumeMagicLinkTokenMock).toHaveBeenCalledWith("valid-token", { expectedPurpose: "login" });
|
||||
expect(setLoggedUserMock).toHaveBeenCalledWith({
|
||||
id: "user_1",
|
||||
email: "ada@example.test",
|
||||
defaultTenantId: "tenant_1",
|
||||
});
|
||||
expect(createUserSessionMock).toHaveBeenCalledWith(
|
||||
{
|
||||
lng: "de",
|
||||
userId: "user_1",
|
||||
},
|
||||
"/"
|
||||
);
|
||||
});
|
||||
|
||||
it("has no action export, so unsafe HTTP methods stay outside the route module", () => {
|
||||
expect("action" in MagicLinkRoute).toBe(false);
|
||||
});
|
||||
|
||||
it("marks token responses as non-cacheable and strips referrers", () => {
|
||||
expect(MagicLinkRoute.headers()).toEqual({
|
||||
"Cache-Control": "no-store",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user