1 /* tslint:disable:no-unused-expression */
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/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'
74 for (const p of paths) {
75 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
79 it('Should fail when requesting a plugin in the theme path', async function () {
80 await makeGetRequest({
82 path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png',
83 statusCodeExpected: 404
87 it('Should fail with invalid versions', async function () {
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'
96 for (const p of paths) {
97 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
101 it('Should fail with invalid paths', async function () {
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'
110 for (const p of paths) {
111 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 400 })
115 it('Should fail with an unknown static file', async function () {
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'
123 for (const p of paths) {
124 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 404 })
128 it('Should fail with an unknown CSS file', async function () {
129 await makeGetRequest({
131 path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css',
132 statusCodeExpected: 404
136 it('Should succeed with the correct parameters', async function () {
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'
145 for (const p of paths) {
146 await makeGetRequest({ url: server.url, path: p, statusCodeExpected: 200 })
151 describe('When listing available plugins/themes', function () {
152 const path = '/api/v1/plugins/available'
154 search: 'super search',
155 pluginType: PluginType.PLUGIN,
156 currentPeerTubeEngine: '1.2.3'
159 it('Should fail with an invalid token', async function () {
160 await makeGetRequest({
165 statusCodeExpected: 401
169 it('Should fail if the user is not an administrator', async function () {
170 await makeGetRequest({
173 token: userAccessToken,
175 statusCodeExpected: 403
179 it('Should fail with a bad start pagination', async function () {
180 await checkBadStartPagination(server.url, path, server.accessToken)
183 it('Should fail with a bad count pagination', async function () {
184 await checkBadCountPagination(server.url, path, server.accessToken)
187 it('Should fail with an incorrect sort', async function () {
188 await checkBadSortPagination(server.url, path, server.accessToken)
191 it('Should fail with an invalid plugin type', async function () {
192 const query = immutableAssign(baseQuery, { pluginType: 5 })
194 await makeGetRequest({
197 token: server.accessToken,
202 it('Should fail with an invalid current peertube engine', async function () {
203 const query = immutableAssign(baseQuery, { currentPeerTubeEngine: '1.0' })
205 await makeGetRequest({
208 token: server.accessToken,
213 it('Should success with the correct parameters', async function () {
214 await makeGetRequest({
217 token: server.accessToken,
219 statusCodeExpected: 200
224 describe('When listing local plugins/themes', function () {
225 const path = '/api/v1/plugins'
227 pluginType: PluginType.THEME
230 it('Should fail with an invalid token', async function () {
231 await makeGetRequest({
236 statusCodeExpected: 401
240 it('Should fail if the user is not an administrator', async function () {
241 await makeGetRequest({
244 token: userAccessToken,
246 statusCodeExpected: 403
250 it('Should fail with a bad start pagination', async function () {
251 await checkBadStartPagination(server.url, path, server.accessToken)
254 it('Should fail with a bad count pagination', async function () {
255 await checkBadCountPagination(server.url, path, server.accessToken)
258 it('Should fail with an incorrect sort', async function () {
259 await checkBadSortPagination(server.url, path, server.accessToken)
262 it('Should fail with an invalid plugin type', async function () {
263 const query = immutableAssign(baseQuery, { pluginType: 5 })
265 await makeGetRequest({
268 token: server.accessToken,
273 it('Should success with the correct parameters', async function () {
274 await makeGetRequest({
277 token: server.accessToken,
279 statusCodeExpected: 200
284 describe('When getting a plugin or the registered settings', function () {
285 const path = '/api/v1/plugins/'
287 it('Should fail with an invalid token', async function () {
288 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
289 await makeGetRequest({
293 statusCodeExpected: 401
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({
303 token: userAccessToken,
304 statusCodeExpected: 403
309 it('Should fail with an invalid npm name', async function () {
310 for (const suffix of [ 'toto', 'toto/registered-settings' ]) {
311 await makeGetRequest({
314 token: server.accessToken,
315 statusCodeExpected: 400
319 for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings' ]) {
320 await makeGetRequest({
323 token: server.accessToken,
324 statusCodeExpected: 400
329 it('Should fail with an unknown plugin', async function () {
330 for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings' ]) {
331 await makeGetRequest({
334 token: server.accessToken,
335 statusCodeExpected: 404
340 it('Should succeed with the correct parameters', async function () {
341 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
342 await makeGetRequest({
345 token: server.accessToken,
346 statusCodeExpected: 200
352 describe('When updating plugin settings', function () {
353 const path = '/api/v1/plugins/'
354 const settings = { setting1: 'value1' }
356 it('Should fail with an invalid token', async function () {
357 await makePutBodyRequest({
359 path: path + npmPlugin + '/settings',
360 fields: { settings },
362 statusCodeExpected: 401
366 it('Should fail if the user is not an administrator', async function () {
367 await makePutBodyRequest({
369 path: path + npmPlugin + '/settings',
370 fields: { settings },
371 token: userAccessToken,
372 statusCodeExpected: 403
376 it('Should fail with an invalid npm name', async function () {
377 await makePutBodyRequest({
379 path: path + 'toto/settings',
380 fields: { settings },
381 token: server.accessToken,
382 statusCodeExpected: 400
385 await makePutBodyRequest({
387 path: path + 'peertube-plugin-TOTO/settings',
388 fields: { settings },
389 token: server.accessToken,
390 statusCodeExpected: 400
394 it('Should fail with an unknown plugin', async function () {
395 await makePutBodyRequest({
397 path: path + 'peertube-plugin-toto/settings',
398 fields: { settings },
399 token: server.accessToken,
400 statusCodeExpected: 404
404 it('Should succeed with the correct parameters', async function () {
405 await makePutBodyRequest({
407 path: path + npmPlugin + '/settings',
408 fields: { settings },
409 token: server.accessToken,
410 statusCodeExpected: 204
415 describe('When installing/updating/uninstalling a plugin', function () {
416 const path = '/api/v1/plugins/'
418 it('Should fail with an invalid token', async function () {
419 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
420 await makePostBodyRequest({
423 fields: { npmName: npmPlugin },
425 statusCodeExpected: 401
430 it('Should fail if the user is not an administrator', async function () {
431 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
432 await makePostBodyRequest({
435 fields: { npmName: npmPlugin },
436 token: userAccessToken,
437 statusCodeExpected: 403
442 it('Should fail with an invalid npm name', async function () {
443 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
444 await makePostBodyRequest({
447 fields: { npmName: 'toto' },
448 token: server.accessToken,
449 statusCodeExpected: 400
453 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
454 await makePostBodyRequest({
457 fields: { npmName: 'peertube-plugin-TOTO' },
458 token: server.accessToken,
459 statusCodeExpected: 400
464 it('Should succeed with the correct parameters', async function () {
466 { suffix: 'install', status: 200 },
467 { suffix: 'update', status: 200 },
468 { suffix: 'uninstall', status: 204 }
471 for (const obj of it) {
472 await makePostBodyRequest({
474 path: path + obj.suffix,
475 fields: { npmName: npmPlugin },
476 token: server.accessToken,
477 statusCodeExpected: obj.status
483 after(async function () {
484 await cleanupTests([ server ])