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
158 it('Should fail with an invalid token', async function () {
159 await makeGetRequest({
164 statusCodeExpected: 401
168 it('Should fail if the user is not an administrator', async function () {
169 await makeGetRequest({
172 token: userAccessToken,
174 statusCodeExpected: 403
178 it('Should fail with a bad start pagination', async function () {
179 await checkBadStartPagination(server.url, path, server.accessToken)
182 it('Should fail with a bad count pagination', async function () {
183 await checkBadCountPagination(server.url, path, server.accessToken)
186 it('Should fail with an incorrect sort', async function () {
187 await checkBadSortPagination(server.url, path, server.accessToken)
190 it('Should fail with an invalid plugin type', async function () {
191 const query = immutableAssign(baseQuery, { pluginType: 5 })
193 await makeGetRequest({
196 token: server.accessToken,
201 it('Should success with the correct parameters', async function () {
202 await makeGetRequest({
205 token: server.accessToken,
207 statusCodeExpected: 200
212 describe('When listing local plugins/themes', function () {
213 const path = '/api/v1/plugins'
215 pluginType: PluginType.THEME
218 it('Should fail with an invalid token', async function () {
219 await makeGetRequest({
224 statusCodeExpected: 401
228 it('Should fail if the user is not an administrator', async function () {
229 await makeGetRequest({
232 token: userAccessToken,
234 statusCodeExpected: 403
238 it('Should fail with a bad start pagination', async function () {
239 await checkBadStartPagination(server.url, path, server.accessToken)
242 it('Should fail with a bad count pagination', async function () {
243 await checkBadCountPagination(server.url, path, server.accessToken)
246 it('Should fail with an incorrect sort', async function () {
247 await checkBadSortPagination(server.url, path, server.accessToken)
250 it('Should fail with an invalid plugin type', async function () {
251 const query = immutableAssign(baseQuery, { pluginType: 5 })
253 await makeGetRequest({
256 token: server.accessToken,
261 it('Should success with the correct parameters', async function () {
262 await makeGetRequest({
265 token: server.accessToken,
267 statusCodeExpected: 200
272 describe('When getting a plugin or the registered settings', function () {
273 const path = '/api/v1/plugins/'
275 it('Should fail with an invalid token', async function () {
276 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
277 await makeGetRequest({
281 statusCodeExpected: 401
286 it('Should fail if the user is not an administrator', async function () {
287 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
288 await makeGetRequest({
291 token: userAccessToken,
292 statusCodeExpected: 403
297 it('Should fail with an invalid npm name', async function () {
298 for (const suffix of [ 'toto', 'toto/registered-settings' ]) {
299 await makeGetRequest({
302 token: server.accessToken,
303 statusCodeExpected: 400
307 for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings' ]) {
308 await makeGetRequest({
311 token: server.accessToken,
312 statusCodeExpected: 400
317 it('Should fail with an unknown plugin', async function () {
318 for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings' ]) {
319 await makeGetRequest({
322 token: server.accessToken,
323 statusCodeExpected: 404
328 it('Should succeed with the correct parameters', async function () {
329 for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
330 await makeGetRequest({
333 token: server.accessToken,
334 statusCodeExpected: 200
340 describe('When updating plugin settings', function () {
341 const path = '/api/v1/plugins/'
342 const settings = { setting1: 'value1' }
344 it('Should fail with an invalid token', async function () {
345 await makePutBodyRequest({
347 path: path + npmPlugin + '/settings',
348 fields: { settings },
350 statusCodeExpected: 401
354 it('Should fail if the user is not an administrator', async function () {
355 await makePutBodyRequest({
357 path: path + npmPlugin + '/settings',
358 fields: { settings },
359 token: userAccessToken,
360 statusCodeExpected: 403
364 it('Should fail with an invalid npm name', async function () {
365 await makePutBodyRequest({
367 path: path + 'toto/settings',
368 fields: { settings },
369 token: server.accessToken,
370 statusCodeExpected: 400
373 await makePutBodyRequest({
375 path: path + 'peertube-plugin-TOTO/settings',
376 fields: { settings },
377 token: server.accessToken,
378 statusCodeExpected: 400
382 it('Should fail with an unknown plugin', async function () {
383 await makePutBodyRequest({
385 path: path + 'peertube-plugin-toto/settings',
386 fields: { settings },
387 token: server.accessToken,
388 statusCodeExpected: 404
392 it('Should succeed with the correct parameters', async function () {
393 await makePutBodyRequest({
395 path: path + npmPlugin + '/settings',
396 fields: { settings },
397 token: server.accessToken,
398 statusCodeExpected: 204
403 describe('When installing/updating/uninstalling a plugin', function () {
404 const path = '/api/v1/plugins/'
406 it('Should fail with an invalid token', async function () {
407 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
408 await makePostBodyRequest({
411 fields: { npmName: npmPlugin },
413 statusCodeExpected: 401
418 it('Should fail if the user is not an administrator', async function () {
419 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
420 await makePostBodyRequest({
423 fields: { npmName: npmPlugin },
424 token: userAccessToken,
425 statusCodeExpected: 403
430 it('Should fail with an invalid npm name', async function () {
431 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
432 await makePostBodyRequest({
435 fields: { npmName: 'toto' },
436 token: server.accessToken,
437 statusCodeExpected: 400
441 for (const suffix of [ 'install', 'update', 'uninstall' ]) {
442 await makePostBodyRequest({
445 fields: { npmName: 'peertube-plugin-TOTO' },
446 token: server.accessToken,
447 statusCodeExpected: 400
452 it('Should succeed with the correct parameters', async function () {
454 { suffix: 'install', status: 200 },
455 { suffix: 'update', status: 200 },
456 { suffix: 'uninstall', status: 204 }
459 for (const obj of it) {
460 await makePostBodyRequest({
462 path: path + obj.suffix,
463 fields: { npmName: npmPlugin },
464 token: server.accessToken,
465 statusCodeExpected: obj.status
471 after(async function () {
472 await cleanupTests([ server ])