]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/logs.ts
Automatically rebuild native modules on ABI change
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / logs.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/models'
6 import {
7 cleanupTests,
8 createSingleServer,
9 killallServers,
10 LogsCommand,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 waitJobs
14 } from '@shared/server-commands'
15
16 const expect = chai.expect
17
18 describe('Test logs', function () {
19 let server: PeerTubeServer
20 let logsCommand: LogsCommand
21
22 before(async function () {
23 this.timeout(30000)
24
25 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ])
27
28 logsCommand = server.logs
29 })
30
31 describe('With the standard log file', function () {
32
33 it('Should get logs with a start date', async function () {
34 this.timeout(20000)
35
36 await server.videos.upload({ attributes: { name: 'video 1' } })
37 await waitJobs([ server ])
38
39 const now = new Date()
40
41 await server.videos.upload({ attributes: { name: 'video 2' } })
42 await waitJobs([ server ])
43
44 const body = await logsCommand.getLogs({ startDate: now })
45 const logsString = JSON.stringify(body)
46
47 expect(logsString.includes('video 1')).to.be.false
48 expect(logsString.includes('video 2')).to.be.true
49 })
50
51 it('Should get logs with an end date', async function () {
52 this.timeout(30000)
53
54 await server.videos.upload({ attributes: { name: 'video 3' } })
55 await waitJobs([ server ])
56
57 const now1 = new Date()
58
59 await server.videos.upload({ attributes: { name: 'video 4' } })
60 await waitJobs([ server ])
61
62 const now2 = new Date()
63
64 await server.videos.upload({ attributes: { name: 'video 5' } })
65 await waitJobs([ server ])
66
67 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
68 const logsString = JSON.stringify(body)
69
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
73 })
74
75 it('Should filter by level', async function () {
76 this.timeout(20000)
77
78 const now = new Date()
79
80 await server.videos.upload({ attributes: { name: 'video 6' } })
81 await waitJobs([ server ])
82
83 {
84 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
85 const logsString = JSON.stringify(body)
86
87 expect(logsString.includes('video 6')).to.be.true
88 }
89
90 {
91 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
92 const logsString = JSON.stringify(body)
93
94 expect(logsString.includes('video 6')).to.be.false
95 }
96 })
97
98 it('Should filter by tag', async function () {
99 const now = new Date()
100
101 const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
102 await waitJobs([ server ])
103
104 {
105 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
106 expect(body).to.have.lengthOf(0)
107 }
108
109 {
110 const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
111 expect(body).to.not.have.lengthOf(0)
112
113 for (const line of body) {
114 expect(line.tags).to.contain(uuid)
115 }
116 }
117 })
118
119 it('Should log ping requests', async function () {
120 this.timeout(10000)
121
122 const now = new Date()
123
124 await server.servers.ping()
125
126 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
127 const logsString = JSON.stringify(body)
128
129 expect(logsString.includes('/api/v1/ping')).to.be.true
130 })
131
132 it('Should not log ping requests', async function () {
133 this.timeout(30000)
134
135 await killallServers([ server ])
136
137 await server.run({ log: { log_ping_requests: false } })
138
139 const now = new Date()
140
141 await server.servers.ping()
142
143 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
144 const logsString = JSON.stringify(body)
145
146 expect(logsString.includes('/api/v1/ping')).to.be.false
147 })
148 })
149
150 describe('With the audit log', function () {
151 it('Should get logs with a start date', async function () {
152 this.timeout(20000)
153
154 await server.videos.upload({ attributes: { name: 'video 7' } })
155 await waitJobs([ server ])
156
157 const now = new Date()
158
159 await server.videos.upload({ attributes: { name: 'video 8' } })
160 await waitJobs([ server ])
161
162 const body = await logsCommand.getAuditLogs({ startDate: now })
163 const logsString = JSON.stringify(body)
164
165 expect(logsString.includes('video 7')).to.be.false
166 expect(logsString.includes('video 8')).to.be.true
167
168 expect(body).to.have.lengthOf(1)
169
170 const item = body[0]
171
172 const message = JSON.parse(item.message)
173 expect(message.domain).to.equal('videos')
174 expect(message.action).to.equal('create')
175 })
176
177 it('Should get logs with an end date', async function () {
178 this.timeout(30000)
179
180 await server.videos.upload({ attributes: { name: 'video 9' } })
181 await waitJobs([ server ])
182
183 const now1 = new Date()
184
185 await server.videos.upload({ attributes: { name: 'video 10' } })
186 await waitJobs([ server ])
187
188 const now2 = new Date()
189
190 await server.videos.upload({ attributes: { name: 'video 11' } })
191 await waitJobs([ server ])
192
193 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
194 const logsString = JSON.stringify(body)
195
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
199 })
200 })
201
202 describe('When creating log from the client', function () {
203
204 it('Should create a warn client log', async function () {
205 const now = new Date()
206
207 await server.logs.createLogClient({
208 payload: {
209 level: 'warn',
210 url: 'http://example.com',
211 message: 'my super client message'
212 },
213 token: null
214 })
215
216 const body = await logsCommand.getLogs({ startDate: now })
217 const logsString = JSON.stringify(body)
218
219 expect(logsString.includes('my super client message')).to.be.true
220 })
221
222 it('Should create an error authenticated client log', async function () {
223 const now = new Date()
224
225 await server.logs.createLogClient({
226 payload: {
227 url: 'https://example.com/page1',
228 level: 'error',
229 message: 'my super client message 2',
230 userAgent: 'super user agent',
231 meta: '{hello}',
232 stackTrace: 'super stack trace'
233 }
234 })
235
236 const body = await logsCommand.getLogs({ startDate: now })
237 const logsString = JSON.stringify(body)
238
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
244 })
245
246 it('Should refuse to create client logs', async function () {
247 await server.kill()
248
249 await server.run({
250 log: {
251 accept_client_log: false
252 }
253 })
254
255 await server.logs.createLogClient({
256 payload: {
257 level: 'warn',
258 url: 'http://example.com',
259 message: 'my super client message'
260 },
261 expectedStatus: HttpStatusCode.FORBIDDEN_403
262 })
263 })
264 })
265
266 after(async function () {
267 await cleanupTests([ server ])
268 })
269 })