]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
fd8710b8 | 2 | |
fd8710b8 | 3 | import 'mocha' |
c9c43612 | 4 | import * as chai from 'chai' |
42b40636 | 5 | import { HttpStatusCode } from '@shared/models' |
c9c43612 C |
6 | import { |
7 | cleanupTests, | |
254d3579 | 8 | createSingleServer, |
c9c43612 | 9 | killallServers, |
a92ddacb | 10 | LogsCommand, |
254d3579 | 11 | PeerTubeServer, |
a92ddacb | 12 | setAccessTokensToServers, |
a92ddacb | 13 | waitJobs |
bf54587a | 14 | } from '@shared/server-commands' |
fd8710b8 C |
15 | |
16 | const expect = chai.expect | |
17 | ||
18 | describe('Test logs', function () { | |
254d3579 | 19 | let server: PeerTubeServer |
a92ddacb | 20 | let logsCommand: LogsCommand |
fd8710b8 C |
21 | |
22 | before(async function () { | |
23 | this.timeout(30000) | |
24 | ||
254d3579 | 25 | server = await createSingleServer(1) |
fd8710b8 | 26 | await setAccessTokensToServers([ server ]) |
a92ddacb | 27 | |
89d241a7 | 28 | logsCommand = server.logs |
fd8710b8 C |
29 | }) |
30 | ||
566c125d | 31 | describe('With the standard log file', function () { |
78d62f4d | 32 | |
566c125d | 33 | it('Should get logs with a start date', async function () { |
ff9112ad | 34 | this.timeout(20000) |
fd8710b8 | 35 | |
89d241a7 | 36 | await server.videos.upload({ attributes: { name: 'video 1' } }) |
566c125d | 37 | await waitJobs([ server ]) |
fd8710b8 | 38 | |
566c125d | 39 | const now = new Date() |
fd8710b8 | 40 | |
89d241a7 | 41 | await server.videos.upload({ attributes: { name: 'video 2' } }) |
566c125d | 42 | await waitJobs([ server ]) |
fd8710b8 | 43 | |
a92ddacb C |
44 | const body = await logsCommand.getLogs({ startDate: now }) |
45 | const logsString = JSON.stringify(body) | |
fd8710b8 | 46 | |
566c125d C |
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 () { | |
ff9112ad | 52 | this.timeout(30000) |
566c125d | 53 | |
89d241a7 | 54 | await server.videos.upload({ attributes: { name: 'video 3' } }) |
566c125d C |
55 | await waitJobs([ server ]) |
56 | ||
57 | const now1 = new Date() | |
58 | ||
89d241a7 | 59 | await server.videos.upload({ attributes: { name: 'video 4' } }) |
566c125d C |
60 | await waitJobs([ server ]) |
61 | ||
62 | const now2 = new Date() | |
63 | ||
89d241a7 | 64 | await server.videos.upload({ attributes: { name: 'video 5' } }) |
566c125d C |
65 | await waitJobs([ server ]) |
66 | ||
a92ddacb C |
67 | const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 }) |
68 | const logsString = JSON.stringify(body) | |
fd8710b8 | 69 | |
566c125d C |
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 | }) | |
fd8710b8 | 74 | |
64553e88 | 75 | it('Should filter by level', async function () { |
ff9112ad | 76 | this.timeout(20000) |
fd8710b8 | 77 | |
566c125d | 78 | const now = new Date() |
fd8710b8 | 79 | |
89d241a7 | 80 | await server.videos.upload({ attributes: { name: 'video 6' } }) |
566c125d | 81 | await waitJobs([ server ]) |
fd8710b8 | 82 | |
566c125d | 83 | { |
a92ddacb C |
84 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
85 | const logsString = JSON.stringify(body) | |
fd8710b8 | 86 | |
566c125d C |
87 | expect(logsString.includes('video 6')).to.be.true |
88 | } | |
fd8710b8 | 89 | |
566c125d | 90 | { |
a92ddacb C |
91 | const body = await logsCommand.getLogs({ startDate: now, level: 'warn' }) |
92 | const logsString = JSON.stringify(body) | |
fd8710b8 | 93 | |
566c125d C |
94 | expect(logsString.includes('video 6')).to.be.false |
95 | } | |
96 | }) | |
78d62f4d | 97 | |
64553e88 C |
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 | ||
78d62f4d | 119 | it('Should log ping requests', async function () { |
ff9112ad C |
120 | this.timeout(10000) |
121 | ||
78d62f4d C |
122 | const now = new Date() |
123 | ||
89d241a7 | 124 | await server.servers.ping() |
78d62f4d | 125 | |
a92ddacb C |
126 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
127 | const logsString = JSON.stringify(body) | |
78d62f4d C |
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 | ||
9293139f | 135 | await killallServers([ server ]) |
78d62f4d | 136 | |
254d3579 | 137 | await server.run({ log: { log_ping_requests: false } }) |
78d62f4d C |
138 | |
139 | const now = new Date() | |
140 | ||
89d241a7 | 141 | await server.servers.ping() |
78d62f4d | 142 | |
a92ddacb C |
143 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
144 | const logsString = JSON.stringify(body) | |
78d62f4d C |
145 | |
146 | expect(logsString.includes('/api/v1/ping')).to.be.false | |
147 | }) | |
fd8710b8 C |
148 | }) |
149 | ||
566c125d C |
150 | describe('With the audit log', function () { |
151 | it('Should get logs with a start date', async function () { | |
ff9112ad | 152 | this.timeout(20000) |
fd8710b8 | 153 | |
89d241a7 | 154 | await server.videos.upload({ attributes: { name: 'video 7' } }) |
566c125d | 155 | await waitJobs([ server ]) |
fd8710b8 | 156 | |
566c125d | 157 | const now = new Date() |
fd8710b8 | 158 | |
89d241a7 | 159 | await server.videos.upload({ attributes: { name: 'video 8' } }) |
566c125d C |
160 | await waitJobs([ server ]) |
161 | ||
a92ddacb C |
162 | const body = await logsCommand.getAuditLogs({ startDate: now }) |
163 | const logsString = JSON.stringify(body) | |
fd8710b8 | 164 | |
566c125d C |
165 | expect(logsString.includes('video 7')).to.be.false |
166 | expect(logsString.includes('video 8')).to.be.true | |
167 | ||
a92ddacb | 168 | expect(body).to.have.lengthOf(1) |
566c125d | 169 | |
a92ddacb | 170 | const item = body[0] |
566c125d C |
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 () { | |
ff9112ad | 178 | this.timeout(30000) |
566c125d | 179 | |
89d241a7 | 180 | await server.videos.upload({ attributes: { name: 'video 9' } }) |
566c125d C |
181 | await waitJobs([ server ]) |
182 | ||
183 | const now1 = new Date() | |
184 | ||
89d241a7 | 185 | await server.videos.upload({ attributes: { name: 'video 10' } }) |
566c125d C |
186 | await waitJobs([ server ]) |
187 | ||
188 | const now2 = new Date() | |
189 | ||
89d241a7 | 190 | await server.videos.upload({ attributes: { name: 'video 11' } }) |
566c125d | 191 | await waitJobs([ server ]) |
fd8710b8 | 192 | |
a92ddacb C |
193 | const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 }) |
194 | const logsString = JSON.stringify(body) | |
fd8710b8 | 195 | |
566c125d C |
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 | }) | |
fd8710b8 C |
200 | }) |
201 | ||
42b40636 C |
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 | ||
7c3b7976 C |
266 | after(async function () { |
267 | await cleanupTests([ server ]) | |
fd8710b8 C |
268 | }) |
269 | }) |