|
| 1 | +/** |
| 2 | + * Tests for logOtpVerificationFailure — the bridge that surfaces better-auth's |
| 3 | + * 4xx OTP verification errors ("OTP expired" vs "Invalid OTP" vs "Too many |
| 4 | + * attempts") in our pino logs, with the user's email attached. |
| 5 | + * |
| 6 | + * These are exactly the errors better-auth throws from its email-otp verify |
| 7 | + * endpoint (better-auth 1.4.18 dist/plugins/email-otp/routes.mjs), which the |
| 8 | + * browser posts to directly and which were previously logged nowhere. |
| 9 | + */ |
| 10 | +import { describe, it, expect, vi } from 'vitest' |
| 11 | +import { APIError } from 'better-auth/api' |
| 12 | +import { isOtpVerifyPath, logOtpVerificationFailure } from '../better-auth.js' |
| 13 | + |
| 14 | +const PATH = '/sign-in/email-otp' |
| 15 | + |
| 16 | +/** Minimal stand-in for the pino logger — only `info` and `warn` are used. */ |
| 17 | +function makeLogger() { |
| 18 | + return { info: vi.fn(), warn: vi.fn() } as unknown as Parameters< |
| 19 | + typeof logOtpVerificationFailure |
| 20 | + >[3] |
| 21 | +} |
| 22 | + |
| 23 | +describe('logOtpVerificationFailure', () => { |
| 24 | + it('maps an expired-OTP error to a self-contained message with the email', () => { |
| 25 | + const log = makeLogger() |
| 26 | + const error = new APIError('BAD_REQUEST', { message: 'OTP expired' }) |
| 27 | + logOtpVerificationFailure(error, 'alice@example.com', PATH, log) |
| 28 | + |
| 29 | + expect(log.info).toHaveBeenCalledOnce() |
| 30 | + expect(log.info).toHaveBeenCalledWith( |
| 31 | + { email: 'alice@example.com', statusCode: 400, path: PATH }, |
| 32 | + 'OTP verification failed: code expired', |
| 33 | + ) |
| 34 | + expect(log.warn).not.toHaveBeenCalled() |
| 35 | + }) |
| 36 | + |
| 37 | + it('maps an invalid-OTP error, distinct from expiry', () => { |
| 38 | + const log = makeLogger() |
| 39 | + logOtpVerificationFailure( |
| 40 | + new APIError('BAD_REQUEST', { message: 'Invalid OTP' }), |
| 41 | + 'bob@example.com', |
| 42 | + PATH, |
| 43 | + log, |
| 44 | + ) |
| 45 | + |
| 46 | + expect(log.info).toHaveBeenCalledWith( |
| 47 | + { email: 'bob@example.com', statusCode: 400, path: PATH }, |
| 48 | + 'OTP verification failed: invalid or unrecognized code', |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + it('logs a 403 too-many-attempts error at warn (possible abuse signal)', () => { |
| 53 | + const log = makeLogger() |
| 54 | + logOtpVerificationFailure( |
| 55 | + new APIError('FORBIDDEN', { message: 'Too many attempts' }), |
| 56 | + 'carol@example.com', |
| 57 | + PATH, |
| 58 | + log, |
| 59 | + ) |
| 60 | + |
| 61 | + expect(log.warn).toHaveBeenCalledWith( |
| 62 | + { email: 'carol@example.com', statusCode: 403, path: PATH }, |
| 63 | + 'OTP verification failed: too many attempts, code invalidated', |
| 64 | + ) |
| 65 | + expect(log.info).not.toHaveBeenCalled() |
| 66 | + }) |
| 67 | + |
| 68 | + it('logs the broad-scope path verbatim in the path field', () => { |
| 69 | + const log = makeLogger() |
| 70 | + logOtpVerificationFailure( |
| 71 | + new APIError('BAD_REQUEST', { message: 'Invalid OTP' }), |
| 72 | + 'dave@example.com', |
| 73 | + '/email-otp/reset-password', |
| 74 | + log, |
| 75 | + ) |
| 76 | + |
| 77 | + expect(log.info).toHaveBeenCalledWith( |
| 78 | + expect.objectContaining({ path: '/email-otp/reset-password' }), |
| 79 | + 'OTP verification failed: invalid or unrecognized code', |
| 80 | + ) |
| 81 | + }) |
| 82 | + |
| 83 | + it('falls back to the prefixed reason for an unmapped 4xx reason', () => { |
| 84 | + const log = makeLogger() |
| 85 | + logOtpVerificationFailure( |
| 86 | + new APIError('BAD_REQUEST', { message: 'Some other reason' }), |
| 87 | + 'eve@example.com', |
| 88 | + PATH, |
| 89 | + log, |
| 90 | + ) |
| 91 | + |
| 92 | + expect(log.info).toHaveBeenCalledWith( |
| 93 | + expect.objectContaining({ email: 'eve@example.com', statusCode: 400 }), |
| 94 | + 'OTP verification failed: Some other reason', |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it('logs email as undefined when the body had no email', () => { |
| 99 | + const log = makeLogger() |
| 100 | + logOtpVerificationFailure( |
| 101 | + new APIError('BAD_REQUEST', { message: 'Invalid OTP' }), |
| 102 | + undefined, |
| 103 | + PATH, |
| 104 | + log, |
| 105 | + ) |
| 106 | + |
| 107 | + expect(log.info).toHaveBeenCalledWith( |
| 108 | + { email: undefined, statusCode: 400, path: PATH }, |
| 109 | + 'OTP verification failed: invalid or unrecognized code', |
| 110 | + ) |
| 111 | + }) |
| 112 | + |
| 113 | + it('ignores 5xx errors (already logged by better-auth itself)', () => { |
| 114 | + const log = makeLogger() |
| 115 | + logOtpVerificationFailure( |
| 116 | + new APIError('INTERNAL_SERVER_ERROR', { message: 'boom' }), |
| 117 | + 'alice@example.com', |
| 118 | + PATH, |
| 119 | + log, |
| 120 | + ) |
| 121 | + |
| 122 | + expect(log.warn).not.toHaveBeenCalled() |
| 123 | + expect(log.info).not.toHaveBeenCalled() |
| 124 | + }) |
| 125 | + |
| 126 | + it('ignores 3xx redirects', () => { |
| 127 | + const log = makeLogger() |
| 128 | + logOtpVerificationFailure( |
| 129 | + new APIError('FOUND'), |
| 130 | + 'alice@example.com', |
| 131 | + PATH, |
| 132 | + log, |
| 133 | + ) |
| 134 | + |
| 135 | + expect(log.warn).not.toHaveBeenCalled() |
| 136 | + expect(log.info).not.toHaveBeenCalled() |
| 137 | + }) |
| 138 | + |
| 139 | + it('ignores non-APIError values (e.g. a successful response)', () => { |
| 140 | + const log = makeLogger() |
| 141 | + logOtpVerificationFailure( |
| 142 | + new Error('some other failure'), |
| 143 | + 'a@x.com', |
| 144 | + PATH, |
| 145 | + log, |
| 146 | + ) |
| 147 | + logOtpVerificationFailure({ token: 'ok' }, 'a@x.com', PATH, log) |
| 148 | + logOtpVerificationFailure(undefined, 'a@x.com', PATH, log) |
| 149 | + |
| 150 | + expect(log.warn).not.toHaveBeenCalled() |
| 151 | + expect(log.info).not.toHaveBeenCalled() |
| 152 | + }) |
| 153 | +}) |
| 154 | + |
| 155 | +describe('isOtpVerifyPath', () => { |
| 156 | + it('matches the OTP verification endpoints', () => { |
| 157 | + expect(isOtpVerifyPath('/sign-in/email-otp')).toBe(true) |
| 158 | + expect(isOtpVerifyPath('/email-otp/check-verification-otp')).toBe(true) |
| 159 | + expect(isOtpVerifyPath('/email-otp/verify-email')).toBe(true) |
| 160 | + expect(isOtpVerifyPath('/email-otp/reset-password')).toBe(true) |
| 161 | + }) |
| 162 | + |
| 163 | + it('excludes the OTP send endpoints (a send failure is not a verification failure)', () => { |
| 164 | + expect(isOtpVerifyPath('/email-otp/send-verification-otp')).toBe(false) |
| 165 | + expect(isOtpVerifyPath('/email-otp/request-password-reset')).toBe(false) |
| 166 | + }) |
| 167 | + |
| 168 | + it('excludes unrelated endpoints', () => { |
| 169 | + expect(isOtpVerifyPath('/sign-in/social')).toBe(false) |
| 170 | + expect(isOtpVerifyPath('/callback/google')).toBe(false) |
| 171 | + }) |
| 172 | +}) |
0 commit comments