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