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

62 lines
1.5 KiB
JavaScript
Raw Normal View History

import { Eltro as t, assert} from 'eltro'
2017-12-10 09:45:38 +00:00
import fs from 'fs'
import { fileURLToPath } from 'url'
import path from 'path'
2017-12-10 09:45:38 +00:00
import createClient from '../helper.client.mjs'
import config from '../../api/config.mjs'
import jwt from '../../api/jwt.mjs'
2017-12-10 09:45:38 +00:00
let __dirname = path.dirname(fileURLToPath(import.meta.url))
console.log(__dirname)
t.describe('Media (API)', () => {
2017-12-10 09:45:38 +00:00
let testFile
let client = createClient()
2017-12-10 09:45:38 +00:00
t.before(() => {
2017-12-10 09:45:38 +00:00
config.set('sites', {
development: 'hello-world'
})
})
t.after(done => {
2017-12-10 09:45:38 +00:00
if (testFile) {
// path.resolve(path.join(__dirname, 'fixtures', file))
return fs.unlink( appRoot.resolve(`/public${testFile}`), done)
2017-12-10 09:45:38 +00:00
}
done()
})
t.describe('POST /media', function temp() {
2017-12-10 09:45:38 +00:00
this.timeout(10000)
t.test('should require authentication', async () => {
2017-12-10 09:45:38 +00:00
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 () => {
2017-12-10 09:45:38 +00:00
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
})
})
})