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