1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/models'
12 setAccessTokensToServers,
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test logs', function () {
19 let server: PeerTubeServer
20 let logsCommand: LogsCommand
22 before(async function () {
25 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ])
28 logsCommand = server.logs
31 describe('With the standard log file', function () {
33 it('Should get logs with a start date', async function () {
36 await server.videos.upload({ attributes: { name: 'video 1' } })
37 await waitJobs([ server ])
39 const now = new Date()
41 await server.videos.upload({ attributes: { name: 'video 2' } })
42 await waitJobs([ server ])
44 const body = await logsCommand.getLogs({ startDate: now })
45 const logsString = JSON.stringify(body)
47 expect(logsString.includes('video 1')).to.be.false
48 expect(logsString.includes('video 2')).to.be.true
51 it('Should get logs with an end date', async function () {
54 await server.videos.upload({ attributes: { name: 'video 3' } })
55 await waitJobs([ server ])
57 const now1 = new Date()
59 await server.videos.upload({ attributes: { name: 'video 4' } })
60 await waitJobs([ server ])
62 const now2 = new Date()
64 await server.videos.upload({ attributes: { name: 'video 5' } })
65 await waitJobs([ server ])
67 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
68 const logsString = JSON.stringify(body)
70 expect(logsString.includes('video 3')).to.be.false
71 expect(logsString.includes('video 4')).to.be.true
72 expect(logsString.includes('video 5')).to.be.false
75 it('Should filter by level', async function () {
78 const now = new Date()
80 await server.videos.upload({ attributes: { name: 'video 6' } })
81 await waitJobs([ server ])
84 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
85 const logsString = JSON.stringify(body)
87 expect(logsString.includes('video 6')).to.be.true
91 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
92 const logsString = JSON.stringify(body)
94 expect(logsString.includes('video 6')).to.be.false
98 it('Should filter by tag', async function () {
99 const now = new Date()
101 const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
102 await waitJobs([ server ])
105 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
106 expect(body).to.have.lengthOf(0)
110 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
111 expect(body).to.not.have.lengthOf(0)
113 for (const line of body) {
114 expect(line.tags).to.contain(uuid)
119 it('Should log ping requests', async function () {
122 const now = new Date()
124 await server.servers.ping()
126 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
127 const logsString = JSON.stringify(body)
129 expect(logsString.includes('/api/v1/ping')).to.be.true
132 it('Should not log ping requests', async function () {
135 await killallServers([ server ])
137 await server.run({ log: { log_ping_requests: false } })
139 const now = new Date()
141 await server.servers.ping()
143 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
144 const logsString = JSON.stringify(body)
146 expect(logsString.includes('/api/v1/ping')).to.be.false
150 describe('With the audit log', function () {
151 it('Should get logs with a start date', async function () {
154 await server.videos.upload({ attributes: { name: 'video 7' } })
155 await waitJobs([ server ])
157 const now = new Date()
159 await server.videos.upload({ attributes: { name: 'video 8' } })
160 await waitJobs([ server ])
162 const body = await logsCommand.getAuditLogs({ startDate: now })
163 const logsString = JSON.stringify(body)
165 expect(logsString.includes('video 7')).to.be.false
166 expect(logsString.includes('video 8')).to.be.true
168 expect(body).to.have.lengthOf(1)
172 const message = JSON.parse(item.message)
173 expect(message.domain).to.equal('videos')
174 expect(message.action).to.equal('create')
177 it('Should get logs with an end date', async function () {
180 await server.videos.upload({ attributes: { name: 'video 9' } })
181 await waitJobs([ server ])
183 const now1 = new Date()
185 await server.videos.upload({ attributes: { name: 'video 10' } })
186 await waitJobs([ server ])
188 const now2 = new Date()
190 await server.videos.upload({ attributes: { name: 'video 11' } })
191 await waitJobs([ server ])
193 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
194 const logsString = JSON.stringify(body)
196 expect(logsString.includes('video 9')).to.be.false
197 expect(logsString.includes('video 10')).to.be.true
198 expect(logsString.includes('video 11')).to.be.false
202 describe('When creating log from the client', function () {
204 it('Should create a warn client log', async function () {
205 const now = new Date()
207 await server.logs.createLogClient({
210 url: 'http://example.com',
211 message: 'my super client message'
216 const body = await logsCommand.getLogs({ startDate: now })
217 const logsString = JSON.stringify(body)
219 expect(logsString.includes('my super client message')).to.be.true
222 it('Should create an error authenticated client log', async function () {
223 const now = new Date()
225 await server.logs.createLogClient({
227 url: 'https://example.com/page1',
229 message: 'my super client message 2',
230 userAgent: 'super user agent',
232 stackTrace: 'super stack trace'
236 const body = await logsCommand.getLogs({ startDate: now })
237 const logsString = JSON.stringify(body)
239 expect(logsString.includes('my super client message 2')).to.be.true
240 expect(logsString.includes('super user agent')).to.be.true
241 expect(logsString.includes('super stack trace')).to.be.true
242 expect(logsString.includes('{hello}')).to.be.true
243 expect(logsString.includes('https://example.com/page1')).to.be.true
246 it('Should refuse to create client logs', async function () {
251 accept_client_log: false
255 await server.logs.createLogClient({
258 url: 'http://example.com',
259 message: 'my super client message'
261 expectedStatus: HttpStatusCode.FORBIDDEN_403
266 after(async function () {
267 await cleanupTests([ server ])