1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
14 makeGetRequest, makePostBodyRequest, makePutBodyRequest,
16 setAccessTokensToServers,
18 } from '../../../../shared/extra-utils'
19 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
20 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
22 describe('Test server plugins API validators', function () {
23 let server: ServerInfo
24 let userAccessToken = null
26 const npmPlugin = 'peertube-plugin-hello-world'
27 const pluginName = 'hello-world'
28 let npmVersion: string
30 const themePlugin = 'peertube-theme-background-red'
31 const themeName = 'background-red'
32 let themeVersion: string
34 // ---------------------------------------------------------------
36 before(async function () {
39 server = await flushAndRunServer(1)
41 await setAccessTokensToServers([ server ])
48 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
49 userAccessToken = await userLogin(server, user)
52 const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: npmPlugin })
53 const plugin = res.body as PeerTubePlugin
54 npmVersion = plugin.version
58 const res = await installPlugin({ url: server.url, accessToken: server.accessToken, npmName: themePlugin })
59 const plugin = res.body as PeerTubePlugin
60 themeVersion = plugin.version
64 describe('With static plugin routes', function () {
65 it('Should fail with an unknown plugin name/plugin version', async function () {
67 '/plugins/' + pluginName + '/0.0.1/auth/fake-auth',
68 '/plugins/' + pluginName + '/0.0.1/static/images/chocobo.png',
69 '/plugins/' + pluginName + '/0.0.1/client-scripts/client/common-client-plugin.js',
70 '/themes/' + themeName + '/0.0.1/static/images/chocobo.png',
71 '/themes/' + themeName + '/0.0.1/client-scripts/client/video-watch-client-plugin.js',
72 '/themes/' + themeName + '/0.0.1/css/assets/style1.css'
75 for (const p of paths) {
76 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
80 it('Should fail when requesting a plugin in the theme path', async function () {
81 await makeGetRequest({
83 path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
84 statusCodeExpected: 404
88 it('Should fail with invalid versions', async function () {
90 '/plugins/' + pluginName + '/0.0.1.1/auth/fake-auth',
91 '/plugins/' + pluginName + '/0.0.1.1/static/images/chocobo.png',
92 '/plugins/' + pluginName + '/0.1/client-scripts/client/common-client-plugin.js',
93 '/themes/' + themeName + '/1/static/images/chocobo.png',
94 '/themes/' + themeName + '/0.0.1000a/client-scripts/client/video-watch-client-plugin.js',
95 '/themes/' + themeName + '/0.a.1/css/assets/style1.css'
98 for (const p of paths) {
99 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
103 it('Should fail with invalid paths', async function () {
105 '/plugins/' + pluginName + '/' + npmVersion + '/static/images/../chocobo.png',
106 '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/../client/common-client-plugin.js',
107 '/themes/' + themeName + '/' + themeVersion + '/static/../images/chocobo.png',
108 '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js/..',
109 '/themes/' + themeName + '/' + themeVersion + '/css/../assets/style1.css'
112 for (const p of paths) {
113 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
117 it('Should fail with an unknown auth name', async function () {
118 const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth'
120 await makeGetRequest({ url: server.url, path, statusCodeExpected: 404 })
123 it('Should fail with an unknown static file', async function () {
125 '/plugins/' + pluginName + '/' + npmVersion + '/static/fake/chocobo.png',
126 '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/fake.js',
127 '/themes/' + themeName + '/' + themeVersion + '/static/fake/chocobo.png',
128 '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/fake.js'
131 for (const p of paths) {
132 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
136 it('Should fail with an unknown CSS file', async function () {
137 await makeGetRequest({
139 path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css',
140 statusCodeExpected: 404
144 it('Should succeed with the correct parameters', async function () {
146 '/plugins/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
147 '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/common-client-plugin.js',
148 '/themes/' + themeName + '/' + themeVersion + '/static/images/chocobo.png',
149 '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js',
150 '/themes/' + themeName + '/' + themeVersion + '/css/assets/style1.css'
153 for (const p of paths) {
154 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 200 })
157 const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth'
158 await makeGetRequest({ url: server.url, path: authPath, statusCodeExpected: 302 })
162 describe('When listing available plugins/themes', function () {
163 const path = '/api/v1/plugins/available'
165 search: 'super search',
166 pluginType: PluginType.PLUGIN,
167 currentPeerTubeEngine: '1.2.3'
170 it('Should fail with an invalid token', async function () {
171 await makeGetRequest({
176 statusCodeExpected: 401
180 it('Should fail if the user is not an administrator', async function () {
181 await makeGetRequest({
184 token: userAccessToken,
186 statusCodeExpected: 403
190 it('Should fail with a bad start pagination', async function () {
191 await checkBadStartPagination(server.url, path, server.accessToken)
194 it('Should fail with a bad count pagination', async function () {
195 await checkBadCountPagination(server.url, path, server.accessToken)
198 it('Should fail with an incorrect sort', async function () {
199 await checkBadSortPagination(server.url, path, server.accessToken)
202 it('Should fail with an invalid plugin type', async function () {
203 const query = immutableAssign(baseQuery, { pluginType: 5 })
205 await makeGetRequest({
208 token: server.accessToken,
213 it('Should fail with an invalid current peertube engine', async function () {
214 const query = immutableAssign(baseQuery, { currentPeerTubeEngine: '1.0' })
216 await makeGetRequest({
219 token: server.accessToken,
224 it('Should success with the correct parameters', async function () {
225 await makeGetRequest({
228 token: server.accessToken,
230 statusCodeExpected: 200
235 describe('When listing local plugins/themes', function () {
236 const path = '/api/v1/plugins'
238 pluginType: PluginType.THEME
241 it('Should fail with an invalid token', async function () {
242 await makeGetRequest({
247 statusCodeExpected: 401
251 it('Should fail if the user is not an administrator', async function () {
252 await makeGetRequest({
255 token: userAccessToken,
257 statusCodeExpected: 403
261 it('Should fail with a bad start pagination', async function () {
262 await checkBadStartPagination(server.url, path, server.accessToken)
265 it('Should fail with a bad count pagination', async function () {
266 await checkBadCountPagination(server.url, path, server.accessToken)
269 it('Should fail with an incorrect sort', async function () {
270 await checkBadSortPagination(server.url, path, server.accessToken)
273 it('Should fail with an invalid plugin type', async function () {
274 const query = immutableAssign(baseQuery, { pluginType: 5 })
276 await makeGetRequest({
279 token: server.accessToken,
284 it('Should success with the correct parameters', async function () {
285 await makeGetRequest({
288 token: server.accessToken,
290 statusCodeExpected: 200
295 describe('When getting a plugin or the registered settings or public settings', function () {
296 const path = '/api/v1/plugins/'
298 it('Should fail with an invalid token', async function () {
299 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
300 await makeGetRequest({
304 statusCodeExpected: 401
309 it('Should fail if the user is not an administrator', async function () {
310 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
311 await makeGetRequest({
314 token: userAccessToken,
315 statusCodeExpected: 403
320 it('Should fail with an invalid npm name', async function () {
321 for (const suffix of [ 'toto', 'toto/registered-settings', 'toto/public-settings' ]) {
322 await makeGetRequest({
325 token: server.accessToken,
326 statusCodeExpected: 400
330 for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings', 'peertube-plugin-TOTO/public-settings' ]) {
331 await makeGetRequest({
334 token: server.accessToken,
335 statusCodeExpected: 400
340 it('Should fail with an unknown plugin', async function () {
341 for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings', 'peertube-plugin-toto/public-settings' ]) {
342 await makeGetRequest({
345 token: server.accessToken,
346 statusCodeExpected: 404
351 it('Should succeed with the correct parameters', async function () {
352 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings`, `${npmPlugin}/public-settings` ]) {
353 await makeGetRequest({
356 token: server.accessToken,
357 statusCodeExpected: 200
363 describe('When updating plugin settings', function () {
364 const path = '/api/v1/plugins/'
365 const settings = { setting1: 'value1' }
367 it('Should fail with an invalid token', async function () {
368 await makePutBodyRequest({
370 path: path + npmPlugin + '/settings',
371 fields: { settings },
373 statusCodeExpected: 401
377 it('Should fail if the user is not an administrator', async function () {
378 await makePutBodyRequest({
380 path: path + npmPlugin + '/settings',
381 fields: { settings },
382 token: userAccessToken,
383 statusCodeExpected: 403
387 it('Should fail with an invalid npm name', async function () {
388 await makePutBodyRequest({
390 path: path + 'toto/settings',
391 fields: { settings },
392 token: server.accessToken,
393 statusCodeExpected: 400
396 await makePutBodyRequest({
398 path: path + 'peertube-plugin-TOTO/settings',
399 fields: { settings },
400 token: server.accessToken,
401 statusCodeExpected: 400
405 it('Should fail with an unknown plugin', async function () {
406 await makePutBodyRequest({
408 path: path + 'peertube-plugin-toto/settings',
409 fields: { settings },
410 token: server.accessToken,
411 statusCodeExpected: 404
415 it('Should succeed with the correct parameters', async function () {
416 await makePutBodyRequest({
418 path: path + npmPlugin + '/settings',
419 fields: { settings },
420 token: server.accessToken,
421 statusCodeExpected: 204
426 describe('When installing/updating/uninstalling a plugin', function () {
427 const path = '/api/v1/plugins/'
429 it('Should fail with an invalid token', async function () {
430 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
431 await makePostBodyRequest({
434 fields: { npmName: npmPlugin },
436 statusCodeExpected: 401
441 it('Should fail if the user is not an administrator', async function () {
442 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
443 await makePostBodyRequest({
446 fields: { npmName: npmPlugin },
447 token: userAccessToken,
448 statusCodeExpected: 403
453 it('Should fail with an invalid npm name', async function () {
454 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
455 await makePostBodyRequest({
458 fields: { npmName: 'toto' },
459 token: server.accessToken,
460 statusCodeExpected: 400
464 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
465 await makePostBodyRequest({
468 fields: { npmName: 'peertube-plugin-TOTO' },
469 token: server.accessToken,
470 statusCodeExpected: 400
475 it('Should succeed with the correct parameters', async function () {
479 { suffix: 'install', status: 200 },
480 { suffix: 'update', status: 200 },
481 { suffix: 'uninstall', status: 204 }
484 for (const obj of it) {
485 await makePostBodyRequest({
487 path: path + obj.suffix,
488 fields: { npmName: npmPlugin },
489 token: server.accessToken,
490 statusCodeExpected: obj.status
496 after(async function () {
497 await cleanupTests([ server ])