Spend login, invite, and email-change links only after a confirm POST so mail scanners cannot use them up.
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.
This commit is contained in:
1 parent
600056499c
commit
a725cf1a32
33 files changed
+1002
-89
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
import { renderWithProviders as render, screen } from "../utils/test-utils";
|
||||
import "@testing-library/jest-dom/vitest";
|
||||
import { ConfirmLinkScreen } from "../../app/(app)/_components/ConfirmLinkScreen";
|
||||
|
||||
const { navMock } = vi.hoisted(() => ({
|
||||
navMock: {
|
||||
push: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
useRouter: () => ({
|
||||
push: navMock.push,
|
||||
replace: vi.fn(),
|
||||
prefetch: vi.fn(),
|
||||
refresh: vi.fn(),
|
||||
back: vi.fn(),
|
||||
forward: vi.fn(),
|
||||
}),
|
||||
useSearchParams: () => new URLSearchParams(),
|
||||
usePathname: () => "/login/confirm",
|
||||
}));
|
||||
|
||||
const baseProps = {
|
||||
title: "Open this login link",
|
||||
description: "Press continue to log in.",
|
||||
buttonLabel: "Continue",
|
||||
action: "/api/auth/magic-link/verify?token=abcdefghijkl",
|
||||
token: "abcdefghijkl",
|
||||
backHref: "/",
|
||||
backLabel: "← Back to home",
|
||||
missingTitle: "This login link is missing",
|
||||
missingDescription: "Request a new login link from the login page.",
|
||||
missingHref: "/login",
|
||||
missingButtonLabel: "Go to log in",
|
||||
};
|
||||
|
||||
describe("ConfirmLinkScreen", () => {
|
||||
beforeEach(() => {
|
||||
navMock.push.mockReset();
|
||||
});
|
||||
|
||||
it("posts the token when Continue is pressed", () => {
|
||||
const { container } = render(<ConfirmLinkScreen {...baseProps} />);
|
||||
|
||||
expect(
|
||||
screen.getByRole("heading", { name: "Open this login link" }),
|
||||
).toBeInTheDocument();
|
||||
const form = container.querySelector("form");
|
||||
expect(form).toHaveAttribute("method", "POST");
|
||||
expect(form).toHaveAttribute(
|
||||
"action",
|
||||
"/api/auth/magic-link/verify?token=abcdefghijkl",
|
||||
);
|
||||
expect(container.querySelector('input[name="token"]')).toHaveValue(
|
||||
"abcdefghijkl",
|
||||
);
|
||||
expect(screen.getByRole("button", { name: "Continue" })).toHaveAttribute(
|
||||
"type",
|
||||
"submit",
|
||||
);
|
||||
});
|
||||
|
||||
it("offers a way back when the token is missing", () => {
|
||||
render(<ConfirmLinkScreen {...baseProps} token="" action="" />);
|
||||
|
||||
expect(
|
||||
screen.getByRole("heading", { name: "This login link is missing" }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Continue" })).not.toBeInTheDocument();
|
||||
expect(screen.getByRole("link", { name: "Go to log in" })).toHaveAttribute(
|
||||
"href",
|
||||
"/login",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -34,10 +34,13 @@ vi.mock("../../lib/server/db", () => ({
|
||||
user: {
|
||||
upsert: (...args: unknown[]) => upsertMock(...args),
|
||||
},
|
||||
ruleDraft: {
|
||||
upsert: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/auth/magic-link/verify/route";
|
||||
import { GET, POST } from "../../app/api/auth/magic-link/verify/route";
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
@@ -87,5 +90,54 @@ describe("GET /api/auth/magic-link/verify", () => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
emailChangeConfirmPath,
|
||||
emailChangeVerifyPath,
|
||||
inviteConfirmPath,
|
||||
inviteVerifyPath,
|
||||
magicLinkConfirmPath,
|
||||
magicLinkVerifyPath,
|
||||
} from "../../lib/create/confirmLinkPaths";
|
||||
|
||||
describe("confirmLinkPaths", () => {
|
||||
it("encodes tokens on confirm and verify URLs", () => {
|
||||
expect(magicLinkConfirmPath("a+b")).toBe("/login/confirm?token=a%2Bb");
|
||||
expect(magicLinkVerifyPath("a+b")).toBe(
|
||||
"/api/auth/magic-link/verify?token=a%2Bb",
|
||||
);
|
||||
expect(emailChangeConfirmPath("a+b")).toBe(
|
||||
"/email-change/confirm?token=a%2Bb",
|
||||
);
|
||||
expect(emailChangeVerifyPath("a+b")).toBe(
|
||||
"/api/user/email-change/verify?token=a%2Bb",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps copy=1 on invitation confirm and verify URLs", () => {
|
||||
expect(inviteConfirmPath("abc1234567", { copy: true })).toBe(
|
||||
"/invite/confirm?token=abc1234567©=1",
|
||||
);
|
||||
expect(inviteVerifyPath("abc1234567", { copy: true })).toBe(
|
||||
"/api/invites/rule-stakeholder/verify?token=abc1234567©=1",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,10 @@ describe("isChromelessNavigationPath", () => {
|
||||
["/create", true],
|
||||
["/create/completed", true],
|
||||
["/login", true],
|
||||
["/login/confirm", true],
|
||||
["/invite/confirm", true],
|
||||
["/invite/expired", true],
|
||||
["/email-change/confirm", true],
|
||||
["/invite/rule-1", true],
|
||||
["/use-cases/mutual-aid-colorado/rule", true],
|
||||
["/use-cases/food-not-bombs/rule/", true],
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readOneTimeLinkToken } from "../../lib/server/oneTimeLinkToken";
|
||||
|
||||
describe("readOneTimeLinkToken", () => {
|
||||
it("reads a token from the query string", async () => {
|
||||
const request = new NextRequest(
|
||||
"https://x.test/api/auth/magic-link/verify?token=abcdefghijkl",
|
||||
);
|
||||
await expect(readOneTimeLinkToken(request)).resolves.toBe("abcdefghijkl");
|
||||
});
|
||||
|
||||
it("returns null for a short query token", async () => {
|
||||
const request = new NextRequest(
|
||||
"https://x.test/api/auth/magic-link/verify?token=short",
|
||||
);
|
||||
await expect(readOneTimeLinkToken(request)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it("reads a token from a JSON body when the query is empty", async () => {
|
||||
const request = new NextRequest(
|
||||
"https://x.test/api/auth/magic-link/verify",
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ token: "abcdefghijkl" }),
|
||||
},
|
||||
);
|
||||
await expect(readOneTimeLinkToken(request)).resolves.toBe("abcdefghijkl");
|
||||
});
|
||||
});
|
||||
@@ -74,6 +74,41 @@ describe("POST /api/invites/rule-stakeholder/resend", () => {
|
||||
expect(sendInviteMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rotates the token and sends mail after the original link was used", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: "st-1",
|
||||
email: "inv@example.com",
|
||||
inviteTokenHash: "hash-old",
|
||||
inviteExpiresAt: null,
|
||||
acceptedAt: new Date(),
|
||||
invitedBy: { email: "owner@example.com" },
|
||||
rule: {
|
||||
title: "Juniper Street Food Share",
|
||||
summary: null,
|
||||
document: {},
|
||||
user: { email: "owner@example.com" },
|
||||
},
|
||||
});
|
||||
|
||||
const res = await POST(
|
||||
new NextRequest("https://x.test/api/invites/rule-stakeholder/resend", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: "a".repeat(12),
|
||||
email: "inv@example.com",
|
||||
}),
|
||||
headers: { "content-type": "application/json" },
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(sendInviteMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
to: "inv@example.com",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("rotates the token and sends mail when email matches", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: "st-1",
|
||||
|
||||
@@ -35,7 +35,7 @@ vi.mock("../../lib/server/session", () => ({
|
||||
setSessionCookie: (...args: unknown[]) => setCookieMock(...args),
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/invites/rule-stakeholder/verify/route";
|
||||
import { GET, POST } from "../../app/api/invites/rule-stakeholder/verify/route";
|
||||
|
||||
beforeEach(() => {
|
||||
findUniqueMock.mockReset();
|
||||
@@ -47,31 +47,29 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe("GET /api/invites/rule-stakeholder/verify", () => {
|
||||
it("redirects to the rule after a valid token", async () => {
|
||||
it("redirects a valid token to the confirm page without spending it", async () => {
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() + 60_000),
|
||||
acceptedAt: null,
|
||||
});
|
||||
upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" });
|
||||
updateMock.mockResolvedValue({});
|
||||
createSessionMock.mockResolvedValue({
|
||||
token: "sess",
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
const token = "x".repeat(12);
|
||||
|
||||
const res = await GET(
|
||||
new NextRequest(
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}`,
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${token}`,
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.status).toBeGreaterThanOrEqual(300);
|
||||
expect(res.status).toBeLessThan(400);
|
||||
expect(res.headers.get("location")).toContain("/invite/rule-1");
|
||||
expect(setCookieMock).toHaveBeenCalled();
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("/invite/confirm");
|
||||
expect(res.headers.get("location")).toContain(`token=${token}`);
|
||||
expect(upsertMock).not.toHaveBeenCalled();
|
||||
expect(updateMock).not.toHaveBeenCalled();
|
||||
expect(setCookieMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects to login when another user is already signed in", async () => {
|
||||
@@ -84,6 +82,7 @@ describe("GET /api/invites/rule-stakeholder/verify", () => {
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() + 60_000),
|
||||
acceptedAt: null,
|
||||
});
|
||||
|
||||
const res = await GET(
|
||||
@@ -105,6 +104,7 @@ describe("GET /api/invites/rule-stakeholder/verify", () => {
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() - 60_000),
|
||||
acceptedAt: null,
|
||||
});
|
||||
const token = "z".repeat(12);
|
||||
|
||||
@@ -119,19 +119,14 @@ describe("GET /api/invites/rule-stakeholder/verify", () => {
|
||||
expect(upsertMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps copy=1 on the invited view after a valid token", async () => {
|
||||
it("keeps copy=1 on the confirm page after a valid token", async () => {
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() + 60_000),
|
||||
});
|
||||
upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" });
|
||||
updateMock.mockResolvedValue({});
|
||||
createSessionMock.mockResolvedValue({
|
||||
token: "sess",
|
||||
expiresAt: new Date(),
|
||||
acceptedAt: null,
|
||||
});
|
||||
|
||||
const res = await GET(
|
||||
@@ -140,6 +135,117 @@ describe("GET /api/invites/rule-stakeholder/verify", () => {
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.headers.get("location")).toContain("/invite/confirm");
|
||||
expect(res.headers.get("location")).toContain("copy=1");
|
||||
expect(setCookieMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("opens the Rule when a used link is opened by the signed-in invitee", async () => {
|
||||
getSessionUserMock.mockResolvedValue({
|
||||
id: "u1",
|
||||
email: "inv@example.com",
|
||||
});
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: null,
|
||||
acceptedAt: new Date(),
|
||||
});
|
||||
|
||||
const res = await GET(
|
||||
new NextRequest(
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}`,
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.headers.get("location")).toContain("/invite/rule-1");
|
||||
expect(upsertMock).not.toHaveBeenCalled();
|
||||
expect(setCookieMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("sends a used link to the expired screen when the invitee is signed out", async () => {
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: null,
|
||||
acceptedAt: new Date(),
|
||||
});
|
||||
const token = "z".repeat(12);
|
||||
|
||||
const res = await GET(
|
||||
new NextRequest(
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${token}`,
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.headers.get("location")).toContain("/invite/expired");
|
||||
expect(res.headers.get("location")).toContain(`token=${token}`);
|
||||
expect(upsertMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/invites/rule-stakeholder/verify", () => {
|
||||
it("spends a valid token, keeps the hash, and redirects to the Rule", async () => {
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() + 60_000),
|
||||
acceptedAt: null,
|
||||
});
|
||||
upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" });
|
||||
updateMock.mockResolvedValue({});
|
||||
createSessionMock.mockResolvedValue({
|
||||
token: "sess",
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
|
||||
const res = await POST(
|
||||
new NextRequest(
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}`,
|
||||
{ method: "POST" },
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("/invite/rule-1");
|
||||
expect(setCookieMock).toHaveBeenCalled();
|
||||
expect(updateMock).toHaveBeenCalledWith({
|
||||
where: { id: "st1" },
|
||||
data: {
|
||||
userId: "u1",
|
||||
acceptedAt: expect.any(Date),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps copy=1 on the invited view after confirm", async () => {
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
findUniqueMock.mockResolvedValue({
|
||||
id: "st1",
|
||||
email: "inv@example.com",
|
||||
ruleId: "rule-1",
|
||||
inviteExpiresAt: new Date(Date.now() + 60_000),
|
||||
acceptedAt: null,
|
||||
});
|
||||
upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" });
|
||||
updateMock.mockResolvedValue({});
|
||||
createSessionMock.mockResolvedValue({
|
||||
token: "sess",
|
||||
expiresAt: new Date(),
|
||||
});
|
||||
|
||||
const res = await POST(
|
||||
new NextRequest(
|
||||
`https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}©=1`,
|
||||
{ method: "POST" },
|
||||
),
|
||||
);
|
||||
|
||||
expect(res.headers.get("location")).toContain("/invite/rule-1");
|
||||
expect(res.headers.get("location")).toContain("copy=1");
|
||||
});
|
||||
|
||||
@@ -192,8 +192,9 @@ describe("POST /api/rules/[id]/stakeholders/[stakeholderId]/resend", () => {
|
||||
findFirstStakeholderMock.mockResolvedValueOnce({
|
||||
id: "st-1",
|
||||
email: "inv@example.com",
|
||||
inviteTokenHash: null,
|
||||
inviteTokenHash: "kept-after-accept",
|
||||
inviteExpiresAt: null,
|
||||
acceptedAt: new Date("2026-01-03T00:00:00Z"),
|
||||
rule: { userId: owner.id, title: "My rule" },
|
||||
});
|
||||
const res = await POST_RESEND(
|
||||
@@ -214,6 +215,7 @@ describe("POST /api/rules/[id]/stakeholders/[stakeholderId]/resend", () => {
|
||||
email: "inv@example.com",
|
||||
inviteTokenHash: "old-hash",
|
||||
inviteExpiresAt: new Date("2026-01-01T00:00:00Z"),
|
||||
acceptedAt: null,
|
||||
rule: { userId: owner.id, title: "My rule" },
|
||||
});
|
||||
const res = await POST_RESEND(
|
||||
|
||||
@@ -61,7 +61,7 @@ describe("GET /api/rules/[id]/stakeholders", () => {
|
||||
email: "x@y.z",
|
||||
invitedAt: new Date("2026-01-02T00:00:00Z"),
|
||||
acceptedAt: new Date("2026-01-03T00:00:00Z"),
|
||||
inviteTokenHash: null,
|
||||
inviteTokenHash: "kept-after-accept",
|
||||
},
|
||||
]);
|
||||
const res = await GET(
|
||||
|
||||
@@ -36,7 +36,7 @@ vi.mock("../../lib/server/db", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/user/email-change/verify/route";
|
||||
import { GET, POST } from "../../app/api/user/email-change/verify/route";
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
@@ -103,8 +103,37 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("error=email_change_expired");
|
||||
expect(transactionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects a valid token to the confirm page without changing the address", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
emailChangeFindUniqueMock.mockResolvedValueOnce({
|
||||
id: "tok1",
|
||||
userId: "u1",
|
||||
newEmail: "new@x.com",
|
||||
expiresAt: new Date(Date.now() + 60_000),
|
||||
tokenHash: "token-hash",
|
||||
});
|
||||
const token = "long-enough-token";
|
||||
const res = await GET(getWithToken(token));
|
||||
expect(res.status).toBe(307);
|
||||
expect(res.headers.get("location")).toContain("/email-change/confirm");
|
||||
expect(res.headers.get("location")).toContain(encodeURIComponent(token));
|
||||
expect(transactionMock).not.toHaveBeenCalled();
|
||||
expect(getValidatedSessionTokenHashForUserMock).not.toHaveBeenCalled();
|
||||
expect(createSessionForUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/user/email-change/verify", () => {
|
||||
function postWithToken(token: string) {
|
||||
return new NextRequest(
|
||||
`https://x.test/api/user/email-change/verify?token=${encodeURIComponent(token)}`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
}
|
||||
|
||||
it("redirects with email_change_taken when another user claims the email in the transaction", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
const future = new Date(Date.now() + 60_000);
|
||||
@@ -137,8 +166,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
},
|
||||
);
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("error=email_change_taken");
|
||||
});
|
||||
|
||||
@@ -180,8 +209,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
},
|
||||
);
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("email_change=ok");
|
||||
expect(userUpdate).toHaveBeenCalledWith({
|
||||
where: { id: "u1" },
|
||||
@@ -233,8 +262,8 @@ describe("GET /api/user/email-change/verify", () => {
|
||||
expiresAt: new Date("2026-06-01T00:00:00Z"),
|
||||
});
|
||||
|
||||
const res = await GET(getWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(307);
|
||||
const res = await POST(postWithToken("long-enough-token"));
|
||||
expect(res.status).toBe(303);
|
||||
expect(res.headers.get("location")).toContain("email_change=ok");
|
||||
expect(sessionDeleteMany).toHaveBeenCalledWith({
|
||||
where: { userId: "u1" },
|
||||
|
||||
Reference in new issue
Block a user