1
0
Fork 0
peertube/packages/tests/src/api/server/logs.ts

269 lines
7.7 KiB
TypeScript
Raw Normal View History

2020-01-31 15:56:52 +00:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-04-10 13:26:33 +00:00
2022-08-17 13:44:32 +00:00
import { expect } from 'chai'
import { HttpStatusCode } from '@peertube/peertube-models'
2021-01-13 09:15:38 +00:00
import {
cleanupTests,
2021-07-16 07:47:51 +00:00
createSingleServer,
2021-01-13 09:15:38 +00:00
killallServers,
2021-07-06 08:34:29 +00:00
LogsCommand,
2021-07-16 07:47:51 +00:00
PeerTubeServer,
2021-07-06 08:34:29 +00:00
setAccessTokensToServers,
waitJobs
} from '@peertube/peertube-server-commands'
2019-04-10 13:26:33 +00:00
describe('Test logs', function () {
2021-07-16 07:47:51 +00:00
let server: PeerTubeServer
2021-07-06 08:34:29 +00:00
let logsCommand: LogsCommand
2019-04-10 13:26:33 +00:00
before(async function () {
this.timeout(30000)
2021-07-16 07:47:51 +00:00
server = await createSingleServer(1)
2019-04-10 13:26:33 +00:00
await setAccessTokensToServers([ server ])
2021-07-06 08:34:29 +00:00
2021-07-16 07:04:35 +00:00
logsCommand = server.logs
2019-04-10 13:26:33 +00:00
})
2019-12-11 13:14:01 +00:00
describe('With the standard log file', function () {
2021-01-13 08:38:19 +00:00
2019-12-11 13:14:01 +00:00
it('Should get logs with a start date', async function () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2019-04-10 13:26:33 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 1' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
const now = new Date()
2019-04-10 13:26:33 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 2' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2019-04-10 13:26:33 +00:00
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2023-07-31 07:35:48 +00:00
expect(logsString.includes('Video with name video 1')).to.be.false
expect(logsString.includes('Video with name video 2')).to.be.true
2019-12-11 13:14:01 +00:00
})
it('Should get logs with an end date', async function () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2019-12-11 13:14:01 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 3' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
const now1 = new Date()
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 4' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
const now2 = new Date()
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 5' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2023-07-31 07:35:48 +00:00
expect(logsString.includes('Video with name video 3')).to.be.false
expect(logsString.includes('Video with name video 4')).to.be.true
expect(logsString.includes('Video with name video 5')).to.be.false
2019-12-11 13:14:01 +00:00
})
2019-04-10 13:26:33 +00:00
2021-10-20 12:23:32 +00:00
it('Should filter by level', async function () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
const now = new Date()
2019-04-10 13:26:33 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 6' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
{
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2023-07-31 07:35:48 +00:00
expect(logsString.includes('Video with name video 6')).to.be.true
2019-12-11 13:14:01 +00:00
}
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
{
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2023-07-31 07:35:48 +00:00
expect(logsString.includes('Video with name video 6')).to.be.false
2019-12-11 13:14:01 +00:00
}
})
2021-01-13 08:38:19 +00:00
2021-10-20 12:23:32 +00:00
it('Should filter by tag', async function () {
const now = new Date()
const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
await waitJobs([ server ])
{
const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
expect(body).to.have.lengthOf(0)
}
{
const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
expect(body).to.not.have.lengthOf(0)
for (const line of body) {
expect(line.tags).to.contain(uuid)
}
}
})
2023-10-24 08:57:41 +00:00
it('Should log ping/HTTP requests', async function () {
2021-01-13 08:38:19 +00:00
const now = new Date()
2021-07-16 07:04:35 +00:00
await server.servers.ping()
2021-01-13 08:38:19 +00:00
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
2021-01-13 08:38:19 +00:00
expect(logsString.includes('/api/v1/ping')).to.be.true
2023-10-24 08:57:41 +00:00
expect(logsString.includes(' HTTP/1.1')).to.be.true
2021-01-13 08:38:19 +00:00
})
2023-10-24 08:57:41 +00:00
it('Should not log ping/HTTP requests', async function () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2021-01-13 08:38:19 +00:00
2021-07-09 13:37:43 +00:00
await killallServers([ server ])
2021-01-13 08:38:19 +00:00
2023-10-24 08:57:41 +00:00
await server.run({ log: { log_ping_requests: false, log_http_requests: false } })
2021-01-13 08:38:19 +00:00
const now = new Date()
2021-07-16 07:04:35 +00:00
await server.servers.ping()
2023-10-24 08:57:41 +00:00
await server.videos.list()
2021-01-13 08:38:19 +00:00
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
const logsString = JSON.stringify(body)
2021-01-13 08:38:19 +00:00
expect(logsString.includes('/api/v1/ping')).to.be.false
2023-10-24 08:57:41 +00:00
expect(logsString.includes(' HTTP/1.1"')).to.be.false
2021-01-13 08:38:19 +00:00
})
2019-04-10 13:26:33 +00:00
})
2019-12-11 13:14:01 +00:00
describe('With the audit log', function () {
2023-05-11 14:33:30 +00:00
2019-12-11 13:14:01 +00:00
it('Should get logs with a start date', async function () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2019-04-10 13:26:33 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 7' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
const now = new Date()
2019-04-10 13:26:33 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 8' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getAuditLogs({ startDate: now })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
expect(logsString.includes('video 7')).to.be.false
expect(logsString.includes('video 8')).to.be.true
2021-07-06 08:34:29 +00:00
expect(body).to.have.lengthOf(1)
2019-12-11 13:14:01 +00:00
2021-07-06 08:34:29 +00:00
const item = body[0]
2019-12-11 13:14:01 +00:00
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 () {
2023-05-11 14:33:30 +00:00
this.timeout(60000)
2019-12-11 13:14:01 +00:00
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 9' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
const now1 = new Date()
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 10' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
const now2 = new Date()
2021-07-16 07:04:35 +00:00
await server.videos.upload({ attributes: { name: 'video 11' } })
2019-12-11 13:14:01 +00:00
await waitJobs([ server ])
2019-04-10 13:26:33 +00:00
2021-07-06 08:34:29 +00:00
const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
const logsString = JSON.stringify(body)
2019-04-10 13:26:33 +00:00
2019-12-11 13:14:01 +00:00
expect(logsString.includes('video 9')).to.be.false
expect(logsString.includes('video 10')).to.be.true
expect(logsString.includes('video 11')).to.be.false
})
2019-04-10 13:26:33 +00:00
})
describe('When creating log from the client', function () {
it('Should create a warn client log', async function () {
const now = new Date()
await server.logs.createLogClient({
payload: {
level: 'warn',
url: 'http://example.com',
message: 'my super client message'
},
token: null
})
const body = await logsCommand.getLogs({ startDate: now })
const logsString = JSON.stringify(body)
expect(logsString.includes('my super client message')).to.be.true
})
it('Should create an error authenticated client log', async function () {
const now = new Date()
await server.logs.createLogClient({
payload: {
url: 'https://example.com/page1',
level: 'error',
message: 'my super client message 2',
userAgent: 'super user agent',
meta: '{hello}',
stackTrace: 'super stack trace'
}
})
const body = await logsCommand.getLogs({ startDate: now })
const logsString = JSON.stringify(body)
expect(logsString.includes('my super client message 2')).to.be.true
expect(logsString.includes('super user agent')).to.be.true
expect(logsString.includes('super stack trace')).to.be.true
expect(logsString.includes('{hello}')).to.be.true
expect(logsString.includes('https://example.com/page1')).to.be.true
})
it('Should refuse to create client logs', async function () {
await server.kill()
await server.run({
log: {
accept_client_log: false
}
})
await server.logs.createLogClient({
payload: {
level: 'warn',
url: 'http://example.com',
message: 'my super client message'
},
expectedStatus: HttpStatusCode.FORBIDDEN_403
})
})
})
2019-04-24 13:10:37 +00:00
after(async function () {
await cleanupTests([ server ])
2019-04-10 13:26:33 +00:00
})
})