2021-10-09 00:11:52 +00:00
|
|
|
import { Eltro as t, assert} from 'eltro'
|
2017-12-10 09:45:38 +00:00
|
|
|
import fs from 'fs'
|
2021-10-09 00:11:52 +00:00
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import path from 'path'
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2021-10-09 00:11:52 +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
|
|
|
|
2021-10-09 00:11:52 +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
|
2021-10-09 00:11:52 +00:00
|
|
|
let client = createClient()
|
2017-12-10 09:45:38 +00:00
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
t.before(() => {
|
2017-12-10 09:45:38 +00:00
|
|
|
config.set('sites', {
|
|
|
|
development: 'hello-world'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
t.after(done => {
|
2017-12-10 09:45:38 +00:00
|
|
|
if (testFile) {
|
2021-10-09 00:11:52 +00:00
|
|
|
// path.resolve(path.join(__dirname, 'fixtures', file))
|
|
|
|
return fs.unlink( appRoot.resolve(`/public${testFile}`), done)
|
2017-12-10 09:45:38 +00:00
|
|
|
}
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
t.describe('POST /media', function temp() {
|
2017-12-10 09:45:38 +00:00
|
|
|
this.timeout(10000)
|
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
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/)
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:11:52 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|