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