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