|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +// @vitest-environment node |
| 6 | + |
| 7 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 8 | +import type { NextRequest } from "next/server"; |
| 9 | + |
| 10 | +vi.mock("../../../../../db/tables/email_subscriptions", () => ({ |
| 11 | + getEmailSubscriptionByToken: vi.fn(), |
| 12 | + unsubscribeEmailSubscription: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../../../../functions/server/logging", () => ({ |
| 16 | + logger: { info: vi.fn(), error: vi.fn() }, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("@sentry/nextjs", () => ({ |
| 20 | + captureException: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +import { |
| 24 | + getEmailSubscriptionByToken, |
| 25 | + unsubscribeEmailSubscription, |
| 26 | +} from "../../../../../db/tables/email_subscriptions"; |
| 27 | +import { EmailSubscriptionsRow } from "knex/types/tables"; |
| 28 | +import { BREACH_ALERT_LIST_ID } from "../../../../../constants"; |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | +}); |
| 33 | + |
| 34 | +const token = "valid-token-abc"; |
| 35 | +const mockSubscription: EmailSubscriptionsRow = { |
| 36 | + id: "1", |
| 37 | + subscriber_id: 1, |
| 38 | + token, |
| 39 | + list_id: BREACH_ALERT_LIST_ID, |
| 40 | + subscribed: true, |
| 41 | + updated_at: new Date(), |
| 42 | +}; |
| 43 | + |
| 44 | +function makeReq( |
| 45 | + url: string, |
| 46 | + { |
| 47 | + contentType, |
| 48 | + formFields, |
| 49 | + }: { contentType?: string; formFields?: Record<string, string> } = {}, |
| 50 | +): NextRequest { |
| 51 | + const fd = new FormData(); |
| 52 | + for (const [key, value] of Object.entries(formFields ?? {})) { |
| 53 | + fd.append(key, value); |
| 54 | + } |
| 55 | + return { |
| 56 | + url, |
| 57 | + headers: { |
| 58 | + get: (name: string) => |
| 59 | + name === "content-type" ? (contentType ?? null) : null, |
| 60 | + }, |
| 61 | + formData: () => Promise.resolve(fd), |
| 62 | + } as unknown as NextRequest; |
| 63 | +} |
| 64 | + |
| 65 | +describe("POST /api/v1/user/one-click-unsubscribe", () => { |
| 66 | + it("returns 400 when no token is provided", async () => { |
| 67 | + const { POST } = await import("./route"); |
| 68 | + |
| 69 | + const req = makeReq( |
| 70 | + "https://example.com/api/v1/user/one-click-unsubscribe", |
| 71 | + ); |
| 72 | + |
| 73 | + const res = await POST(req); |
| 74 | + expect(res.status).toBe(400); |
| 75 | + const body = await res.json(); |
| 76 | + expect(body.success).toBe(false); |
| 77 | + expect(body.message).toMatch(/token/i); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns 400 when content-type is form-urlencoded but List-Unsubscribe field is missing", async () => { |
| 81 | + const { POST } = await import("./route"); |
| 82 | + |
| 83 | + const req = makeReq( |
| 84 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 85 | + { |
| 86 | + contentType: "application/x-www-form-urlencoded", |
| 87 | + formFields: { some: "other" }, |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + const res = await POST(req); |
| 92 | + expect(res.status).toBe(400); |
| 93 | + const body = await res.json(); |
| 94 | + expect(body.success).toBe(false); |
| 95 | + expect(body.message).toMatch(/List-Unsubscribe=One-Click/); |
| 96 | + }); |
| 97 | + |
| 98 | + it("returns 200 when content-type is form-urlencoded and List-Unsubscribe=One-Click", async () => { |
| 99 | + vi.mocked(getEmailSubscriptionByToken).mockResolvedValue(mockSubscription); |
| 100 | + vi.mocked(unsubscribeEmailSubscription).mockResolvedValue(undefined); |
| 101 | + |
| 102 | + const { POST } = await import("./route"); |
| 103 | + |
| 104 | + const req = makeReq( |
| 105 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 106 | + { |
| 107 | + contentType: "application/x-www-form-urlencoded", |
| 108 | + formFields: { "List-Unsubscribe": "One-Click" }, |
| 109 | + }, |
| 110 | + ); |
| 111 | + |
| 112 | + const res = await POST(req); |
| 113 | + expect(res.status).toBe(200); |
| 114 | + const body = await res.json(); |
| 115 | + expect(body.success).toBe(true); |
| 116 | + expect(unsubscribeEmailSubscription).toHaveBeenCalledWith( |
| 117 | + mockSubscription, |
| 118 | + "one-click", |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it("returns 400 when content-type is multipart/form-data but List-Unsubscribe field is missing", async () => { |
| 123 | + const { POST } = await import("./route"); |
| 124 | + |
| 125 | + const req = makeReq( |
| 126 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 127 | + { |
| 128 | + contentType: "multipart/form-data; boundary=----boundary", |
| 129 | + formFields: { some: "other" }, |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + const res = await POST(req); |
| 134 | + expect(res.status).toBe(400); |
| 135 | + const body = await res.json(); |
| 136 | + expect(body.success).toBe(false); |
| 137 | + expect(body.message).toMatch(/List-Unsubscribe=One-Click/); |
| 138 | + }); |
| 139 | + |
| 140 | + it("returns 200 when content-type is multipart/form-data and List-Unsubscribe=One-Click", async () => { |
| 141 | + vi.mocked(getEmailSubscriptionByToken).mockResolvedValue(mockSubscription); |
| 142 | + vi.mocked(unsubscribeEmailSubscription).mockResolvedValue(undefined); |
| 143 | + |
| 144 | + const { POST } = await import("./route"); |
| 145 | + |
| 146 | + const req = makeReq( |
| 147 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 148 | + { |
| 149 | + contentType: "multipart/form-data; boundary=----boundary", |
| 150 | + formFields: { "List-Unsubscribe": "One-Click" }, |
| 151 | + }, |
| 152 | + ); |
| 153 | + |
| 154 | + const res = await POST(req); |
| 155 | + expect(res.status).toBe(200); |
| 156 | + const body = await res.json(); |
| 157 | + expect(body.success).toBe(true); |
| 158 | + expect(unsubscribeEmailSubscription).toHaveBeenCalledWith( |
| 159 | + mockSubscription, |
| 160 | + "one-click", |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it("returns 200 if token does not map to a subscription", async () => { |
| 165 | + vi.mocked(getEmailSubscriptionByToken).mockResolvedValue(undefined); |
| 166 | + |
| 167 | + const { POST } = await import("./route"); |
| 168 | + |
| 169 | + const req = makeReq( |
| 170 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 171 | + ); |
| 172 | + |
| 173 | + const res = await POST(req); |
| 174 | + expect(res.status).toBe(200); |
| 175 | + const body = await res.json(); |
| 176 | + expect(body.success).toBe(true); |
| 177 | + expect(getEmailSubscriptionByToken).toHaveBeenCalledWith(token); |
| 178 | + expect(unsubscribeEmailSubscription).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it("returns 200 and unsubscribes with source 'one-click' on success", async () => { |
| 182 | + vi.mocked(getEmailSubscriptionByToken).mockResolvedValue(mockSubscription); |
| 183 | + vi.mocked(unsubscribeEmailSubscription).mockResolvedValue(undefined); |
| 184 | + |
| 185 | + const { POST } = await import("./route"); |
| 186 | + |
| 187 | + const req = makeReq( |
| 188 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 189 | + ); |
| 190 | + |
| 191 | + const res = await POST(req); |
| 192 | + expect(res.status).toBe(200); |
| 193 | + const body = await res.json(); |
| 194 | + expect(body.success).toBe(true); |
| 195 | + expect(unsubscribeEmailSubscription).toHaveBeenCalledWith( |
| 196 | + mockSubscription, |
| 197 | + "one-click", |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it("returns 500 when unsubscribeEmailSubscription throws", async () => { |
| 202 | + vi.mocked(getEmailSubscriptionByToken).mockResolvedValue(mockSubscription); |
| 203 | + vi.mocked(unsubscribeEmailSubscription).mockRejectedValue( |
| 204 | + new Error("db failure"), |
| 205 | + ); |
| 206 | + |
| 207 | + const { POST } = await import("./route"); |
| 208 | + |
| 209 | + const req = makeReq( |
| 210 | + `https://example.com/api/v1/user/one-click-unsubscribe?token=${token}`, |
| 211 | + ); |
| 212 | + |
| 213 | + const res = await POST(req); |
| 214 | + expect(res.status).toBe(500); |
| 215 | + const body = await res.json(); |
| 216 | + expect(body.success).toBe(false); |
| 217 | + }); |
| 218 | +}); |
0 commit comments