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:
adilallo committed 2026-11-22 16:58:30 -07:00
1 parent 600056499c
commit a725cf1a32
33 files changed
+1002 -89

No files matched your search

@@ -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)}&copy=1`,
{ method: "POST" },
),
);
expect(res.headers.get("location")).toContain("/invite/rule-1");
expect(res.headers.get("location")).toContain("copy=1");
});