Fix misc tests
[oweals/peertube.git] / server / tests / api / server / plugins.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6   cleanupTests,
7   closeAllSequelize,
8   flushAndRunServer,
9   getConfig, getMyUserInformation, getPluginPackageJSON,
10   getPlugin,
11   getPluginRegisteredSettings,
12   getPluginsCSS,
13   installPlugin, killallServers,
14   listAvailablePlugins,
15   listPlugins, reRunServer,
16   ServerInfo,
17   setAccessTokensToServers,
18   setPluginVersion, uninstallPlugin,
19   updateCustomSubConfig, updateMyUser, updatePluginPackageJSON, updatePlugin,
20   updatePluginSettings,
21   wait, getPublicSettings
22 } from '../../../../shared/extra-utils'
23 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
24 import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
25 import { ServerConfig } from '../../../../shared/models/server'
26 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
27 import { User } from '../../../../shared/models/users'
28 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
29 import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
30 import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
31
32 const expect = chai.expect
33
34 describe('Test plugins', function () {
35   let server: ServerInfo = null
36
37   before(async function () {
38     this.timeout(30000)
39
40     const configOverride = {
41       plugins: {
42         index: { check_latest_versions_interval: '5 seconds' }
43       }
44     }
45     server = await flushAndRunServer(1, configOverride)
46     await setAccessTokensToServers([ server ])
47   })
48
49   it('Should list and search available plugins and themes', async function () {
50     this.timeout(30000)
51
52     {
53       const res = await listAvailablePlugins({
54         url: server.url,
55         accessToken: server.accessToken,
56         count: 1,
57         start: 0,
58         pluginType: PluginType.THEME,
59         search: 'background-red'
60       })
61
62       expect(res.body.total).to.be.at.least(1)
63       expect(res.body.data).to.have.lengthOf(1)
64     }
65
66     {
67       const res1 = await listAvailablePlugins({
68         url: server.url,
69         accessToken: server.accessToken,
70         count: 2,
71         start: 0,
72         sort: 'npmName'
73       })
74       const data1: PeerTubePluginIndex[] = res1.body.data
75
76       expect(res1.body.total).to.be.at.least(2)
77       expect(data1).to.have.lengthOf(2)
78
79       const res2 = await listAvailablePlugins({
80         url: server.url,
81         accessToken: server.accessToken,
82         count: 2,
83         start: 0,
84         sort: '-npmName'
85       })
86       const data2: PeerTubePluginIndex[] = res2.body.data
87
88       expect(res2.body.total).to.be.at.least(2)
89       expect(data2).to.have.lengthOf(2)
90
91       expect(data1[0].npmName).to.not.equal(data2[ 0 ].npmName)
92     }
93
94     {
95       const res = await listAvailablePlugins({
96         url: server.url,
97         accessToken: server.accessToken,
98         count: 10,
99         start: 0,
100         pluginType: PluginType.THEME,
101         search: 'background-red',
102         currentPeerTubeEngine: '1.0.0'
103       })
104       const data: PeerTubePluginIndex[] = res.body.data
105
106       const p = data.find(p => p.npmName === 'peertube-theme-background-red')
107       expect(p).to.be.undefined
108     }
109   })
110
111   it('Should have an empty global css', async function () {
112     const res = await getPluginsCSS(server.url)
113
114     expect(res.text).to.be.empty
115   })
116
117   it('Should install a plugin and a theme', async function () {
118     this.timeout(30000)
119
120     await installPlugin({
121       url: server.url,
122       accessToken: server.accessToken,
123       npmName: 'peertube-plugin-hello-world'
124     })
125
126     await installPlugin({
127       url: server.url,
128       accessToken: server.accessToken,
129       npmName: 'peertube-theme-background-red'
130     })
131   })
132
133   it('Should have the correct global css', async function () {
134     const res = await getPluginsCSS(server.url)
135
136     expect(res.text).to.contain('--mainBackgroundColor')
137   })
138
139   it('Should have the plugin loaded in the configuration', async function () {
140     const res = await getConfig(server.url)
141     const config: ServerConfig = res.body
142
143     const theme = config.theme.registered.find(r => r.name === 'background-red')
144     expect(theme).to.not.be.undefined
145
146     const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
147     expect(plugin).to.not.be.undefined
148   })
149
150   it('Should update the default theme in the configuration', async function () {
151     await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
152
153     const res = await getConfig(server.url)
154     const config: ServerConfig = res.body
155
156     expect(config.theme.default).to.equal('background-red')
157   })
158
159   it('Should update my default theme', async function () {
160     await updateMyUser({
161       url: server.url,
162       accessToken: server.accessToken,
163       theme: 'background-red'
164     })
165
166     const res = await getMyUserInformation(server.url, server.accessToken)
167     expect((res.body as User).theme).to.equal('background-red')
168   })
169
170   it('Should list plugins and themes', async function () {
171     {
172       const res = await listPlugins({
173         url: server.url,
174         accessToken: server.accessToken,
175         count: 1,
176         start: 0,
177         pluginType: PluginType.THEME
178       })
179       const data: PeerTubePlugin[] = res.body.data
180
181       expect(res.body.total).to.be.at.least(1)
182       expect(data).to.have.lengthOf(1)
183       expect(data[0].name).to.equal('background-red')
184     }
185
186     {
187       const res = await listPlugins({
188         url: server.url,
189         accessToken: server.accessToken,
190         count: 2,
191         start: 0,
192         sort: 'name'
193       })
194       const data: PeerTubePlugin[] = res.body.data
195
196       expect(data[0].name).to.equal('background-red')
197       expect(data[1].name).to.equal('hello-world')
198     }
199
200     {
201       const res = await listPlugins({
202         url: server.url,
203         accessToken: server.accessToken,
204         count: 2,
205         start: 1,
206         sort: 'name'
207       })
208       const data: PeerTubePlugin[] = res.body.data
209
210       expect(data[0].name).to.equal('hello-world')
211     }
212   })
213
214   it('Should get registered settings', async function () {
215     const res = await getPluginRegisteredSettings({
216       url: server.url,
217       accessToken: server.accessToken,
218       npmName: 'peertube-plugin-hello-world'
219     })
220
221     const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
222
223     expect(registeredSettings).to.have.length.at.least(1)
224
225     const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
226     expect(adminNameSettings).to.not.be.undefined
227   })
228
229   it('Should get public settings', async function () {
230     const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
231
232     const publicSettings = (res.body as PublicServerSetting).publicSettings
233
234     expect(Object.keys(publicSettings)).to.have.lengthOf(1)
235     expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
236     expect(publicSettings['user-name']).to.be.null
237   })
238
239   it('Should update the settings', async function () {
240     const settings = {
241       'admin-name': 'Cid'
242     }
243
244     await updatePluginSettings({
245       url: server.url,
246       accessToken: server.accessToken,
247       npmName: 'peertube-plugin-hello-world',
248       settings
249     })
250   })
251
252   it('Should get a plugin and a theme', async function () {
253     {
254       const res = await getPlugin({
255         url: server.url,
256         accessToken: server.accessToken,
257         npmName: 'peertube-plugin-hello-world'
258       })
259
260       const plugin: PeerTubePlugin = res.body
261
262       expect(plugin.type).to.equal(PluginType.PLUGIN)
263       expect(plugin.name).to.equal('hello-world')
264       expect(plugin.description).to.exist
265       expect(plugin.homepage).to.exist
266       expect(plugin.uninstalled).to.be.false
267       expect(plugin.enabled).to.be.true
268       expect(plugin.description).to.exist
269       expect(plugin.version).to.exist
270       expect(plugin.peertubeEngine).to.exist
271       expect(plugin.createdAt).to.exist
272
273       expect(plugin.settings).to.not.be.undefined
274       expect(plugin.settings['admin-name']).to.equal('Cid')
275     }
276
277     {
278       const res = await getPlugin({
279         url: server.url,
280         accessToken: server.accessToken,
281         npmName: 'peertube-theme-background-red'
282       })
283
284       const plugin: PeerTubePlugin = res.body
285
286       expect(plugin.type).to.equal(PluginType.THEME)
287       expect(plugin.name).to.equal('background-red')
288       expect(plugin.description).to.exist
289       expect(plugin.homepage).to.exist
290       expect(plugin.uninstalled).to.be.false
291       expect(plugin.enabled).to.be.true
292       expect(plugin.description).to.exist
293       expect(plugin.version).to.exist
294       expect(plugin.peertubeEngine).to.exist
295       expect(plugin.createdAt).to.exist
296
297       expect(plugin.settings).to.be.null
298     }
299   })
300
301   it('Should update the plugin and the theme', async function () {
302     this.timeout(30000)
303
304     // Wait the scheduler that get the latest plugins versions
305     await wait(6000)
306
307     // Fake update our plugin version
308     await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
309
310     // Fake update package.json
311     const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
312     const oldVersion = packageJSON.version
313
314     packageJSON.version = '0.0.1'
315     await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
316
317     // Restart the server to take into account this change
318     killallServers([ server ])
319     await reRunServer(server)
320
321     {
322       const res = await listPlugins({
323         url: server.url,
324         accessToken: server.accessToken,
325         pluginType: PluginType.PLUGIN
326       })
327
328       const plugin: PeerTubePlugin = res.body.data[0]
329
330       expect(plugin.version).to.equal('0.0.1')
331       expect(plugin.latestVersion).to.exist
332       expect(plugin.latestVersion).to.not.equal('0.0.1')
333     }
334
335     {
336       await updatePlugin({
337         url: server.url,
338         accessToken: server.accessToken,
339         npmName: 'peertube-plugin-hello-world'
340       })
341
342       const res = await listPlugins({
343         url: server.url,
344         accessToken: server.accessToken,
345         pluginType: PluginType.PLUGIN
346       })
347
348       const plugin: PeerTubePlugin = res.body.data[0]
349
350       expect(plugin.version).to.equal(oldVersion)
351
352       const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
353       expect(updatedPackageJSON.version).to.equal(oldVersion)
354     }
355   })
356
357   it('Should uninstall the plugin', async function () {
358     await uninstallPlugin({
359       url: server.url,
360       accessToken: server.accessToken,
361       npmName: 'peertube-plugin-hello-world'
362     })
363
364     const res = await listPlugins({
365       url: server.url,
366       accessToken: server.accessToken,
367       pluginType: PluginType.PLUGIN
368     })
369
370     expect(res.body.total).to.equal(0)
371     expect(res.body.data).to.have.lengthOf(0)
372   })
373
374   it('Should have an empty global css', async function () {
375     const res = await getPluginsCSS(server.url)
376
377     expect(res.text).to.be.empty
378   })
379
380   it('Should list uninstalled plugins', async function () {
381     const res = await listPlugins({
382       url: server.url,
383       accessToken: server.accessToken,
384       pluginType: PluginType.PLUGIN,
385       uninstalled: true
386     })
387
388     expect(res.body.total).to.equal(1)
389     expect(res.body.data).to.have.lengthOf(1)
390
391     const plugin: PeerTubePlugin = res.body.data[0]
392     expect(plugin.name).to.equal('hello-world')
393     expect(plugin.enabled).to.be.false
394     expect(plugin.uninstalled).to.be.true
395   })
396
397   it('Should uninstall the theme', async function () {
398     await uninstallPlugin({
399       url: server.url,
400       accessToken: server.accessToken,
401       npmName: 'peertube-theme-background-red'
402     })
403   })
404
405   it('Should have updated the configuration', async function () {
406     // get /config (default theme + registered themes + registered plugins)
407     const res = await getConfig(server.url)
408     const config: ServerConfig = res.body
409
410     expect(config.theme.default).to.equal('default')
411
412     const theme = config.theme.registered.find(r => r.name === 'background-red')
413     expect(theme).to.be.undefined
414
415     const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
416     expect(plugin).to.be.undefined
417   })
418
419   it('Should have updated the user theme', async function () {
420     const res = await getMyUserInformation(server.url, server.accessToken)
421     expect((res.body as User).theme).to.equal('instance-default')
422   })
423
424   after(async function () {
425     await closeAllSequelize([ server ])
426     await cleanupTests([ server ])
427   })
428 })