Files
product-issue/tests/auth-service.test.ts
T
2026-08-24 17:27:41 +08:00

62 lines
1.9 KiB
TypeScript

import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { AuthService } from '../src/server/auth-service.js'
import { openDatabase, type AppDatabase } from '../src/server/database.js'
const directories: string[] = []
const databases: AppDatabase[] = []
afterEach(async () => {
while (databases.length > 0) {
databases.pop()?.close()
}
await Promise.all(
directories.splice(0).map((directory) =>
rm(directory, { recursive: true, force: true })
)
)
})
describe('AuthService', () => {
it('invalidates existing sessions when an administrator is reset', async () => {
const directory = await mkdtemp(join(tmpdir(), 'product-issue-auth-'))
directories.push(directory)
const database = openDatabase(join(directory, 'database.sqlite'))
databases.push(database)
const auth = new AuthService(
database,
'test-secret-that-is-at-least-thirty-two-characters',
60 * 60 * 1_000
)
await auth.createAdmin({
email: 'admin@example.com',
displayName: 'First name',
password: 'correct horse battery staple'
})
await expect(
auth.login('unknown@example.com', 'an incorrect password')
).resolves.toBeUndefined()
const session = await auth.login(
'admin@example.com',
'correct horse battery staple'
)
expect(session).toBeDefined()
await auth.createAdmin({
email: 'admin@example.com',
displayName: 'Updated name',
password: 'a different secure password'
})
expect(auth.authenticate(session?.token)).toBeUndefined()
await expect(
auth.login('admin@example.com', 'correct horse battery staple')
).resolves.toBeUndefined()
await expect(
auth.login('admin@example.com', 'a different secure password')
).resolves.toMatchObject({ displayName: 'Updated name' })
})
})