@@ -17,7 +17,7 @@ vi.mock('@/lib/core/config/redis', async (importOriginal) => ({
1717 getRedisClient : ( ) => redisClient . current ,
1818} ) )
1919
20- import { MAX_OTP_ATTEMPTS , storeOTP } from '@/lib/core/security/otp'
20+ import { MAX_OTP_ATTEMPTS , storeOTP , verifyOTP } from '@/lib/core/security/otp'
2121import { PUT } from '@/app/api/chat/[identifier]/otp/route'
2222
2323const redisUrl = readTestRedisUrl ( )
@@ -29,15 +29,14 @@ const identifier = `otp-${chatId}`
2929const email = `${ generateId ( ) } @otp.invalid`
3030const CODE = '246810'
3131const NEWER_CODE = '135791'
32- const NO_CODE = 'No verification code found, request a new one'
33- const INVALID = 'Invalid verification code'
34- const LOCKED = 'Too many failed attempts. Please request a new code.'
32+ const FAILED = 'Invalid or expired verification code'
33+ const REJECTED = { status : 400 , error : FAILED , cookie : null }
3534
36- async function verify ( otp : string ) {
35+ async function verify ( otp : string , address = email ) {
3736 const request = createMockRequest ( {
3837 method : 'PUT' ,
3938 url : `/api/chat/${ identifier } /otp` ,
40- body : { email, otp } ,
39+ body : { email : address , otp } ,
4140 } )
4241 const response = await PUT ( request , createRouteContext ( { identifier } ) )
4342 const body = ( await response . json ( ) ) as { error ?: string ; message ?: string }
@@ -111,39 +110,50 @@ describe('chat OTP verification', () => {
111110 . where ( eq ( chat . id , chatId ) )
112111 } )
113112
114- it . each ( [
115- {
116- rejection : 'the chat no longer uses email auth' ,
117- change : { authType : 'public' } ,
118- response : { status : 400 , error : 'This chat does not use email authentication' } ,
119- } ,
120- {
121- rejection : 'the email is no longer allowed' ,
122- change : { allowedEmails : [ 'someone-else@otp.invalid' ] } ,
123- response : { status : 403 , error : 'Email not authorized' } ,
124- } ,
125- ] ) ( 'rejects without spending the code when $rejection' , async ( { change, response } ) => {
126- await db . update ( chat ) . set ( change ) . where ( eq ( chat . id , chatId ) )
113+ it ( 'rejects without spending the code when the chat no longer uses email auth' , async ( ) => {
114+ await db . update ( chat ) . set ( { authType : 'public' } ) . where ( eq ( chat . id , chatId ) )
127115 for ( let attempt = 0 ; attempt < MAX_OTP_ATTEMPTS ; attempt ++ ) {
128- expect ( await verify ( CODE ) ) . toMatchObject ( { ...response , cookie : null } )
116+ expect ( await verify ( CODE ) ) . toMatchObject ( {
117+ status : 400 ,
118+ error : 'This chat does not use email authentication' ,
119+ cookie : null ,
120+ } )
129121 }
122+ await db . update ( chat ) . set ( { authType : 'email' } ) . where ( eq ( chat . id , chatId ) )
123+ expect ( await verify ( CODE ) ) . toMatchObject ( { status : 200 } )
124+ } )
125+
126+ it ( 'spends but never accepts a correct code once the email leaves the allow-list' , async ( ) => {
130127 await db
131128 . update ( chat )
132- . set ( { authType : 'email' , allowedEmails : [ email ] } )
129+ . set ( { allowedEmails : [ 'someone-else@otp.invalid' ] } )
133130 . where ( eq ( chat . id , chatId ) )
134- expect ( await verify ( CODE ) ) . toMatchObject ( { status : 200 } )
131+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
132+ await db
133+ . update ( chat )
134+ . set ( { allowedEmails : [ email ] } )
135+ . where ( eq ( chat . id , chatId ) )
136+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
135137 } )
136138
137139 it ( 'compares at most the attempt limit of guesses however many arrive concurrently' , async ( ) => {
138- const results = await Promise . all ( wrongGuesses ( 30 ) . map ( verify ) )
139-
140- const compared = results . filter ( ( result ) => result . error !== NO_CODE )
141- expect ( compared ) . toHaveLength ( MAX_OTP_ATTEMPTS )
142- expect ( compared . filter ( ( result ) => result . error === INVALID ) ) . toHaveLength (
143- MAX_OTP_ATTEMPTS - 1
140+ const results = await Promise . all (
141+ wrongGuesses ( 30 ) . map ( ( guess ) => verifyOTP ( 'chat' , chatId , email , guess ) )
144142 )
145- expect ( compared . filter ( ( result ) => result . error === LOCKED ) ) . toHaveLength ( 1 )
146- expect ( await verify ( CODE ) ) . toMatchObject ( { status : 400 , error : NO_CODE , cookie : null } )
143+
144+ expect ( results . filter ( ( result ) => result === 'invalid' ) ) . toHaveLength ( MAX_OTP_ATTEMPTS - 1 )
145+ expect ( results . filter ( ( result ) => result === 'locked' ) ) . toHaveLength ( 1 )
146+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
147+ } )
148+
149+ it ( 'answers an allow-listed email exactly like an unlisted one on every failure' , async ( ) => {
150+ const unlisted = `${ generateId ( ) } @elsewhere.invalid`
151+ const unlistedAnswer = await verify ( '000000' , unlisted )
152+
153+ expect ( unlistedAnswer ) . toEqual ( REJECTED )
154+ for ( const guess of wrongGuesses ( MAX_OTP_ATTEMPTS + 1 ) ) {
155+ expect ( await verify ( guess ) ) . toEqual ( unlistedAnswer )
156+ }
147157 } )
148158
149159 it ( 'lets only one of several concurrent correct submissions succeed' , async ( ) => {
@@ -167,31 +177,31 @@ describe('chat OTP verification', () => {
167177 return evaluate ( ...args )
168178 } )
169179
170- expect ( await verify ( CODE ) ) . toMatchObject ( { status : 400 , error : NO_CODE , cookie : null } )
180+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
171181 expect ( await verify ( NEWER_CODE ) ) . toMatchObject ( { status : 200 } )
172182 } )
173183
174184 it ( 'treats a code with no expiry as expired' , async ( ) => {
175185 const key = `otp:${ email } :${ chatId } `
176186 await redisClient . current ?. set ( key , `${ CODE } :${ MAX_OTP_ATTEMPTS - 1 } ` )
177- expect ( await verify ( CODE ) ) . toMatchObject ( { status : 400 , error : NO_CODE , cookie : null } )
187+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
178188 expect ( await redisClient . current ?. exists ( key ) ) . toBe ( 0 )
179189 } )
180190
181191 it ( 'accepts the correct code on the last remaining attempt and mints the auth cookie' , async ( ) => {
182192 for ( const guess of wrongGuesses ( MAX_OTP_ATTEMPTS - 1 ) ) {
183- expect ( await verify ( guess ) ) . toMatchObject ( { status : 400 , error : INVALID } )
193+ expect ( await verify ( guess ) ) . toMatchObject ( REJECTED )
184194 }
185195 const accepted = await verify ( CODE )
186196 expect ( accepted . status ) . toBe ( 200 )
187197 expect ( accepted . cookie ) . toContain ( `chat_auth_${ chatId } =` )
188- expect ( await verify ( CODE ) ) . toMatchObject ( { status : 400 , error : NO_CODE } )
198+ expect ( await verify ( CODE ) ) . toMatchObject ( REJECTED )
189199 } )
190200
191201 it ( 'keeps the code expiry when a failed attempt is recorded' , async ( ) => {
192202 const key = `otp:${ email } :${ chatId } `
193203 await redisClient . current ?. pexpire ( key , 900 )
194- expect ( await verify ( '999999' ) ) . toMatchObject ( { status : 400 , error : INVALID } )
204+ expect ( await verify ( '999999' ) ) . toMatchObject ( REJECTED )
195205 const ttl = await redisClient . current ?. pttl ( key )
196206 expect ( ttl ) . toBeGreaterThan ( 0 )
197207 expect ( ttl ) . toBeLessThanOrEqual ( 900 )
0 commit comments