diff options
Diffstat (limited to 'packages/tests/src/api/server/logs.ts')
-rw-r--r-- | packages/tests/src/api/server/logs.ts | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/packages/tests/src/api/server/logs.ts b/packages/tests/src/api/server/logs.ts new file mode 100644 index 000000000..11c86d694 --- /dev/null +++ b/packages/tests/src/api/server/logs.ts | |||
@@ -0,0 +1,265 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { HttpStatusCode } from '@peertube/peertube-models' | ||
5 | import { | ||
6 | cleanupTests, | ||
7 | createSingleServer, | ||
8 | killallServers, | ||
9 | LogsCommand, | ||
10 | PeerTubeServer, | ||
11 | setAccessTokensToServers, | ||
12 | waitJobs | ||
13 | } from '@peertube/peertube-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 with name video 1')).to.be.false | ||
45 | expect(logsString.includes('Video with name 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 with name video 3')).to.be.false | ||
68 | expect(logsString.includes('Video with name video 4')).to.be.true | ||
69 | expect(logsString.includes('Video with name 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 with name 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 with name 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 | const now = new Date() | ||
118 | |||
119 | await server.servers.ping() | ||
120 | |||
121 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) | ||
122 | const logsString = JSON.stringify(body) | ||
123 | |||
124 | expect(logsString.includes('/api/v1/ping')).to.be.true | ||
125 | }) | ||
126 | |||
127 | it('Should not log ping requests', async function () { | ||
128 | this.timeout(60000) | ||
129 | |||
130 | await killallServers([ server ]) | ||
131 | |||
132 | await server.run({ log: { log_ping_requests: false } }) | ||
133 | |||
134 | const now = new Date() | ||
135 | |||
136 | await server.servers.ping() | ||
137 | |||
138 | const body = await logsCommand.getLogs({ startDate: now, level: 'info' }) | ||
139 | const logsString = JSON.stringify(body) | ||
140 | |||
141 | expect(logsString.includes('/api/v1/ping')).to.be.false | ||
142 | }) | ||
143 | }) | ||
144 | |||
145 | describe('With the audit log', function () { | ||
146 | |||
147 | it('Should get logs with a start date', async function () { | ||
148 | this.timeout(60000) | ||
149 | |||
150 | await server.videos.upload({ attributes: { name: 'video 7' } }) | ||
151 | await waitJobs([ server ]) | ||
152 | |||
153 | const now = new Date() | ||
154 | |||
155 | await server.videos.upload({ attributes: { name: 'video 8' } }) | ||
156 | await waitJobs([ server ]) | ||
157 | |||
158 | const body = await logsCommand.getAuditLogs({ startDate: now }) | ||
159 | const logsString = JSON.stringify(body) | ||
160 | |||
161 | expect(logsString.includes('video 7')).to.be.false | ||
162 | expect(logsString.includes('video 8')).to.be.true | ||
163 | |||
164 | expect(body).to.have.lengthOf(1) | ||
165 | |||
166 | const item = body[0] | ||
167 | |||
168 | const message = JSON.parse(item.message) | ||
169 | expect(message.domain).to.equal('videos') | ||
170 | expect(message.action).to.equal('create') | ||
171 | }) | ||
172 | |||
173 | it('Should get logs with an end date', async function () { | ||
174 | this.timeout(60000) | ||
175 | |||
176 | await server.videos.upload({ attributes: { name: 'video 9' } }) | ||
177 | await waitJobs([ server ]) | ||
178 | |||
179 | const now1 = new Date() | ||
180 | |||
181 | await server.videos.upload({ attributes: { name: 'video 10' } }) | ||
182 | await waitJobs([ server ]) | ||
183 | |||
184 | const now2 = new Date() | ||
185 | |||
186 | await server.videos.upload({ attributes: { name: 'video 11' } }) | ||
187 | await waitJobs([ server ]) | ||
188 | |||
189 | const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 }) | ||
190 | const logsString = JSON.stringify(body) | ||
191 | |||
192 | expect(logsString.includes('video 9')).to.be.false | ||
193 | expect(logsString.includes('video 10')).to.be.true | ||
194 | expect(logsString.includes('video 11')).to.be.false | ||
195 | }) | ||
196 | }) | ||
197 | |||
198 | describe('When creating log from the client', function () { | ||
199 | |||
200 | it('Should create a warn client log', async function () { | ||
201 | const now = new Date() | ||
202 | |||
203 | await server.logs.createLogClient({ | ||
204 | payload: { | ||
205 | level: 'warn', | ||
206 | url: 'http://example.com', | ||
207 | message: 'my super client message' | ||
208 | }, | ||
209 | token: null | ||
210 | }) | ||
211 | |||
212 | const body = await logsCommand.getLogs({ startDate: now }) | ||
213 | const logsString = JSON.stringify(body) | ||
214 | |||
215 | expect(logsString.includes('my super client message')).to.be.true | ||
216 | }) | ||
217 | |||
218 | it('Should create an error authenticated client log', async function () { | ||
219 | const now = new Date() | ||
220 | |||
221 | await server.logs.createLogClient({ | ||
222 | payload: { | ||
223 | url: 'https://example.com/page1', | ||
224 | level: 'error', | ||
225 | message: 'my super client message 2', | ||
226 | userAgent: 'super user agent', | ||
227 | meta: '{hello}', | ||
228 | stackTrace: 'super stack trace' | ||
229 | } | ||
230 | }) | ||
231 | |||
232 | const body = await logsCommand.getLogs({ startDate: now }) | ||
233 | const logsString = JSON.stringify(body) | ||
234 | |||
235 | expect(logsString.includes('my super client message 2')).to.be.true | ||
236 | expect(logsString.includes('super user agent')).to.be.true | ||
237 | expect(logsString.includes('super stack trace')).to.be.true | ||
238 | expect(logsString.includes('{hello}')).to.be.true | ||
239 | expect(logsString.includes('https://example.com/page1')).to.be.true | ||
240 | }) | ||
241 | |||
242 | it('Should refuse to create client logs', async function () { | ||
243 | await server.kill() | ||
244 | |||
245 | await server.run({ | ||
246 | log: { | ||
247 | accept_client_log: false | ||
248 | } | ||
249 | }) | ||
250 | |||
251 | await server.logs.createLogClient({ | ||
252 | payload: { | ||
253 | level: 'warn', | ||
254 | url: 'http://example.com', | ||
255 | message: 'my super client message' | ||
256 | }, | ||
257 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | ||
258 | }) | ||
259 | }) | ||
260 | }) | ||
261 | |||
262 | after(async function () { | ||
263 | await cleanupTests([ server ]) | ||
264 | }) | ||
265 | }) | ||