@@ -8,7 +8,7 @@ import { db } from '@acme/db'
88
99import * as mailService from '../../mail/mail.service'
1010import { emailLogin , emailVerifyOtp } from '../auth.service'
11- import { createAuthToken } from '../auth.utils'
11+ import { createAuthToken , createVfnIdentifier } from '../auth.utils'
1212
1313const mockedMailService = mock ( mailService )
1414
@@ -30,9 +30,10 @@ describe('auth.service', () => {
3030 otpPrefix : expect . any ( String ) ,
3131 } )
3232
33- // Verify token was created in database with nonce as identifier
33+ // Verify token was created in database with vfnIdentifier
34+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
3435 const token = await db . verificationToken . findUnique ( {
35- where : { identifier : nonce } ,
36+ where : { identifier : vfnIdentifier } ,
3637 } )
3738 expect ( token ) . toBeDefined ( )
3839 expect ( mockedMailService . sendMail ) . toHaveBeenCalledWith ( {
@@ -49,17 +50,18 @@ describe('auth.service', () => {
4950 // First login
5051 await emailLogin ( { email, nonce } )
5152
53+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
5254 // Simulate failed attempts
5355 await db . verificationToken . update ( {
54- where : { identifier : nonce } ,
56+ where : { identifier : vfnIdentifier } ,
5557 data : { attempts : 3 } ,
5658 } )
5759
5860 // Second login should reset attempts
5961 await emailLogin ( { email, nonce } )
6062
6163 const token = await db . verificationToken . findUnique ( {
62- where : { identifier : nonce } ,
64+ where : { identifier : vfnIdentifier } ,
6365 } )
6466 expect ( token ?. attempts ) . toBe ( 0 )
6567 } )
@@ -71,9 +73,10 @@ describe('auth.service', () => {
7173 await emailLogin ( { email, nonce } )
7274 await emailLogin ( { email, nonce } )
7375
76+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
7477 // Should only have one record
7578 const tokens = await db . verificationToken . findMany ( {
76- where : { identifier : nonce } ,
79+ where : { identifier : vfnIdentifier } ,
7780 } )
7881 expect ( tokens ) . toHaveLength ( 1 )
7982 } )
@@ -87,11 +90,13 @@ describe('auth.service', () => {
8790 await emailLogin ( { email, nonce : nonce2 } )
8891
8992 // Should have two records with different nonces
93+ const vfnIdentifier1 = createVfnIdentifier ( { email, nonce : nonce1 } )
94+ const vfnIdentifier2 = createVfnIdentifier ( { email, nonce : nonce2 } )
9095 const token1 = await db . verificationToken . findUnique ( {
91- where : { identifier : nonce1 } ,
96+ where : { identifier : vfnIdentifier1 } ,
9297 } )
9398 const token2 = await db . verificationToken . findUnique ( {
94- where : { identifier : nonce2 } ,
99+ where : { identifier : vfnIdentifier2 } ,
95100 } )
96101
97102 expect ( token1 ) . toBeDefined ( )
@@ -114,8 +119,9 @@ describe('auth.service', () => {
114119 ) . resolves . not . toThrow ( )
115120
116121 // Token should be deleted after successful verification
122+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
117123 const verificationToken = await db . verificationToken . findUnique ( {
118- where : { identifier : nonce } ,
124+ where : { identifier : vfnIdentifier } ,
119125 } )
120126 expect ( verificationToken ) . toBeNull ( )
121127 } )
@@ -137,11 +143,12 @@ describe('auth.service', () => {
137143
138144 const { token, hashedToken } = createAuthToken ( { email, nonce } )
139145
146+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
140147 // Create a verification token with an old issuedAt date
141148 const oldDate = add ( new Date ( ) , { seconds : - 700 } ) // 700 seconds ago (beyond 600s expiry)
142149 await db . verificationToken . create ( {
143150 data : {
144- identifier : nonce ,
151+ identifier : vfnIdentifier ,
145152 token : hashedToken ,
146153 issuedAt : oldDate ,
147154 } ,
@@ -159,17 +166,18 @@ describe('auth.service', () => {
159166
160167 await emailLogin ( { email, nonce } )
161168
169+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
162170 // First attempt
163171 await expect ( emailVerifyOtp ( { email, token, nonce } ) ) . rejects . toThrow ( )
164172 let verificationToken = await db . verificationToken . findUnique ( {
165- where : { identifier : nonce } ,
173+ where : { identifier : vfnIdentifier } ,
166174 } )
167175 expect ( verificationToken ?. attempts ) . toBe ( 1 )
168176
169177 // Second attempt
170178 await expect ( emailVerifyOtp ( { email, token, nonce } ) ) . rejects . toThrow ( )
171179 verificationToken = await db . verificationToken . findUnique ( {
172- where : { identifier : nonce } ,
180+ where : { identifier : vfnIdentifier } ,
173181 } )
174182 expect ( verificationToken ?. attempts ) . toBe ( 2 )
175183 } )
@@ -210,8 +218,9 @@ describe('auth.service', () => {
210218 await emailVerifyOtp ( { email, token, nonce } )
211219
212220 // Token should be deleted
221+ const vfnIdentifier = createVfnIdentifier ( { email, nonce } )
213222 const verificationToken = await db . verificationToken . findUnique ( {
214- where : { identifier : nonce } ,
223+ where : { identifier : vfnIdentifier } ,
215224 } )
216225 expect ( verificationToken ) . toBeNull ( )
217226 } )
@@ -245,8 +254,9 @@ describe('auth.service', () => {
245254 ) . rejects . toThrow ( 'Invalid login email or missing nonce' )
246255
247256 // Original token should still exist
257+ const vfnIdentifier1 = createVfnIdentifier ( { email, nonce : nonce1 } )
248258 const verificationToken = await db . verificationToken . findUnique ( {
249- where : { identifier : nonce1 } ,
259+ where : { identifier : vfnIdentifier1 } ,
250260 } )
251261 expect ( verificationToken ) . toBeDefined ( )
252262 } )
0 commit comments