1
1
import { http , HttpResponse } from 'msw'
2
- import { expect , test , vi } from 'vitest'
2
+ import { describe , expect , test } from 'vitest'
3
3
import { server } from '#tests/mocks'
4
+ import { consoleWarn } from '#tests/setup/setup-test-env.ts'
4
5
import { checkIsCommonPassword , getPasswordHashParts } from './auth.server.ts'
5
6
6
7
test ( 'checkIsCommonPassword returns true when password is found in breach database' , async ( ) => {
@@ -55,28 +56,8 @@ test('checkIsCommonPassword returns false when API returns 500', async () => {
55
56
expect ( result ) . toBe ( false )
56
57
} )
57
58
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
-
78
59
test ( 'checkIsCommonPassword returns false when response has invalid format' , async ( ) => {
79
- const consoleWarnSpy = vi . spyOn ( console , 'warn' )
60
+ consoleWarn . mockImplementation ( ( ) => { } )
80
61
const password = 'testpassword'
81
62
const [ prefix ] = getPasswordHashParts ( password )
82
63
@@ -93,9 +74,36 @@ test('checkIsCommonPassword returns false when response has invalid format', asy
93
74
94
75
const result = await checkIsCommonPassword ( password )
95
76
expect ( result ) . toBe ( false )
96
- expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
77
+ expect ( consoleWarn ) . toHaveBeenCalledWith (
97
78
'Unknown error during password check' ,
98
79
expect . any ( TypeError ) ,
99
80
)
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
+ } )
101
109
} )
0 commit comments