2021-10-11 00:21:57 +00:00
|
|
|
import { Eltro as t, assert} from 'eltro'
|
2021-10-11 00:56:22 +00:00
|
|
|
import fs from 'fs/promises'
|
2021-10-11 00:21:57 +00:00
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
import { server, resetLog } from '../helper.server.mjs'
|
|
|
|
import Client from '../helper.client.mjs'
|
|
|
|
import config from '../../api/config.mjs'
|
|
|
|
import encode from '../../api/jwt/encode.mjs'
|
|
|
|
|
|
|
|
let __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
|
|
|
function resolve(file) {
|
|
|
|
return path.resolve(path.join(__dirname, file))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.describe('Media (API)', () => {
|
|
|
|
const client = new Client()
|
|
|
|
const secret = 'asdf1234'
|
|
|
|
let testFile
|
|
|
|
|
2021-10-11 00:56:22 +00:00
|
|
|
t.after(function() {
|
2021-10-11 00:21:57 +00:00
|
|
|
if (testFile) {
|
2021-10-11 00:56:22 +00:00
|
|
|
return fs.unlink(resolve(`../../public/${testFile}`))
|
2021-10-11 00:21:57 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.timeout(10000).describe('POST /media', function temp() {
|
|
|
|
t.test('should require authentication', async () => {
|
|
|
|
resetLog()
|
|
|
|
assert.strictEqual(server.log.error.callCount, 0)
|
|
|
|
assert.strictEqual(server.log.warn.callCount, 0)
|
|
|
|
let err = await assert.isRejected(
|
|
|
|
client.upload('/media',
|
|
|
|
resolve('test.png')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.strictEqual(err.status, 422)
|
|
|
|
assert.match(err.message, /[Tt]oken/)
|
|
|
|
assert.match(err.message, /[Mm]issing/)
|
|
|
|
|
2021-10-11 01:32:29 +00:00
|
|
|
await new Promise(function(res) {
|
2021-10-11 02:09:18 +00:00
|
|
|
setTimeout(res, 10)
|
2021-10-11 01:32:29 +00:00
|
|
|
})
|
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
assert.strictEqual(server.log.error.callCount, 0)
|
|
|
|
assert.strictEqual(server.log.warn.callCount, 2)
|
|
|
|
assert.strictEqual(typeof(server.log.warn.firstCall[0]), 'string')
|
|
|
|
assert.match(server.log.warn.firstCall[0], /[Tt]oken/)
|
|
|
|
assert.match(server.log.warn.firstCall[0], /[Mm]issing/)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should verify token correctly', async () => {
|
|
|
|
const assertToken = 'asdf.asdf.asdf'
|
|
|
|
resetLog()
|
|
|
|
assert.strictEqual(server.log.error.callCount, 0)
|
|
|
|
assert.strictEqual(server.log.warn.callCount, 0)
|
|
|
|
assert.strictEqual(server.log.info.callCount, 0)
|
|
|
|
|
|
|
|
let err = await assert.isRejected(
|
|
|
|
client.upload('/media?token=' + assertToken,
|
|
|
|
resolve('test.png')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.strictEqual(err.status, 422)
|
|
|
|
assert.match(err.message, /[Tt]oken/)
|
|
|
|
assert.match(err.message, /[Ii]nvalid/)
|
2021-10-11 01:32:29 +00:00
|
|
|
|
|
|
|
await new Promise(function(res) {
|
2021-10-11 02:09:18 +00:00
|
|
|
setTimeout(res, 10)
|
2021-10-11 01:32:29 +00:00
|
|
|
})
|
2021-10-11 00:21:57 +00:00
|
|
|
|
|
|
|
assert.strictEqual(server.log.error.callCount, 1)
|
|
|
|
assert.strictEqual(server.log.warn.callCount, 2)
|
|
|
|
assert.strictEqual(typeof(server.log.warn.firstCall[0]), 'string')
|
|
|
|
assert.match(server.log.warn.firstCall[0], /[Tt]oken/)
|
|
|
|
assert.match(server.log.warn.firstCall[0], /[Ii]nvalid/)
|
|
|
|
assert.ok(server.log.error.lastCall[0] instanceof Error)
|
|
|
|
assert.match(server.log.error.lastCall[1], new RegExp(assertToken))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should upload file and create file', async () => {
|
|
|
|
let token = encode(null, { iss: 'development' }, secret)
|
|
|
|
|
|
|
|
let data = await assert.isFulfilled(
|
|
|
|
client.upload(
|
|
|
|
`/media?token=${token}`,
|
|
|
|
resolve('test.png')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.ok(data)
|
|
|
|
assert.ok(data.filename)
|
|
|
|
assert.ok(data.path)
|
|
|
|
|
|
|
|
testFile = data.path
|
2021-10-11 00:56:22 +00:00
|
|
|
|
|
|
|
let stats = await Promise.all([
|
|
|
|
fs.stat(resolve('test.png')),
|
|
|
|
fs.stat(resolve(`../../public/${testFile}`)),
|
|
|
|
])
|
|
|
|
assert.strictEqual(stats[0].size, stats[1].size)
|
2021-10-11 00:21:57 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|