diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-15 15:30:14 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-07-18 11:37:18 +0200 |
commit | 42b40636991b97fe818007fab19091764fc5db73 (patch) | |
tree | db431787c06ce898d22e91ff771f795219274fc6 /server/tests | |
parent | 654d4ede7fa4d0faa71e49bcfab6b65a686397b2 (diff) | |
download | PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.gz PeerTube-42b40636991b97fe818007fab19091764fc5db73.tar.zst PeerTube-42b40636991b97fe818007fab19091764fc5db73.zip |
Add ability for client to create server logs
Diffstat (limited to 'server/tests')
-rw-r--r-- | server/tests/api/check-params/logs.ts | 59 | ||||
-rw-r--r-- | server/tests/api/server/logs.ts | 65 |
2 files changed, 123 insertions, 1 deletions
diff --git a/server/tests/api/check-params/logs.ts b/server/tests/api/check-params/logs.ts index 970671c15..fa67408b7 100644 --- a/server/tests/api/check-params/logs.ts +++ b/server/tests/api/check-params/logs.ts | |||
@@ -1,8 +1,9 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' | 4 | import { expect } from 'chai' |
5 | import { HttpStatusCode } from '@shared/models' | 5 | import { HttpStatusCode } from '@shared/models' |
6 | import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' | ||
6 | 7 | ||
7 | describe('Test logs API validators', function () { | 8 | describe('Test logs API validators', function () { |
8 | const path = '/api/v1/server/logs' | 9 | const path = '/api/v1/server/logs' |
@@ -95,6 +96,62 @@ describe('Test logs API validators', function () { | |||
95 | }) | 96 | }) |
96 | }) | 97 | }) |
97 | 98 | ||
99 | describe('When creating client logs', function () { | ||
100 | const base = { | ||
101 | level: 'warn' as 'warn', | ||
102 | message: 'my super message', | ||
103 | url: 'https://example.com/toto' | ||
104 | } | ||
105 | const expectedStatus = HttpStatusCode.BAD_REQUEST_400 | ||
106 | |||
107 | it('Should fail with an invalid level', async function () { | ||
108 | await server.logs.createLogClient({ payload: { ...base, level: '' as any }, expectedStatus }) | ||
109 | await server.logs.createLogClient({ payload: { ...base, level: undefined }, expectedStatus }) | ||
110 | await server.logs.createLogClient({ payload: { ...base, level: 'toto' as any }, expectedStatus }) | ||
111 | }) | ||
112 | |||
113 | it('Should fail with an invalid message', async function () { | ||
114 | await server.logs.createLogClient({ payload: { ...base, message: undefined }, expectedStatus }) | ||
115 | await server.logs.createLogClient({ payload: { ...base, message: '' }, expectedStatus }) | ||
116 | await server.logs.createLogClient({ payload: { ...base, message: 'm'.repeat(2500) }, expectedStatus }) | ||
117 | }) | ||
118 | |||
119 | it('Should fail with an invalid url', async function () { | ||
120 | await server.logs.createLogClient({ payload: { ...base, url: undefined }, expectedStatus }) | ||
121 | await server.logs.createLogClient({ payload: { ...base, url: 'toto' }, expectedStatus }) | ||
122 | }) | ||
123 | |||
124 | it('Should fail with an invalid stackTrace', async function () { | ||
125 | await server.logs.createLogClient({ payload: { ...base, stackTrace: 's'.repeat(10000) }, expectedStatus }) | ||
126 | }) | ||
127 | |||
128 | it('Should fail with an invalid userAgent', async function () { | ||
129 | await server.logs.createLogClient({ payload: { ...base, userAgent: 's'.repeat(500) }, expectedStatus }) | ||
130 | }) | ||
131 | |||
132 | it('Should fail with an invalid meta', async function () { | ||
133 | await server.logs.createLogClient({ payload: { ...base, meta: 's'.repeat(10000) }, expectedStatus }) | ||
134 | }) | ||
135 | |||
136 | it('Should succeed with the correct params', async function () { | ||
137 | await server.logs.createLogClient({ payload: { ...base, stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' } }) | ||
138 | }) | ||
139 | |||
140 | it('Should rate limit log creation', async function () { | ||
141 | let fail = false | ||
142 | |||
143 | for (let i = 0; i < 10; i++) { | ||
144 | try { | ||
145 | await server.logs.createLogClient({ token: null, payload: base }) | ||
146 | } catch { | ||
147 | fail = true | ||
148 | } | ||
149 | } | ||
150 | |||
151 | expect(fail).to.be.true | ||
152 | }) | ||
153 | }) | ||
154 | |||
98 | after(async function () { | 155 | after(async function () { |
99 | await cleanupTests([ server ]) | 156 | await cleanupTests([ server ]) |
100 | }) | 157 | }) |
diff --git a/server/tests/api/server/logs.ts b/server/tests/api/server/logs.ts index 697f10337..ed7555fd7 100644 --- a/server/tests/api/server/logs.ts +++ b/server/tests/api/server/logs.ts | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import * as chai from 'chai' | 4 | import * as chai from 'chai' |
5 | import { HttpStatusCode } from '@shared/models' | ||
5 | import { | 6 | import { |
6 | cleanupTests, | 7 | cleanupTests, |
7 | createSingleServer, | 8 | createSingleServer, |
@@ -198,6 +199,70 @@ describe('Test logs', function () { | |||
198 | }) | 199 | }) |
199 | }) | 200 | }) |
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 | |||
201 | after(async function () { | 266 | after(async function () { |
202 | await cleanupTests([ server ]) | 267 | await cleanupTests([ server ]) |
203 | }) | 268 | }) |