]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
60cfd4cb C |
2 | |
3 | import 'mocha' | |
c55e3d72 C |
4 | import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared' |
5 | import { HttpStatusCode, PeerTubePlugin, PluginType } from '@shared/models' | |
60cfd4cb | 6 | import { |
60cfd4cb | 7 | cleanupTests, |
254d3579 | 8 | createSingleServer, |
428ccb8b C |
9 | makeGetRequest, |
10 | makePostBodyRequest, | |
11 | makePutBodyRequest, | |
254d3579 | 12 | PeerTubeServer, |
41d1d075 | 13 | setAccessTokensToServers |
bf54587a | 14 | } from '@shared/server-commands' |
60cfd4cb C |
15 | |
16 | describe('Test server plugins API validators', function () { | |
254d3579 | 17 | let server: PeerTubeServer |
60cfd4cb C |
18 | let userAccessToken = null |
19 | ||
20 | const npmPlugin = 'peertube-plugin-hello-world' | |
21 | const pluginName = 'hello-world' | |
22 | let npmVersion: string | |
23 | ||
24 | const themePlugin = 'peertube-theme-background-red' | |
25 | const themeName = 'background-red' | |
26 | let themeVersion: string | |
27 | ||
28 | // --------------------------------------------------------------- | |
29 | ||
30 | before(async function () { | |
33675a47 | 31 | this.timeout(60000) |
60cfd4cb | 32 | |
254d3579 | 33 | server = await createSingleServer(1) |
60cfd4cb C |
34 | |
35 | await setAccessTokensToServers([ server ]) | |
36 | ||
37 | const user = { | |
38 | username: 'user1', | |
39 | password: 'password' | |
40 | } | |
41 | ||
89d241a7 C |
42 | await server.users.create({ username: user.username, password: user.password }) |
43 | userAccessToken = await server.login.getAccessToken(user) | |
60cfd4cb C |
44 | |
45 | { | |
89d241a7 | 46 | const res = await server.plugins.install({ npmName: npmPlugin }) |
60cfd4cb C |
47 | const plugin = res.body as PeerTubePlugin |
48 | npmVersion = plugin.version | |
49 | } | |
50 | ||
51 | { | |
89d241a7 | 52 | const res = await server.plugins.install({ npmName: themePlugin }) |
60cfd4cb C |
53 | const plugin = res.body as PeerTubePlugin |
54 | themeVersion = plugin.version | |
55 | } | |
56 | }) | |
57 | ||
58 | describe('With static plugin routes', function () { | |
59 | it('Should fail with an unknown plugin name/plugin version', async function () { | |
60 | const paths = [ | |
9107d791 | 61 | '/plugins/' + pluginName + '/0.0.1/auth/fake-auth', |
60cfd4cb C |
62 | '/plugins/' + pluginName + '/0.0.1/static/images/chocobo.png', |
63 | '/plugins/' + pluginName + '/0.0.1/client-scripts/client/common-client-plugin.js', | |
64 | '/themes/' + themeName + '/0.0.1/static/images/chocobo.png', | |
65 | '/themes/' + themeName + '/0.0.1/client-scripts/client/video-watch-client-plugin.js', | |
66 | '/themes/' + themeName + '/0.0.1/css/assets/style1.css' | |
67 | ] | |
68 | ||
69 | for (const p of paths) { | |
c0e8b12e | 70 | await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
60cfd4cb C |
71 | } |
72 | }) | |
73 | ||
74 | it('Should fail when requesting a plugin in the theme path', async function () { | |
75 | await makeGetRequest({ | |
76 | url: server.url, | |
77 | path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png', | |
c0e8b12e | 78 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
60cfd4cb C |
79 | }) |
80 | }) | |
81 | ||
82 | it('Should fail with invalid versions', async function () { | |
83 | const paths = [ | |
9107d791 | 84 | '/plugins/' + pluginName + '/0.0.1.1/auth/fake-auth', |
60cfd4cb C |
85 | '/plugins/' + pluginName + '/0.0.1.1/static/images/chocobo.png', |
86 | '/plugins/' + pluginName + '/0.1/client-scripts/client/common-client-plugin.js', | |
87 | '/themes/' + themeName + '/1/static/images/chocobo.png', | |
88 | '/themes/' + themeName + '/0.0.1000a/client-scripts/client/video-watch-client-plugin.js', | |
89 | '/themes/' + themeName + '/0.a.1/css/assets/style1.css' | |
90 | ] | |
91 | ||
92 | for (const p of paths) { | |
c0e8b12e | 93 | await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
60cfd4cb C |
94 | } |
95 | }) | |
96 | ||
97 | it('Should fail with invalid paths', async function () { | |
98 | const paths = [ | |
99 | '/plugins/' + pluginName + '/' + npmVersion + '/static/images/../chocobo.png', | |
100 | '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/../client/common-client-plugin.js', | |
101 | '/themes/' + themeName + '/' + themeVersion + '/static/../images/chocobo.png', | |
102 | '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js/..', | |
103 | '/themes/' + themeName + '/' + themeVersion + '/css/../assets/style1.css' | |
104 | ] | |
105 | ||
106 | for (const p of paths) { | |
c0e8b12e | 107 | await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
60cfd4cb C |
108 | } |
109 | }) | |
110 | ||
9107d791 C |
111 | it('Should fail with an unknown auth name', async function () { |
112 | const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth' | |
113 | ||
c0e8b12e | 114 | await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
9107d791 C |
115 | }) |
116 | ||
60cfd4cb C |
117 | it('Should fail with an unknown static file', async function () { |
118 | const paths = [ | |
119 | '/plugins/' + pluginName + '/' + npmVersion + '/static/fake/chocobo.png', | |
120 | '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/fake.js', | |
121 | '/themes/' + themeName + '/' + themeVersion + '/static/fake/chocobo.png', | |
122 | '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/fake.js' | |
123 | ] | |
124 | ||
125 | for (const p of paths) { | |
c0e8b12e | 126 | await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
60cfd4cb C |
127 | } |
128 | }) | |
129 | ||
130 | it('Should fail with an unknown CSS file', async function () { | |
131 | await makeGetRequest({ | |
132 | url: server.url, | |
133 | path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css', | |
c0e8b12e | 134 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
60cfd4cb C |
135 | }) |
136 | }) | |
137 | ||
138 | it('Should succeed with the correct parameters', async function () { | |
139 | const paths = [ | |
140 | '/plugins/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png', | |
141 | '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/common-client-plugin.js', | |
142 | '/themes/' + themeName + '/' + themeVersion + '/static/images/chocobo.png', | |
143 | '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js', | |
144 | '/themes/' + themeName + '/' + themeVersion + '/css/assets/style1.css' | |
145 | ] | |
146 | ||
147 | for (const p of paths) { | |
c0e8b12e | 148 | await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.OK_200 }) |
60cfd4cb | 149 | } |
9107d791 C |
150 | |
151 | const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth' | |
c0e8b12e | 152 | await makeGetRequest({ url: server.url, path: authPath, expectedStatus: HttpStatusCode.FOUND_302 }) |
60cfd4cb C |
153 | }) |
154 | }) | |
155 | ||
156 | describe('When listing available plugins/themes', function () { | |
157 | const path = '/api/v1/plugins/available' | |
158 | const baseQuery = { | |
159 | search: 'super search', | |
09071200 C |
160 | pluginType: PluginType.PLUGIN, |
161 | currentPeerTubeEngine: '1.2.3' | |
60cfd4cb C |
162 | } |
163 | ||
164 | it('Should fail with an invalid token', async function () { | |
165 | await makeGetRequest({ | |
166 | url: server.url, | |
167 | path, | |
168 | token: 'fake_token', | |
169 | query: baseQuery, | |
c0e8b12e | 170 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
60cfd4cb C |
171 | }) |
172 | }) | |
173 | ||
174 | it('Should fail if the user is not an administrator', async function () { | |
175 | await makeGetRequest({ | |
176 | url: server.url, | |
177 | path, | |
178 | token: userAccessToken, | |
179 | query: baseQuery, | |
c0e8b12e | 180 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
60cfd4cb C |
181 | }) |
182 | }) | |
183 | ||
184 | it('Should fail with a bad start pagination', async function () { | |
185 | await checkBadStartPagination(server.url, path, server.accessToken) | |
186 | }) | |
187 | ||
188 | it('Should fail with a bad count pagination', async function () { | |
189 | await checkBadCountPagination(server.url, path, server.accessToken) | |
190 | }) | |
191 | ||
192 | it('Should fail with an incorrect sort', async function () { | |
193 | await checkBadSortPagination(server.url, path, server.accessToken) | |
194 | }) | |
195 | ||
196 | it('Should fail with an invalid plugin type', async function () { | |
6c5065a0 | 197 | const query = { ...baseQuery, pluginType: 5 } |
60cfd4cb C |
198 | |
199 | await makeGetRequest({ | |
200 | url: server.url, | |
201 | path, | |
202 | token: server.accessToken, | |
203 | query | |
204 | }) | |
205 | }) | |
206 | ||
09071200 | 207 | it('Should fail with an invalid current peertube engine', async function () { |
6c5065a0 | 208 | const query = { ...baseQuery, currentPeerTubeEngine: '1.0' } |
09071200 C |
209 | |
210 | await makeGetRequest({ | |
211 | url: server.url, | |
212 | path, | |
213 | token: server.accessToken, | |
214 | query | |
215 | }) | |
216 | }) | |
217 | ||
60cfd4cb C |
218 | it('Should success with the correct parameters', async function () { |
219 | await makeGetRequest({ | |
220 | url: server.url, | |
221 | path, | |
222 | token: server.accessToken, | |
223 | query: baseQuery, | |
c0e8b12e | 224 | expectedStatus: HttpStatusCode.OK_200 |
60cfd4cb C |
225 | }) |
226 | }) | |
227 | }) | |
228 | ||
229 | describe('When listing local plugins/themes', function () { | |
230 | const path = '/api/v1/plugins' | |
231 | const baseQuery = { | |
232 | pluginType: PluginType.THEME | |
233 | } | |
234 | ||
235 | it('Should fail with an invalid token', async function () { | |
236 | await makeGetRequest({ | |
237 | url: server.url, | |
238 | path, | |
239 | token: 'fake_token', | |
240 | query: baseQuery, | |
c0e8b12e | 241 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
60cfd4cb C |
242 | }) |
243 | }) | |
244 | ||
245 | it('Should fail if the user is not an administrator', async function () { | |
246 | await makeGetRequest({ | |
247 | url: server.url, | |
248 | path, | |
249 | token: userAccessToken, | |
250 | query: baseQuery, | |
c0e8b12e | 251 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
60cfd4cb C |
252 | }) |
253 | }) | |
254 | ||
255 | it('Should fail with a bad start pagination', async function () { | |
256 | await checkBadStartPagination(server.url, path, server.accessToken) | |
257 | }) | |
258 | ||
259 | it('Should fail with a bad count pagination', async function () { | |
260 | await checkBadCountPagination(server.url, path, server.accessToken) | |
261 | }) | |
262 | ||
263 | it('Should fail with an incorrect sort', async function () { | |
264 | await checkBadSortPagination(server.url, path, server.accessToken) | |
265 | }) | |
266 | ||
267 | it('Should fail with an invalid plugin type', async function () { | |
6c5065a0 | 268 | const query = { ...baseQuery, pluginType: 5 } |
60cfd4cb C |
269 | |
270 | await makeGetRequest({ | |
271 | url: server.url, | |
272 | path, | |
273 | token: server.accessToken, | |
274 | query | |
275 | }) | |
276 | }) | |
277 | ||
278 | it('Should success with the correct parameters', async function () { | |
279 | await makeGetRequest({ | |
280 | url: server.url, | |
281 | path, | |
282 | token: server.accessToken, | |
283 | query: baseQuery, | |
c0e8b12e | 284 | expectedStatus: HttpStatusCode.OK_200 |
60cfd4cb C |
285 | }) |
286 | }) | |
287 | }) | |
288 | ||
ba211e73 | 289 | describe('When getting a plugin or the registered settings or public settings', function () { |
60cfd4cb C |
290 | const path = '/api/v1/plugins/' |
291 | ||
292 | it('Should fail with an invalid token', async function () { | |
293 | for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) { | |
294 | await makeGetRequest({ | |
295 | url: server.url, | |
296 | path: path + suffix, | |
297 | token: 'fake_token', | |
c0e8b12e | 298 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
60cfd4cb C |
299 | }) |
300 | } | |
301 | }) | |
302 | ||
303 | it('Should fail if the user is not an administrator', async function () { | |
304 | for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) { | |
305 | await makeGetRequest({ | |
306 | url: server.url, | |
307 | path: path + suffix, | |
308 | token: userAccessToken, | |
c0e8b12e | 309 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
60cfd4cb C |
310 | }) |
311 | } | |
312 | }) | |
313 | ||
314 | it('Should fail with an invalid npm name', async function () { | |
ba211e73 | 315 | for (const suffix of [ 'toto', 'toto/registered-settings', 'toto/public-settings' ]) { |
60cfd4cb C |
316 | await makeGetRequest({ |
317 | url: server.url, | |
318 | path: path + suffix, | |
319 | token: server.accessToken, | |
c0e8b12e | 320 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
321 | }) |
322 | } | |
323 | ||
ba211e73 | 324 | for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings', 'peertube-plugin-TOTO/public-settings' ]) { |
60cfd4cb C |
325 | await makeGetRequest({ |
326 | url: server.url, | |
327 | path: path + suffix, | |
328 | token: server.accessToken, | |
c0e8b12e | 329 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
330 | }) |
331 | } | |
332 | }) | |
333 | ||
334 | it('Should fail with an unknown plugin', async function () { | |
ba211e73 | 335 | for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings', 'peertube-plugin-toto/public-settings' ]) { |
60cfd4cb C |
336 | await makeGetRequest({ |
337 | url: server.url, | |
338 | path: path + suffix, | |
339 | token: server.accessToken, | |
c0e8b12e | 340 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
60cfd4cb C |
341 | }) |
342 | } | |
343 | }) | |
344 | ||
345 | it('Should succeed with the correct parameters', async function () { | |
ba211e73 | 346 | for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings`, `${npmPlugin}/public-settings` ]) { |
60cfd4cb C |
347 | await makeGetRequest({ |
348 | url: server.url, | |
349 | path: path + suffix, | |
350 | token: server.accessToken, | |
c0e8b12e | 351 | expectedStatus: HttpStatusCode.OK_200 |
60cfd4cb C |
352 | }) |
353 | } | |
354 | }) | |
355 | }) | |
356 | ||
357 | describe('When updating plugin settings', function () { | |
358 | const path = '/api/v1/plugins/' | |
359 | const settings = { setting1: 'value1' } | |
360 | ||
361 | it('Should fail with an invalid token', async function () { | |
362 | await makePutBodyRequest({ | |
363 | url: server.url, | |
364 | path: path + npmPlugin + '/settings', | |
365 | fields: { settings }, | |
366 | token: 'fake_token', | |
c0e8b12e | 367 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
60cfd4cb C |
368 | }) |
369 | }) | |
370 | ||
371 | it('Should fail if the user is not an administrator', async function () { | |
372 | await makePutBodyRequest({ | |
373 | url: server.url, | |
374 | path: path + npmPlugin + '/settings', | |
375 | fields: { settings }, | |
376 | token: userAccessToken, | |
c0e8b12e | 377 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
60cfd4cb C |
378 | }) |
379 | }) | |
380 | ||
381 | it('Should fail with an invalid npm name', async function () { | |
382 | await makePutBodyRequest({ | |
383 | url: server.url, | |
384 | path: path + 'toto/settings', | |
385 | fields: { settings }, | |
386 | token: server.accessToken, | |
c0e8b12e | 387 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
388 | }) |
389 | ||
390 | await makePutBodyRequest({ | |
391 | url: server.url, | |
392 | path: path + 'peertube-plugin-TOTO/settings', | |
393 | fields: { settings }, | |
394 | token: server.accessToken, | |
c0e8b12e | 395 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
396 | }) |
397 | }) | |
398 | ||
399 | it('Should fail with an unknown plugin', async function () { | |
400 | await makePutBodyRequest({ | |
401 | url: server.url, | |
402 | path: path + 'peertube-plugin-toto/settings', | |
403 | fields: { settings }, | |
404 | token: server.accessToken, | |
c0e8b12e | 405 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
60cfd4cb C |
406 | }) |
407 | }) | |
408 | ||
409 | it('Should succeed with the correct parameters', async function () { | |
410 | await makePutBodyRequest({ | |
411 | url: server.url, | |
412 | path: path + npmPlugin + '/settings', | |
413 | fields: { settings }, | |
414 | token: server.accessToken, | |
c0e8b12e | 415 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
60cfd4cb C |
416 | }) |
417 | }) | |
418 | }) | |
419 | ||
420 | describe('When installing/updating/uninstalling a plugin', function () { | |
421 | const path = '/api/v1/plugins/' | |
422 | ||
423 | it('Should fail with an invalid token', async function () { | |
424 | for (const suffix of [ 'install', 'update', 'uninstall' ]) { | |
425 | await makePostBodyRequest({ | |
426 | url: server.url, | |
427 | path: path + suffix, | |
428 | fields: { npmName: npmPlugin }, | |
429 | token: 'fake_token', | |
c0e8b12e | 430 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
60cfd4cb C |
431 | }) |
432 | } | |
433 | }) | |
434 | ||
435 | it('Should fail if the user is not an administrator', async function () { | |
436 | for (const suffix of [ 'install', 'update', 'uninstall' ]) { | |
437 | await makePostBodyRequest({ | |
438 | url: server.url, | |
439 | path: path + suffix, | |
440 | fields: { npmName: npmPlugin }, | |
441 | token: userAccessToken, | |
c0e8b12e | 442 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
60cfd4cb C |
443 | }) |
444 | } | |
445 | }) | |
446 | ||
447 | it('Should fail with an invalid npm name', async function () { | |
448 | for (const suffix of [ 'install', 'update', 'uninstall' ]) { | |
449 | await makePostBodyRequest({ | |
450 | url: server.url, | |
451 | path: path + suffix, | |
452 | fields: { npmName: 'toto' }, | |
453 | token: server.accessToken, | |
c0e8b12e | 454 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
455 | }) |
456 | } | |
457 | ||
458 | for (const suffix of [ 'install', 'update', 'uninstall' ]) { | |
459 | await makePostBodyRequest({ | |
460 | url: server.url, | |
461 | path: path + suffix, | |
462 | fields: { npmName: 'peertube-plugin-TOTO' }, | |
463 | token: server.accessToken, | |
c0e8b12e | 464 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 |
60cfd4cb C |
465 | }) |
466 | } | |
467 | }) | |
468 | ||
469 | it('Should succeed with the correct parameters', async function () { | |
9107d791 C |
470 | this.timeout(10000) |
471 | ||
60cfd4cb | 472 | const it = [ |
2d53be02 RK |
473 | { suffix: 'install', status: HttpStatusCode.OK_200 }, |
474 | { suffix: 'update', status: HttpStatusCode.OK_200 }, | |
475 | { suffix: 'uninstall', status: HttpStatusCode.NO_CONTENT_204 } | |
60cfd4cb C |
476 | ] |
477 | ||
478 | for (const obj of it) { | |
479 | await makePostBodyRequest({ | |
480 | url: server.url, | |
481 | path: path + obj.suffix, | |
482 | fields: { npmName: npmPlugin }, | |
483 | token: server.accessToken, | |
c0e8b12e | 484 | expectedStatus: obj.status |
60cfd4cb C |
485 | }) |
486 | } | |
487 | }) | |
488 | }) | |
489 | ||
490 | after(async function () { | |
491 | await cleanupTests([ server ]) | |
492 | }) | |
493 | }) |