1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { HttpStatusCode } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 describe('Test logs', function () {
16 let server: PeerTubeServer
17 let logsCommand: LogsCommand
19 before(async function () {
22 server = await createSingleServer(1)
23 await setAccessTokensToServers([ server ])
25 logsCommand = server.logs
28 describe('With the standard log file', function () {
30 it('Should get logs with a start date', async function () {
33 await server.videos.upload({ attributes: { name: 'video 1' } })
34 await waitJobs([ server ])
36 const now = new Date()
38 await server.videos.upload({ attributes: { name: 'video 2' } })
39 await waitJobs([ server ])
41 const body = await logsCommand.getLogs({ startDate: now })
42 const logsString = JSON.stringify(body)
44 expect(logsString.includes('video 1')).to.be.false
45 expect(logsString.includes('video 2')).to.be.true
48 it('Should get logs with an end date', async function () {
51 await server.videos.upload({ attributes: { name: 'video 3' } })
52 await waitJobs([ server ])
54 const now1 = new Date()
56 await server.videos.upload({ attributes: { name: 'video 4' } })
57 await waitJobs([ server ])
59 const now2 = new Date()
61 await server.videos.upload({ attributes: { name: 'video 5' } })
62 await waitJobs([ server ])
64 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
65 const logsString = JSON.stringify(body)
67 expect(logsString.includes('video 3')).to.be.false
68 expect(logsString.includes('video 4')).to.be.true
69 expect(logsString.includes('video 5')).to.be.false
72 it('Should filter by level', async function () {
75 const now = new Date()
77 await server.videos.upload({ attributes: { name: 'video 6' } })
78 await waitJobs([ server ])
81 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
82 const logsString = JSON.stringify(body)
84 expect(logsString.includes('video 6')).to.be.true
88 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
89 const logsString = JSON.stringify(body)
91 expect(logsString.includes('video 6')).to.be.false
95 it('Should filter by tag', async function () {
96 const now = new Date()
98 const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
99 await waitJobs([ server ])
102 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
103 expect(body).to.have.lengthOf(0)
107 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
108 expect(body).to.not.have.lengthOf(0)
110 for (const line of body) {
111 expect(line.tags).to.contain(uuid)
116 it('Should log ping requests', async function () {
119 const now = new Date()
121 await server.servers.ping()
123 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
124 const logsString = JSON.stringify(body)
126 expect(logsString.includes('/api/v1/ping')).to.be.true
129 it('Should not log ping requests', async function () {
132 await killallServers([ server ])
134 await server.run({ log: { log_ping_requests: false } })
136 const now = new Date()
138 await server.servers.ping()
140 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
141 const logsString = JSON.stringify(body)
143 expect(logsString.includes('/api/v1/ping')).to.be.false
147 describe('With the audit log', function () {
148 it('Should get logs with a start date', async function () {
151 await server.videos.upload({ attributes: { name: 'video 7' } })
152 await waitJobs([ server ])
154 const now = new Date()
156 await server.videos.upload({ attributes: { name: 'video 8' } })
157 await waitJobs([ server ])
159 const body = await logsCommand.getAuditLogs({ startDate: now })
160 const logsString = JSON.stringify(body)
162 expect(logsString.includes('video 7')).to.be.false
163 expect(logsString.includes('video 8')).to.be.true
165 expect(body).to.have.lengthOf(1)
169 const message = JSON.parse(item.message)
170 expect(message.domain).to.equal('videos')
171 expect(message.action).to.equal('create')
174 it('Should get logs with an end date', async function () {
177 await server.videos.upload({ attributes: { name: 'video 9' } })
178 await waitJobs([ server ])
180 const now1 = new Date()
182 await server.videos.upload({ attributes: { name: 'video 10' } })
183 await waitJobs([ server ])
185 const now2 = new Date()
187 await server.videos.upload({ attributes: { name: 'video 11' } })
188 await waitJobs([ server ])
190 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
191 const logsString = JSON.stringify(body)
193 expect(logsString.includes('video 9')).to.be.false
194 expect(logsString.includes('video 10')).to.be.true
195 expect(logsString.includes('video 11')).to.be.false
199 describe('When creating log from the client', function () {
201 it('Should create a warn client log', async function () {
202 const now = new Date()
204 await server.logs.createLogClient({
207 url: 'http://example.com',
208 message: 'my super client message'
213 const body = await logsCommand.getLogs({ startDate: now })
214 const logsString = JSON.stringify(body)
216 expect(logsString.includes('my super client message')).to.be.true
219 it('Should create an error authenticated client log', async function () {
220 const now = new Date()
222 await server.logs.createLogClient({
224 url: 'https://example.com/page1',
226 message: 'my super client message 2',
227 userAgent: 'super user agent',
229 stackTrace: 'super stack trace'
233 const body = await logsCommand.getLogs({ startDate: now })
234 const logsString = JSON.stringify(body)
236 expect(logsString.includes('my super client message 2')).to.be.true
237 expect(logsString.includes('super user agent')).to.be.true
238 expect(logsString.includes('super stack trace')).to.be.true
239 expect(logsString.includes('{hello}')).to.be.true
240 expect(logsString.includes('https://example.com/page1')).to.be.true
243 it('Should refuse to create client logs', async function () {
248 accept_client_log: false
252 await server.logs.createLogClient({
255 url: 'http://example.com',
256 message: 'my super client message'
258 expectedStatus: HttpStatusCode.FORBIDDEN_403
263 after(async function () {
264 await cleanupTests([ server ])