GET still shows a one-button page, a used invitation can resend or reopen for the signed-in invitee, and an email-change GET no longer signs the account out.
144 lines
4.6 KiB
TypeScript
144 lines
4.6 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const isDatabaseConfiguredMock = vi.fn();
|
|
const getSessionPepperMock = vi.fn();
|
|
const hashSessionTokenMock = vi.fn();
|
|
const findUniqueMock = vi.fn();
|
|
const deleteMock = vi.fn();
|
|
const upsertMock = vi.fn();
|
|
const createSessionForUserMock = vi.fn();
|
|
const setSessionCookieMock = vi.fn();
|
|
|
|
vi.mock("../../lib/server/env", () => ({
|
|
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
|
getSessionPepper: () => getSessionPepperMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/hash", () => ({
|
|
hashSessionToken: (...args: unknown[]) => hashSessionTokenMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/session", () => ({
|
|
createSessionForUser: (...args: unknown[]) =>
|
|
createSessionForUserMock(...args),
|
|
setSessionCookie: (...args: unknown[]) => setSessionCookieMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/db", () => ({
|
|
prisma: {
|
|
magicLinkToken: {
|
|
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
|
delete: (...args: unknown[]) => deleteMock(...args),
|
|
},
|
|
user: {
|
|
upsert: (...args: unknown[]) => upsertMock(...args),
|
|
},
|
|
ruleDraft: {
|
|
upsert: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
import { GET, POST } from "../../app/api/auth/magic-link/verify/route";
|
|
|
|
beforeEach(() => {
|
|
isDatabaseConfiguredMock.mockReset();
|
|
getSessionPepperMock.mockReset();
|
|
hashSessionTokenMock.mockReset();
|
|
findUniqueMock.mockReset();
|
|
deleteMock.mockReset();
|
|
upsertMock.mockReset();
|
|
createSessionForUserMock.mockReset();
|
|
setSessionCookieMock.mockReset();
|
|
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionPepperMock.mockReturnValue("pepper");
|
|
hashSessionTokenMock.mockReturnValue("token-hash");
|
|
});
|
|
|
|
function getWithToken(token: string) {
|
|
return new NextRequest(
|
|
`https://x.test/api/auth/magic-link/verify?token=${encodeURIComponent(token)}`,
|
|
);
|
|
}
|
|
|
|
describe("GET /api/auth/magic-link/verify", () => {
|
|
it("redirects with invalid_link when the token is missing", async () => {
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/auth/magic-link/verify"),
|
|
);
|
|
expect(res.status).toBe(307);
|
|
expect(res.headers.get("location")).toContain("error=invalid_link");
|
|
});
|
|
|
|
it("redirects with invalid_link when no row matches", async () => {
|
|
findUniqueMock.mockResolvedValue(null);
|
|
const res = await GET(getWithToken("a-token-value-long-enough"));
|
|
expect(res.status).toBe(307);
|
|
expect(res.headers.get("location")).toContain("error=invalid_link");
|
|
});
|
|
|
|
it("redirects with expired_link when the row is past expiresAt", async () => {
|
|
findUniqueMock.mockResolvedValue({
|
|
id: "row-1",
|
|
email: "a@b.c",
|
|
expiresAt: new Date(Date.now() - 1000),
|
|
nextPath: null,
|
|
draftPayload: null,
|
|
});
|
|
const res = await GET(getWithToken("a-token-value-long-enough"));
|
|
expect(res.status).toBe(307);
|
|
expect(res.headers.get("location")).toContain("error=expired_link");
|
|
expect(deleteMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("redirects a valid token to the confirm page without spending it", async () => {
|
|
const token = "a-token-value-long-enough";
|
|
findUniqueMock.mockResolvedValue({
|
|
id: "row-1",
|
|
email: "a@b.c",
|
|
expiresAt: new Date(Date.now() + 60_000),
|
|
nextPath: "/create/review",
|
|
draftPayload: null,
|
|
});
|
|
const res = await GET(getWithToken(token));
|
|
expect(res.status).toBe(307);
|
|
expect(res.headers.get("location")).toContain("/login/confirm");
|
|
expect(res.headers.get("location")).toContain(encodeURIComponent(token));
|
|
expect(deleteMock).not.toHaveBeenCalled();
|
|
expect(upsertMock).not.toHaveBeenCalled();
|
|
expect(createSessionForUserMock).not.toHaveBeenCalled();
|
|
expect(setSessionCookieMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("POST /api/auth/magic-link/verify", () => {
|
|
it("spends a valid token, starts a session, and redirects", async () => {
|
|
findUniqueMock.mockResolvedValue({
|
|
id: "row-1",
|
|
email: "a@b.c",
|
|
expiresAt: new Date(Date.now() + 60_000),
|
|
nextPath: "/create/review",
|
|
draftPayload: null,
|
|
});
|
|
upsertMock.mockResolvedValue({ id: "u1", email: "a@b.c" });
|
|
createSessionForUserMock.mockResolvedValue({
|
|
token: "sess",
|
|
expiresAt: new Date(),
|
|
});
|
|
|
|
const res = await POST(
|
|
new NextRequest(getWithToken("a-token-value-long-enough").url, {
|
|
method: "POST",
|
|
}),
|
|
);
|
|
|
|
expect(res.status).toBe(303);
|
|
expect(res.headers.get("location")).toContain("/create/review");
|
|
expect(deleteMock).toHaveBeenCalledWith({ where: { id: "row-1" } });
|
|
expect(upsertMock).toHaveBeenCalled();
|
|
expect(setSessionCookieMock).toHaveBeenCalled();
|
|
});
|
|
});
|