File size: 11,270 Bytes
83e82db
 
 
 
 
 
 
 
2b62b0c
83e82db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b62b0c
83e82db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { describe, it, expect, beforeAll, afterAll, mock } from "bun:test";
import { drizzle } from "drizzle-orm/pglite";
import { eq } from "drizzle-orm";
import * as schema from "../../../db/src/schema";
import { getTestPGlite, closeTestPGlite } from "./test-setup";

// ── Mocks (must be declared before dynamic imports) ──────────────────────────

mock.module("@pram/env/server", () => ({
  env: {
    DATABASE_URL: "postgres://localhost:5432/test",
    BETTER_AUTH_SECRET: "a".repeat(32),
    BETTER_AUTH_URL: "http://localhost:3000",
    CORS_ORIGIN: "http://localhost:5173",
    API_KEY_ENCRYPTION_KEY: "z".repeat(32),
    REDIS_URL: "redis://localhost:6379",
    SMTP_HOST: "localhost",
    SMTP_USER: "test",
    SMTP_PASS: "test",
    SMTP_FROM: "test@test.com",
    FREE_CREDITS_ENABLED: false,
    DEFAULT_SIGNUP_CREDIT_TOKENS: 50_000,
    // CLOUDFLARE_TURNSTILE_SECRET_KEY intentionally omitted β€” Turnstile disabled
  },
}));

const mockCheckRateLimit = mock(async () => {});
mock.module("../lib/rate-limit", () => ({
  checkRateLimit: mockCheckRateLimit,
  checkRateLimitAllowed: mock(async () => true),
}));

const mockSendOtpEmail = mock(async (_opts: unknown) => {});
mock.module("../lib/email", () => ({
  sendOtpEmail: mockSendOtpEmail,
}));

mock.module("../lib/turnstile", () => ({
  validateTurnstileToken: mock(async () => {}),
}));

// ── Tests ─────────────────────────────────────────────────────────────────────

const TEST_IP = "203.0.113.42";
const EMAIL_UNVERIFIED = "unverified@example.com";
const EMAIL_VERIFIED = "verified@example.com";
const EMAIL_NONEXISTENT = "ghost@example.com";

describe("verification router", () => {
  let db: ReturnType<typeof drizzle>;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let verificationRouter: any;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  let caller: any;

  beforeAll(async () => {
    const pg = await getTestPGlite();
    db = drizzle(pg, { schema });

    mock.module("@pram/db", () => ({
      __esModule: true,
      ...schema,
      db,
    }));

    const mod = await import("../routers/verification");
    verificationRouter = mod.verificationRouter;
    caller = verificationRouter.createCaller({ session: null, auth: null, ip: TEST_IP });

    await db.insert(schema.user).values([
      { id: "v-user-1", name: "Unverified User", email: EMAIL_UNVERIFIED, emailVerified: false },
      { id: "v-user-2", name: "Verified User", email: EMAIL_VERIFIED, emailVerified: true },
    ]);

    // Seed account row so resetPassword can update the password hash
    await db.insert(schema.account).values({
      id: "v-account-1",
      accountId: "v-user-1",
      providerId: "credential",
      userId: "v-user-1",
      password: "hashed_old_password",
    });
  });

  afterAll(async () => {
    await closeTestPGlite();
  });

  // ── sendVerificationOtp ───────────────────────────────────────────────────

  describe("sendVerificationOtp", () => {
    it("sends email and returns generic success for unverified user", async () => {
      mockSendOtpEmail.mockClear();
      const result = await caller.sendVerificationOtp({ email: EMAIL_UNVERIFIED });
      expect(result.success).toBe(true);
      expect(mockSendOtpEmail).toHaveBeenCalledTimes(1);
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const callArg = (mockSendOtpEmail.mock.calls[0] as any[])[0] as { to: string; type: string; otp: string };
      expect(callArg.to).toBe(EMAIL_UNVERIFIED);
      expect(callArg.type).toBe("email-verification");
      expect(callArg.otp).toMatch(/^\d{6}$/);
    });

    it("silently returns generic response for non-existent email (no enumeration)", async () => {
      mockSendOtpEmail.mockClear();
      const result = await caller.sendVerificationOtp({ email: EMAIL_NONEXISTENT });
      expect(result.success).toBe(true);
      expect(mockSendOtpEmail).not.toHaveBeenCalled();
    });

    it("silently returns generic response for already-verified email", async () => {
      mockSendOtpEmail.mockClear();
      const result = await caller.sendVerificationOtp({ email: EMAIL_VERIFIED });
      expect(result.success).toBe(true);
      expect(mockSendOtpEmail).not.toHaveBeenCalled();
    });

    it("checks per-email and per-IP rate limits with correct keys", async () => {
      mockCheckRateLimit.mockClear();
      await caller.sendVerificationOtp({ email: EMAIL_UNVERIFIED });
      const keys = mockCheckRateLimit.mock.calls.map((c: unknown[]) => (c[0] as { key: string }).key);
      expect(keys).toContain(`otp-send:email:${EMAIL_UNVERIFIED}`);
      expect(keys).toContain(`otp-send:ip:${TEST_IP}:email`);
    });

    it("passes strict:true to checkRateLimit", async () => {
      mockCheckRateLimit.mockClear();
      await caller.sendVerificationOtp({ email: EMAIL_UNVERIFIED });
      const allStrict = mockCheckRateLimit.mock.calls.every(
        (c: unknown[]) => (c[0] as { strict?: boolean }).strict === true,
      );
      expect(allStrict).toBe(true);
    });
  });

  // ── verifyEmailOtp ────────────────────────────────────────────────────────

  describe("verifyEmailOtp", () => {
    it("verifies email and returns success with correct OTP", async () => {
      // Seed a fresh OTP directly into the verification table
      const otp = "123456";
      await db.delete(schema.verification).where(
        eq(schema.verification.identifier, `email-verification:${EMAIL_UNVERIFIED}`),
      );
      await db.insert(schema.verification).values({
        id: "v-otp-1",
        identifier: `email-verification:${EMAIL_UNVERIFIED}`,
        value: otp,
        expiresAt: new Date(Date.now() + 5 * 60 * 1000),
      });

      const result = await caller.verifyEmailOtp({ email: EMAIL_UNVERIFIED, otp });
      expect(result.success).toBe(true);

      // emailVerified should now be true in the DB
      const [updated] = await db
        .select({ emailVerified: schema.user.emailVerified })
        .from(schema.user)
        .where(eq(schema.user.email, EMAIL_UNVERIFIED));
      expect(updated?.emailVerified).toBe(true);

      // OTP record should be deleted after use
      const remaining = await db
        .select()
        .from(schema.verification)
        .where(eq(schema.verification.id, "v-otp-1"));
      expect(remaining).toHaveLength(0);
    });

    it("throws BAD_REQUEST for wrong OTP", async () => {
      await expect(
        caller.verifyEmailOtp({ email: EMAIL_UNVERIFIED, otp: "000000" }),
      ).rejects.toMatchObject({ code: "BAD_REQUEST" });
    });

    it("throws BAD_REQUEST for expired OTP", async () => {
      await db.delete(schema.verification).where(
        eq(schema.verification.identifier, `email-verification:${EMAIL_VERIFIED}`),
      );
      await db.insert(schema.verification).values({
        id: "v-otp-expired",
        identifier: `email-verification:${EMAIL_VERIFIED}`,
        value: "654321",
        expiresAt: new Date(Date.now() - 1000), // already expired
      });

      await expect(
        caller.verifyEmailOtp({ email: EMAIL_VERIFIED, otp: "654321" }),
      ).rejects.toMatchObject({ code: "BAD_REQUEST" });
    });
  });

  // ── sendPasswordResetOtp ──────────────────────────────────────────────────

  describe("sendPasswordResetOtp", () => {
    it("sends reset email for registered user", async () => {
      mockSendOtpEmail.mockClear();
      const result = await caller.sendPasswordResetOtp({ email: EMAIL_UNVERIFIED });
      expect(result.success).toBe(true);
      expect(mockSendOtpEmail).toHaveBeenCalledTimes(1);
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const callArg = (mockSendOtpEmail.mock.calls[0] as any[])[0] as { to: string; type: string };
      expect(callArg.to).toBe(EMAIL_UNVERIFIED);
      expect(callArg.type).toBe("forget-password");
    });

    it("silently returns generic response for non-existent email", async () => {
      mockSendOtpEmail.mockClear();
      const result = await caller.sendPasswordResetOtp({ email: EMAIL_NONEXISTENT });
      expect(result.success).toBe(true);
      expect(mockSendOtpEmail).not.toHaveBeenCalled();
    });

    it("checks per-email and per-IP rate limits for reset", async () => {
      mockCheckRateLimit.mockClear();
      await caller.sendPasswordResetOtp({ email: EMAIL_UNVERIFIED });
      const keys = mockCheckRateLimit.mock.calls.map((c: unknown[]) => (c[0] as { key: string }).key);
      expect(keys).toContain(`otp-send:email:${EMAIL_UNVERIFIED}`);
      expect(keys).toContain(`otp-send:ip:${TEST_IP}:reset`);
    });
  });

  // ── resetPassword ─────────────────────────────────────────────────────────

  describe("resetPassword", () => {
    it("resets password with valid OTP", async () => {
      const otp = "789012";
      await db.delete(schema.verification).where(
        eq(schema.verification.identifier, `forget-password:${EMAIL_UNVERIFIED}`),
      );
      await db.insert(schema.verification).values({
        id: "v-reset-1",
        identifier: `forget-password:${EMAIL_UNVERIFIED}`,
        value: otp,
        expiresAt: new Date(Date.now() + 5 * 60 * 1000),
      });

      const result = await caller.resetPassword({
        email: EMAIL_UNVERIFIED,
        otp,
        newPassword: "NewPass123",
      });
      expect(result.success).toBe(true);

      // OTP record should be deleted
      const remaining = await db
        .select()
        .from(schema.verification)
        .where(eq(schema.verification.id, "v-reset-1"));
      expect(remaining).toHaveLength(0);
    });

    it("throws BAD_REQUEST for invalid OTP", async () => {
      await expect(
        caller.resetPassword({ email: EMAIL_UNVERIFIED, otp: "000000", newPassword: "NewPass123" }),
      ).rejects.toMatchObject({ code: "BAD_REQUEST" });
    });
  });

  // ── rate limit propagation ─────────────────────────────────────────────────

  describe("rate limit enforcement", () => {
    it("throws TOO_MANY_REQUESTS when checkRateLimit throws", async () => {
      const { TRPCError } = await import("@trpc/server");
      mockCheckRateLimit.mockImplementation(async () => {
        throw new TRPCError({ code: "TOO_MANY_REQUESTS", message: "Too many requests. Please try again later." });
      });

      await expect(
        caller.sendVerificationOtp({ email: EMAIL_UNVERIFIED }),
      ).rejects.toMatchObject({ code: "TOO_MANY_REQUESTS" });

      // Restore no-op
      mockCheckRateLimit.mockImplementation(async () => {});
    });
  });
});