storage-upload/test/media/routes.test.mjs

64 lines
1.7 KiB
JavaScript

import { Eltro as t, assert, stub } from 'eltro'
import { createContext } from '../helper.server.mjs'
import MediaRoutes from '../../api/media/routes.mjs'
t.describe('#upload', () => {
const stubVerify = stub()
const stubUpload = stub()
const routes = new MediaRoutes({
security: { verifyToken: stubVerify },
formidable: { uploadFile: stubUpload },
})
function reset() {
stubVerify.reset()
stubUpload.reset()
}
t.test('should call security correctly', async () => {
reset()
let ctx = createContext()
const assertError = new Error('temp')
stubVerify.rejects(assertError)
let err = await assert.isRejected(routes.upload(ctx))
assert.ok(stubVerify.called)
assert.strictEqual(err, assertError)
assert.strictEqual(stubVerify.firstCall[0], ctx)
})
t.test('should call upload correctly', async () => {
reset()
let ctx = createContext()
const assertSiteName = 'benshapiro'
const assertError = new Error('hello')
stubVerify.resolves(assertSiteName)
stubUpload.rejects(assertError)
let err = await assert.isRejected(routes.upload(ctx))
assert.ok(stubUpload.called)
assert.strictEqual(err, assertError)
assert.strictEqual(stubUpload.firstCall[0], ctx)
assert.strictEqual(stubUpload.firstCall[1], assertSiteName)
})
t.test('should otherwise set context status to 204 and file in result', async () => {
reset()
let ctx = createContext()
const assertFilename = 'asdfsafd'
const assertSite = 'mario'
stubVerify.resolves(assertSite)
stubUpload.resolves({ filename: assertFilename })
await routes.upload(ctx)
assert.strictEqual(ctx.body.filename, assertFilename)
assert.strictEqual(ctx.body.path, `/${assertSite}/${assertFilename}`)
})
})