52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { bootstrapAdmin } from '../../server/utils/bootstrap.js'
|
|
|
|
describe('bootstrapAdmin', () => {
|
|
let run
|
|
let get
|
|
|
|
beforeEach(() => {
|
|
run = vi.fn().mockResolvedValue(undefined)
|
|
get = vi.fn()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
delete process.env.BOOTSTRAP_EMAIL
|
|
delete process.env.BOOTSTRAP_PASSWORD
|
|
})
|
|
|
|
it('returns without inserting when users exist', async () => {
|
|
get.mockResolvedValue({ n: 1 })
|
|
await bootstrapAdmin(run, get)
|
|
expect(get).toHaveBeenCalledWith('SELECT COUNT(*) as n FROM users')
|
|
expect(run).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('inserts default admin when no users and no env', async () => {
|
|
get.mockResolvedValue({ n: 0 })
|
|
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
await bootstrapAdmin(run, get)
|
|
expect(run).toHaveBeenCalledTimes(1)
|
|
const args = run.mock.calls[0][1]
|
|
expect(args[1]).toBe('admin') // identifier
|
|
expect(args[3]).toBe('admin') // role
|
|
expect(logSpy).toHaveBeenCalled()
|
|
logSpy.mockRestore()
|
|
})
|
|
|
|
it('inserts admin with BOOTSTRAP_EMAIL and BOOTSTRAP_PASSWORD when set', async () => {
|
|
get.mockResolvedValue({ n: 0 })
|
|
process.env.BOOTSTRAP_EMAIL = ' admin@example.com '
|
|
process.env.BOOTSTRAP_PASSWORD = 'secret123'
|
|
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
await bootstrapAdmin(run, get)
|
|
expect(run).toHaveBeenCalledTimes(1)
|
|
const args = run.mock.calls[0][1]
|
|
expect(args[1]).toBe('admin@example.com') // identifier
|
|
expect(args[3]).toBe('admin') // role
|
|
expect(logSpy).not.toHaveBeenCalled()
|
|
logSpy.mockRestore()
|
|
})
|
|
})
|