65 lines
2.4 KiB
TypeScript
65 lines
2.4 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest'
|
||
|
|
import { buildTestApp, setupAdmin, loginAs, auth } from './helpers.js'
|
||
|
|
|
||
|
|
async function appWithData() {
|
||
|
|
const app = await buildTestApp()
|
||
|
|
const cookie = await setupAdmin(app)
|
||
|
|
await app.inject({
|
||
|
|
method: 'POST',
|
||
|
|
url: '/api/library/albums/test-seed',
|
||
|
|
...auth(cookie),
|
||
|
|
payload: { albums: [{ subsonicId: 'a1', title: 'Motion', artist: 'TCO' }] },
|
||
|
|
})
|
||
|
|
return { app, cookie }
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('export', () => {
|
||
|
|
it('returns per-user data without secrets', async () => {
|
||
|
|
const { app, cookie } = await appWithData()
|
||
|
|
await app.inject({ method: 'PUT', url: '/api/settings', ...auth(cookie), payload: { discogsToken: 'secret-token' } })
|
||
|
|
const res = await app.inject({ method: 'GET', url: '/api/export', ...auth(cookie) })
|
||
|
|
expect(res.statusCode).toBe(200)
|
||
|
|
const body = res.json()
|
||
|
|
expect(body.exportedAt).toBeTruthy()
|
||
|
|
expect(body.items).toEqual([])
|
||
|
|
expect(body.loans).toEqual([])
|
||
|
|
expect(body.matchLinks).toEqual([])
|
||
|
|
expect(JSON.stringify(body)).not.toContain('secret-token')
|
||
|
|
await app.close()
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
describe('backups', () => {
|
||
|
|
it('non-admin cannot trigger backups', async () => {
|
||
|
|
const app = await buildTestApp()
|
||
|
|
const cookie = await setupAdmin(app)
|
||
|
|
const bob = await loginAs(app, cookie, 'bob', 'bobpass123')
|
||
|
|
const res = await app.inject({ method: 'POST', url: '/api/backup', ...auth(bob) })
|
||
|
|
expect(res.statusCode).toBe(403)
|
||
|
|
await app.close()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('admin creates a backup file and lists it', async () => {
|
||
|
|
const app = await buildTestApp()
|
||
|
|
const cookie = await setupAdmin(app)
|
||
|
|
const res = await app.inject({ method: 'POST', url: '/api/backup', ...auth(cookie) })
|
||
|
|
expect(res.statusCode).toBe(200)
|
||
|
|
expect(res.json().file).toMatch(/record-shop-.*\.db$/)
|
||
|
|
const list = await app.inject({ method: 'GET', url: '/api/backups', ...auth(cookie) })
|
||
|
|
expect(list.json().backups).toHaveLength(1)
|
||
|
|
expect(list.json().backups[0].file).toMatch(/\.db$/)
|
||
|
|
await app.close()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('prunes to the newest 7 backups', async () => {
|
||
|
|
const app = await buildTestApp()
|
||
|
|
const cookie = await setupAdmin(app)
|
||
|
|
for (let i = 0; i < 9; i++) {
|
||
|
|
await app.inject({ method: 'POST', url: '/api/backup', ...auth(cookie) })
|
||
|
|
}
|
||
|
|
const list = await app.inject({ method: 'GET', url: '/api/backups', ...auth(cookie) })
|
||
|
|
expect(list.json().backups).toHaveLength(7)
|
||
|
|
await app.close()
|
||
|
|
})
|
||
|
|
})
|