11import { http , HttpResponse } from 'msw'
2- import { expect , test , vi } from 'vitest'
2+ import { describe , expect , test } from 'vitest'
33import { server } from '#tests/mocks'
4+ import { consoleWarn } from '#tests/setup/setup-test-env.ts'
45import { checkIsCommonPassword , getPasswordHashParts } from './auth.server.ts'
56
67test ( 'checkIsCommonPassword returns true when password is found in breach database' , async ( ) => {
@@ -55,28 +56,8 @@ test('checkIsCommonPassword returns false when API returns 500', async () => {
5556 expect ( result ) . toBe ( false )
5657} )
5758
58- test ( 'checkIsCommonPassword times out after 1 second' , async ( ) => {
59- const consoleWarnSpy = vi . spyOn ( console , 'warn' )
60- server . use (
61- http . get ( 'https://api.pwnedpasswords.com/range/:prefix' , async ( ) => {
62- const twoSecondDelay = 2000
63- await new Promise ( ( resolve ) => setTimeout ( resolve , twoSecondDelay ) )
64- return new HttpResponse (
65- '1234567890123456789012345678901234A:1\n' +
66- '1234567890123456789012345678901234B:2' ,
67- { status : 200 } ,
68- )
69- } ) ,
70- )
71-
72- const result = await checkIsCommonPassword ( 'testpassword' )
73- expect ( result ) . toBe ( false )
74- expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( 'Password check timed out' )
75- consoleWarnSpy . mockRestore ( )
76- } )
77-
7859test ( 'checkIsCommonPassword returns false when response has invalid format' , async ( ) => {
79- const consoleWarnSpy = vi . spyOn ( console , 'warn' )
60+ consoleWarn . mockImplementation ( ( ) => { } )
8061 const password = 'testpassword'
8162 const [ prefix ] = getPasswordHashParts ( password )
8263
@@ -93,9 +74,36 @@ test('checkIsCommonPassword returns false when response has invalid format', asy
9374
9475 const result = await checkIsCommonPassword ( password )
9576 expect ( result ) . toBe ( false )
96- expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
77+ expect ( consoleWarn ) . toHaveBeenCalledWith (
9778 'Unknown error during password check' ,
9879 expect . any ( TypeError ) ,
9980 )
100- consoleWarnSpy . mockRestore ( )
81+ } )
82+
83+ describe ( 'timeout handling' , ( ) => {
84+ // normally we'd use fake timers for a test like this, but there's an issue
85+ // with AbortSignal.timeout() and fake timers: https://github.com/sinonjs/fake-timers/issues/418
86+ // beforeEach(() => vi.useFakeTimers())
87+ // afterEach(() => vi.useRealTimers())
88+
89+ test ( 'checkIsCommonPassword times out after 1 second' , async ( ) => {
90+ consoleWarn . mockImplementation ( ( ) => { } )
91+ server . use (
92+ http . get ( 'https://api.pwnedpasswords.com/range/:prefix' , async ( ) => {
93+ const twoSecondDelay = 2000
94+ await new Promise ( ( resolve ) => setTimeout ( resolve , twoSecondDelay ) )
95+ // swap to this when we can use fake timers:
96+ // await vi.advanceTimersByTimeAsync(twoSecondDelay)
97+ return new HttpResponse (
98+ '1234567890123456789012345678901234A:1\n' +
99+ '1234567890123456789012345678901234B:2' ,
100+ { status : 200 } ,
101+ )
102+ } ) ,
103+ )
104+
105+ const result = await checkIsCommonPassword ( 'testpassword' )
106+ expect ( result ) . toBe ( false )
107+ expect ( consoleWarn ) . toHaveBeenCalledWith ( 'Password check timed out' )
108+ } )
101109} )
0 commit comments