Skip to content

Commit ea28869

Browse files
committed
fix tests and cache
1 parent e3a594f commit ea28869

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

app/utils/auth.server.test.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { http, HttpResponse } from 'msw'
2-
import { expect, test, vi } from 'vitest'
2+
import { describe, expect, test } from 'vitest'
33
import { server } from '#tests/mocks'
4+
import { consoleWarn } from '#tests/setup/setup-test-env.ts'
45
import { checkIsCommonPassword, getPasswordHashParts } from './auth.server.ts'
56

67
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 () => {
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-
7859
test('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
})

app/utils/auth.server.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ export async function checkIsCommonPassword(password: string) {
272272
try {
273273
const response = await fetch(
274274
`https://api.pwnedpasswords.com/range/${prefix}`,
275-
{
276-
signal: AbortSignal.timeout(1000),
277-
},
275+
{ signal: AbortSignal.timeout(1000) },
278276
)
279277

280278
if (!response.ok) return false

tests/setup/global-setup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import path from 'node:path'
22
import { execaCommand } from 'execa'
33
import fsExtra from 'fs-extra'
4+
import 'dotenv/config'
5+
import '#app/utils/env.server.ts'
6+
import '#app/utils/cache.server.ts'
47

58
export const BASE_DATABASE_PATH = path.join(
69
process.cwd(),

tests/setup/setup-test-env.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ afterEach(() => server.resetHandlers())
1212
afterEach(() => cleanup())
1313

1414
export let consoleError: MockInstance<(typeof console)['error']>
15+
export let consoleWarn: MockInstance<(typeof console)['warn']>
1516

1617
beforeEach(() => {
1718
const originalConsoleError = console.error
@@ -24,4 +25,13 @@ beforeEach(() => {
2425
)
2526
},
2627
)
28+
29+
const originalConsoleWarn = console.warn
30+
consoleWarn = vi.spyOn(console, 'warn')
31+
consoleWarn.mockImplementation((...args: Parameters<typeof console.warn>) => {
32+
originalConsoleWarn(...args)
33+
throw new Error(
34+
'Console warn was called. Call consoleWarn.mockImplementation(() => {}) if this is expected.',
35+
)
36+
})
2737
})

0 commit comments

Comments
 (0)