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