storage-upload/test/media/api.test.js

62 lines
1.5 KiB
JavaScript

import { Eltro as t, assert} from 'eltro'
import fs from 'fs'
import { fileURLToPath } from 'url'
import path from 'path'
import createClient from '../helper.client.mjs'
import config from '../../api/config.mjs'
import jwt from '../../api/jwt.mjs'
let __dirname = path.dirname(fileURLToPath(import.meta.url))
console.log(__dirname)
t.describe('Media (API)', () => {
let testFile
let client = createClient()
t.before(() => {
config.set('sites', {
development: 'hello-world'
})
})
t.after(done => {
if (testFile) {
// path.resolve(path.join(__dirname, 'fixtures', file))
return fs.unlink( appRoot.resolve(`/public${testFile}`), done)
}
done()
})
t.describe('POST /media', function temp() {
this.timeout(10000)
t.test('should require authentication', async () => {
let err = await assert.isRejected(
client.sendFileAsync('/media',
appRoot.resolve('/test/media/test.png')))
assert.strictEqual(err.status, 422)
assert.match(err.message, /[Tt]oken/)
})
t.test('should upload file and create file', async () => {
let token = jwt.sign({ site: 'development' }, 'hello-world')
let data = await assert.isFulfilled(
client.sendFileAsync(
`/media?token=${token}`,
appRoot.resolve('/test/media/test.png')
)
)
assert.ok(data)
assert.ok(data.filename)
assert.ok(data.path)
testFile = data.path
})
})
})