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