]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
fd8710b8 | 2 | |
86347717 | 3 | import { expect } from 'chai' |
42b40636 | 4 | import { HttpStatusCode } from '@shared/models' |
c9c43612 C |
5 | import { |
6 | cleanupTests, | |
254d3579 | 7 | createSingleServer, |
c9c43612 | 8 | killallServers, |
a92ddacb | 9 | LogsCommand, |
254d3579 | 10 | PeerTubeServer, |
a92ddacb | 11 | setAccessTokensToServers, |
a92ddacb | 12 | waitJobs |
bf54587a | 13 | } from '@shared/server-commands' |
fd8710b8 | 14 | |
fd8710b8 | 15 | describe('Test logs', function () { |
254d3579 | 16 | let server: PeerTubeServer |
a92ddacb | 17 | let logsCommand: LogsCommand |
fd8710b8 C |
18 | |
19 | before(async function () { | |
20 | this.timeout(30000) | |
21 | ||
254d3579 | 22 | server = await createSingleServer(1) |
fd8710b8 | 23 | await setAccessTokensToServers([ server ]) |
a92ddacb | 24 | |
89d241a7 | 25 | logsCommand = server.logs |
fd8710b8 C |
26 | }) |
27 | ||
566c125d | 28 | describe('With the standard log file', function () { |
78d62f4d | 29 | |
566c125d | 30 | it('Should get logs with a start date', async function () { |
ff9112ad | 31 | this.timeout(20000) |
fd8710b8 | 32 | |
89d241a7 | 33 | await server.videos.upload({ attributes: { name: 'video 1' } }) |
566c125d | 34 | await waitJobs([ server ]) |
fd8710b8 | 35 | |
566c125d | 36 | const now = new Date() |
fd8710b8 | 37 | |
89d241a7 | 38 | await server.videos.upload({ attributes: { name: 'video 2' } }) |
566c125d | 39 | await waitJobs([ server ]) |
fd8710b8 | 40 | |
a92ddacb C |
41 | const body = await logsCommand.getLogs({ startDate: now }) |
42 | const logsString = JSON.stringify(body) | |
fd8710b8 | 43 | |
566c125d C |
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 () { | |
ff9112ad | 49 | this.timeout(30000) |
566c125d | 50 | |
89d241a7 | 51 | await server.videos.upload({ attributes: { name: 'video 3' } }) |
566c125d C |
52 | await waitJobs([ server ]) |
53 | ||
54 | const now1 = new Date() | |
55 | ||
89d241a7 | 56 | await server.videos.upload({ attributes: { name: 'video 4' } }) |
566c125d C |
57 | await waitJobs([ server ]) |
58 | ||
59 | const now2 = new Date() | |
60 | ||
89d241a7 | 61 | await server.videos.upload({ attributes: { name: 'video 5' } }) |
566c125d C |
62 | await waitJobs([ server ]) |
63 | ||
a92ddacb C |
64 | const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 }) |
65 | const logsString = JSON.stringify(body) | |
fd8710b8 | 66 | |
566c125d C |
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 | }) | |
fd8710b8 | 71 | |
64553e88 | 72 | it('Should filter by level', async function () { |
ff9112ad | 73 | this.timeout(20000) |
fd8710b8 | 74 | |
566c125d | 75 | const now = new Date() |
fd8710b8 | 76 | |
89d241a7 | 77 | await server.videos.upload({ attributes: { name: 'video 6' } }) |
566c125d | 78 | await waitJobs([ server ]) |
fd8710b8 | 79 | |
566c125d | 80 | { |
a92ddacb C |
81 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
82 | const logsString = JSON.stringify(body) | |
fd8710b8 | 83 | |
566c125d C |
84 | expect(logsString.includes('video 6')).to.be.true |
85 | } | |
fd8710b8 | 86 | |
566c125d | 87 | { |
a92ddacb C |
88 | const body = await logsCommand.getLogs({ startDate: now, level: 'warn' }) |
89 | const logsString = JSON.stringify(body) | |
fd8710b8 | 90 | |
566c125d C |
91 | expect(logsString.includes('video 6')).to.be.false |
92 | } | |
93 | }) | |
78d62f4d | 94 | |
64553e88 C |
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 | ||
78d62f4d | 116 | it('Should log ping requests', async function () { |
ff9112ad C |
117 | this.timeout(10000) |
118 | ||
78d62f4d C |
119 | const now = new Date() |
120 | ||
89d241a7 | 121 | await server.servers.ping() |
78d62f4d | 122 | |
a92ddacb C |
123 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
124 | const logsString = JSON.stringify(body) | |
78d62f4d C |
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(30000) | |
131 | ||
9293139f | 132 | await killallServers([ server ]) |
78d62f4d | 133 | |
254d3579 | 134 | await server.run({ log: { log_ping_requests: false } }) |
78d62f4d C |
135 | |
136 | const now = new Date() | |
137 | ||
89d241a7 | 138 | await server.servers.ping() |
78d62f4d | 139 | |
a92ddacb C |
140 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) |
141 | const logsString = JSON.stringify(body) | |
78d62f4d C |
142 | |
143 | expect(logsString.includes('/api/v1/ping')).to.be.false | |
144 | }) | |
fd8710b8 C |
145 | }) |
146 | ||
566c125d C |
147 | describe('With the audit log', function () { |
148 | it('Should get logs with a start date', async function () { | |
ff9112ad | 149 | this.timeout(20000) |
fd8710b8 | 150 | |
89d241a7 | 151 | await server.videos.upload({ attributes: { name: 'video 7' } }) |
566c125d | 152 | await waitJobs([ server ]) |
fd8710b8 | 153 | |
566c125d | 154 | const now = new Date() |
fd8710b8 | 155 | |
89d241a7 | 156 | await server.videos.upload({ attributes: { name: 'video 8' } }) |
566c125d C |
157 | await waitJobs([ server ]) |
158 | ||
a92ddacb C |
159 | const body = await logsCommand.getAuditLogs({ startDate: now }) |
160 | const logsString = JSON.stringify(body) | |
fd8710b8 | 161 | |
566c125d C |
162 | expect(logsString.includes('video 7')).to.be.false |
163 | expect(logsString.includes('video 8')).to.be.true | |
164 | ||
a92ddacb | 165 | expect(body).to.have.lengthOf(1) |
566c125d | 166 | |
a92ddacb | 167 | const item = body[0] |
566c125d C |
168 | |
169 | const message = JSON.parse(item.message) | |
170 | expect(message.domain).to.equal('videos') | |
171 | expect(message.action).to.equal('create') | |
172 | }) | |
173 | ||
174 | it('Should get logs with an end date', async function () { | |
ff9112ad | 175 | this.timeout(30000) |
566c125d | 176 | |
89d241a7 | 177 | await server.videos.upload({ attributes: { name: 'video 9' } }) |
566c125d C |
178 | await waitJobs([ server ]) |
179 | ||
180 | const now1 = new Date() | |
181 | ||
89d241a7 | 182 | await server.videos.upload({ attributes: { name: 'video 10' } }) |
566c125d C |
183 | await waitJobs([ server ]) |
184 | ||
185 | const now2 = new Date() | |
186 | ||
89d241a7 | 187 | await server.videos.upload({ attributes: { name: 'video 11' } }) |
566c125d | 188 | await waitJobs([ server ]) |
fd8710b8 | 189 | |
a92ddacb C |
190 | const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 }) |
191 | const logsString = JSON.stringify(body) | |
fd8710b8 | 192 | |
566c125d C |
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 | |
196 | }) | |
fd8710b8 C |
197 | }) |
198 | ||
42b40636 C |
199 | describe('When creating log from the client', function () { |
200 | ||
201 | it('Should create a warn client log', async function () { | |
202 | const now = new Date() | |
203 | ||
204 | await server.logs.createLogClient({ | |
205 | payload: { | |
206 | level: 'warn', | |
207 | url: 'http://example.com', | |
208 | message: 'my super client message' | |
209 | }, | |
210 | token: null | |
211 | }) | |
212 | ||
213 | const body = await logsCommand.getLogs({ startDate: now }) | |
214 | const logsString = JSON.stringify(body) | |
215 | ||
216 | expect(logsString.includes('my super client message')).to.be.true | |
217 | }) | |
218 | ||
219 | it('Should create an error authenticated client log', async function () { | |
220 | const now = new Date() | |
221 | ||
222 | await server.logs.createLogClient({ | |
223 | payload: { | |
224 | url: 'https://example.com/page1', | |
225 | level: 'error', | |
226 | message: 'my super client message 2', | |
227 | userAgent: 'super user agent', | |
228 | meta: '{hello}', | |
229 | stackTrace: 'super stack trace' | |
230 | } | |
231 | }) | |
232 | ||
233 | const body = await logsCommand.getLogs({ startDate: now }) | |
234 | const logsString = JSON.stringify(body) | |
235 | ||
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 | |
241 | }) | |
242 | ||
243 | it('Should refuse to create client logs', async function () { | |
244 | await server.kill() | |
245 | ||
246 | await server.run({ | |
247 | log: { | |
248 | accept_client_log: false | |
249 | } | |
250 | }) | |
251 | ||
252 | await server.logs.createLogClient({ | |
253 | payload: { | |
254 | level: 'warn', | |
255 | url: 'http://example.com', | |
256 | message: 'my super client message' | |
257 | }, | |
258 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
259 | }) | |
260 | }) | |
261 | }) | |
262 | ||
7c3b7976 C |
263 | after(async function () { |
264 | await cleanupTests([ server ]) | |
fd8710b8 C |
265 | }) |
266 | }) |