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