PeerTube_original/server/tests/api/server/logs.ts
2021-07-20 15:27:16 +02:00

187 lines
5.1 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import * as chai from 'chai'
import {
cleanupTests,
flushAndRunServer,
killallServers,
LogsCommand,
makePingRequest,
reRunServer,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
waitJobs
} from '@shared/extra-utils'
const expect = chai.expect
describe('Test logs', function () {
let server: ServerInfo
let logsCommand: LogsCommand
before(async function () {
this.timeout(30000)
server = await flushAndRunServer(1)
await setAccessTokensToServers([ server ])
logsCommand = server.logsCommand
})
describe('With the standard log file', function () {
it('Should get logs with a start date', async function () {
this.timeout(20000)
await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
await waitJobs([ server ])
const now = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
await waitJobs([ server ])
const body = await logsCommand.getLogs({ startDate: now })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 1')).to.be.false
expect(logsString.includes('video 2')).to.be.true
})
it('Should get logs with an end date', async function () {
this.timeout(30000)
await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
await waitJobs([ server ])
const now1 = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 4' })
await waitJobs([ server ])
const now2 = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 5' })
await waitJobs([ server ])
const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 3')).to.be.false
expect(logsString.includes('video 4')).to.be.true
expect(logsString.includes('video 5')).to.be.false
})
it('Should get filter by level', async function () {
this.timeout(20000)
const now = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 6' })
await waitJobs([ server ])
{
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 6')).to.be.true
}
{
const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 6')).to.be.false
}
})
it('Should log ping requests', async function () {
this.timeout(10000)
const now = new Date()
await makePingRequest(server)
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
expect(logsString.includes('/api/v1/ping')).to.be.true
})
it('Should not log ping requests', async function () {
this.timeout(30000)
killallServers([ server ])
await reRunServer(server, { log: { log_ping_requests: false } })
const now = new Date()
await makePingRequest(server)
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
expect(logsString.includes('/api/v1/ping')).to.be.false
})
})
describe('With the audit log', function () {
it('Should get logs with a start date', async function () {
this.timeout(20000)
await uploadVideo(server.url, server.accessToken, { name: 'video 7' })
await waitJobs([ server ])
const now = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 8' })
await waitJobs([ server ])
const body = await logsCommand.getAuditLogs({ startDate: now })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 7')).to.be.false
expect(logsString.includes('video 8')).to.be.true
expect(body).to.have.lengthOf(1)
const item = body[0]
const message = JSON.parse(item.message)
expect(message.domain).to.equal('videos')
expect(message.action).to.equal('create')
})
it('Should get logs with an end date', async function () {
this.timeout(30000)
await uploadVideo(server.url, server.accessToken, { name: 'video 9' })
await waitJobs([ server ])
const now1 = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 10' })
await waitJobs([ server ])
const now2 = new Date()
await uploadVideo(server.url, server.accessToken, { name: 'video 11' })
await waitJobs([ server ])
const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
const logsString = JSON.stringify(body)
expect(logsString.includes('video 9')).to.be.false
expect(logsString.includes('video 10')).to.be.true
expect(logsString.includes('video 11')).to.be.false
})
})
after(async function () {
await cleanupTests([ server ])
})
})