]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/plugins.ts
Remove low timeouts
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / plugins.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
9b474844 2
86347717 3import { expect } from 'chai'
c795e196
C
4import { pathExists, remove } from 'fs-extra'
5import { join } from 'path'
d102de1b 6import { SQLCommand, testHelloWorldRegisteredSettings } from '@server/tests/shared'
c55e3d72
C
7import { wait } from '@shared/core-utils'
8import { HttpStatusCode, PluginType } from '@shared/models'
9b474844
C
9import {
10 cleanupTests,
254d3579 11 createSingleServer,
a1587156 12 killallServers,
c795e196 13 makeGetRequest,
254d3579 14 PeerTubeServer,
4c7e60bc 15 PluginsCommand,
c55e3d72 16 setAccessTokensToServers
bf54587a 17} from '@shared/server-commands'
9b474844 18
9b474844 19describe('Test plugins', function () {
d102de1b
C
20 let server: PeerTubeServer
21 let sqlCommand: SQLCommand
ae2abfd3 22 let command: PluginsCommand
9b474844
C
23
24 before(async function () {
25 this.timeout(30000)
26
09071200
C
27 const configOverride = {
28 plugins: {
29 index: { check_latest_versions_interval: '5 seconds' }
30 }
31 }
254d3579 32 server = await createSingleServer(1, configOverride)
9b474844 33 await setAccessTokensToServers([ server ])
a5461888 34
89d241a7 35 command = server.plugins
d102de1b
C
36
37 sqlCommand = new SQLCommand(server)
09071200
C
38 })
39
40 it('Should list and search available plugins and themes', async function () {
41 this.timeout(30000)
9b474844
C
42
43 {
ae2abfd3 44 const body = await command.listAvailable({
09071200
C
45 count: 1,
46 start: 0,
47 pluginType: PluginType.THEME,
48 search: 'background-red'
49 })
50
ae2abfd3
C
51 expect(body.total).to.be.at.least(1)
52 expect(body.data).to.have.lengthOf(1)
9b474844
C
53 }
54
55 {
ae2abfd3 56 const body1 = await command.listAvailable({
09071200
C
57 count: 2,
58 start: 0,
59 sort: 'npmName'
60 })
ae2abfd3 61 expect(body1.total).to.be.at.least(2)
09071200 62
ae2abfd3 63 const data1 = body1.data
09071200
C
64 expect(data1).to.have.lengthOf(2)
65
ae2abfd3 66 const body2 = await command.listAvailable({
09071200
C
67 count: 2,
68 start: 0,
69 sort: '-npmName'
70 })
ae2abfd3 71 expect(body2.total).to.be.at.least(2)
09071200 72
ae2abfd3 73 const data2 = body2.data
09071200
C
74 expect(data2).to.have.lengthOf(2)
75
a1587156 76 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
9b474844 77 }
9b474844 78
09071200 79 {
ae2abfd3 80 const body = await command.listAvailable({
09071200
C
81 count: 10,
82 start: 0,
83 pluginType: PluginType.THEME,
84 search: 'background-red',
85 currentPeerTubeEngine: '1.0.0'
86 })
9b474844 87
ae2abfd3 88 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
09071200
C
89 expect(p).to.be.undefined
90 }
9b474844
C
91 })
92
9b474844 93 it('Should install a plugin and a theme', async function () {
09071200
C
94 this.timeout(30000)
95
ae2abfd3
C
96 await command.install({ npmName: 'peertube-plugin-hello-world' })
97 await command.install({ npmName: 'peertube-theme-background-red' })
9b474844
C
98 })
99
9b474844 100 it('Should have the plugin loaded in the configuration', async function () {
2769876f
C
101 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
102 const theme = config.theme.registered.find(r => r.name === 'background-red')
103 expect(theme).to.not.be.undefined
104 expect(theme.npmName).to.equal('peertube-theme-background-red')
105
106 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
107 expect(plugin).to.not.be.undefined
108 expect(plugin.npmName).to.equal('peertube-plugin-hello-world')
109 }
9b474844
C
110 })
111
112 it('Should update the default theme in the configuration', async function () {
89d241a7 113 await server.config.updateCustomSubConfig({
65e6e260
C
114 newConfig: {
115 theme: { default: 'background-red' }
116 }
117 })
09071200 118
2769876f
C
119 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
120 expect(config.theme.default).to.equal('background-red')
121 }
9b474844
C
122 })
123
09071200 124 it('Should update my default theme', async function () {
89d241a7 125 await server.users.updateMe({ theme: 'background-red' })
09071200 126
89d241a7 127 const user = await server.users.getMyInfo()
7926c5f9 128 expect(user.theme).to.equal('background-red')
9b474844
C
129 })
130
09071200
C
131 it('Should list plugins and themes', async function () {
132 {
ae2abfd3 133 const body = await command.list({
09071200
C
134 count: 1,
135 start: 0,
136 pluginType: PluginType.THEME
137 })
ae2abfd3 138 expect(body.total).to.be.at.least(1)
09071200 139
ae2abfd3 140 const data = body.data
09071200
C
141 expect(data).to.have.lengthOf(1)
142 expect(data[0].name).to.equal('background-red')
143 }
144
145 {
dd0ebb71 146 const { data } = await command.list({
09071200
C
147 count: 2,
148 start: 0,
149 sort: 'name'
150 })
09071200
C
151
152 expect(data[0].name).to.equal('background-red')
153 expect(data[1].name).to.equal('hello-world')
154 }
155
156 {
ae2abfd3 157 const body = await command.list({
09071200
C
158 count: 2,
159 start: 1,
160 sort: 'name'
161 })
09071200 162
ae2abfd3 163 expect(body.data[0].name).to.equal('hello-world')
09071200 164 }
9b474844
C
165 })
166
167 it('Should get registered settings', async function () {
353f8bc0 168 await testHelloWorldRegisteredSettings(server)
9b474844
C
169 })
170
ba211e73 171 it('Should get public settings', async function () {
ae2abfd3
C
172 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
173 const publicSettings = body.publicSettings
ba211e73
C
174
175 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
176 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
177 expect(publicSettings['user-name']).to.be.null
178 })
179
9b474844 180 it('Should update the settings', async function () {
09071200
C
181 const settings = {
182 'admin-name': 'Cid'
183 }
184
ae2abfd3 185 await command.updateSettings({
09071200
C
186 npmName: 'peertube-plugin-hello-world',
187 settings
188 })
189 })
190
a5896799 191 it('Should have watched settings changes', async function () {
89d241a7 192 await server.servers.waitUntilLog('Settings changed!')
a5896799
C
193 })
194
09071200
C
195 it('Should get a plugin and a theme', async function () {
196 {
ae2abfd3 197 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
09071200
C
198
199 expect(plugin.type).to.equal(PluginType.PLUGIN)
200 expect(plugin.name).to.equal('hello-world')
201 expect(plugin.description).to.exist
202 expect(plugin.homepage).to.exist
203 expect(plugin.uninstalled).to.be.false
204 expect(plugin.enabled).to.be.true
205 expect(plugin.description).to.exist
206 expect(plugin.version).to.exist
207 expect(plugin.peertubeEngine).to.exist
208 expect(plugin.createdAt).to.exist
209
210 expect(plugin.settings).to.not.be.undefined
211 expect(plugin.settings['admin-name']).to.equal('Cid')
212 }
213
214 {
ae2abfd3 215 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
9b474844 216
09071200
C
217 expect(plugin.type).to.equal(PluginType.THEME)
218 expect(plugin.name).to.equal('background-red')
219 expect(plugin.description).to.exist
220 expect(plugin.homepage).to.exist
221 expect(plugin.uninstalled).to.be.false
222 expect(plugin.enabled).to.be.true
223 expect(plugin.description).to.exist
224 expect(plugin.version).to.exist
225 expect(plugin.peertubeEngine).to.exist
226 expect(plugin.createdAt).to.exist
227
228 expect(plugin.settings).to.be.null
229 }
9b474844
C
230 })
231
232 it('Should update the plugin and the theme', async function () {
2769876f 233 this.timeout(180000)
09071200
C
234
235 // Wait the scheduler that get the latest plugins versions
236 await wait(6000)
237
2769876f
C
238 async function testUpdate (type: 'plugin' | 'theme', name: string) {
239 // Fake update our plugin version
d102de1b 240 await sqlCommand.setPluginVersion(name, '0.0.1')
09071200 241
2769876f
C
242 // Fake update package.json
243 const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
244 const oldVersion = packageJSON.version
09071200 245
2769876f
C
246 packageJSON.version = '0.0.1'
247 await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
09071200 248
2769876f
C
249 // Restart the server to take into account this change
250 await killallServers([ server ])
251 await server.run()
09071200 252
2769876f
C
253 const checkConfig = async (version: string) => {
254 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
255 expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
256 }
257 }
09071200 258
2769876f
C
259 const getPluginFromAPI = async () => {
260 const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
09071200 261
2769876f
C
262 return body.data.find(p => p.name === name)
263 }
09071200 264
2769876f
C
265 {
266 const plugin = await getPluginFromAPI()
267 expect(plugin.version).to.equal('0.0.1')
268 expect(plugin.latestVersion).to.exist
269 expect(plugin.latestVersion).to.not.equal('0.0.1')
270
271 await checkConfig('0.0.1')
272 }
273
274 {
275 await command.update({ npmName: `peertube-${type}-${name}` })
09071200 276
2769876f
C
277 const plugin = await getPluginFromAPI()
278 expect(plugin.version).to.equal(oldVersion)
09071200 279
2769876f
C
280 const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
281 expect(updatedPackageJSON.version).to.equal(oldVersion)
282
283 await checkConfig(oldVersion)
284 }
09071200 285 }
2769876f
C
286
287 await testUpdate('theme', 'background-red')
288 await testUpdate('plugin', 'hello-world')
9b474844
C
289 })
290
291 it('Should uninstall the plugin', async function () {
ae2abfd3 292 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
09071200 293
ae2abfd3
C
294 const body = await command.list({ pluginType: PluginType.PLUGIN })
295 expect(body.total).to.equal(0)
296 expect(body.data).to.have.lengthOf(0)
9b474844
C
297 })
298
9b474844 299 it('Should list uninstalled plugins', async function () {
ae2abfd3
C
300 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
301 expect(body.total).to.equal(1)
302 expect(body.data).to.have.lengthOf(1)
09071200 303
ae2abfd3 304 const plugin = body.data[0]
09071200
C
305 expect(plugin.name).to.equal('hello-world')
306 expect(plugin.enabled).to.be.false
307 expect(plugin.uninstalled).to.be.true
9b474844
C
308 })
309
310 it('Should uninstall the theme', async function () {
ae2abfd3 311 await command.uninstall({ npmName: 'peertube-theme-background-red' })
9b474844
C
312 })
313
314 it('Should have updated the configuration', async function () {
2769876f
C
315 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
316 expect(config.theme.default).to.equal('default')
09071200 317
2769876f
C
318 const theme = config.theme.registered.find(r => r.name === 'background-red')
319 expect(theme).to.be.undefined
09071200 320
2769876f
C
321 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
322 expect(plugin).to.be.undefined
323 }
09071200
C
324 })
325
326 it('Should have updated the user theme', async function () {
89d241a7 327 const user = await server.users.getMyInfo()
7926c5f9 328 expect(user.theme).to.equal('instance-default')
9b474844
C
329 })
330
9c2e051c
C
331 it('Should not install a broken plugin', async function () {
332 this.timeout(60000)
333
334 async function check () {
ae2abfd3
C
335 const body = await command.list({ pluginType: PluginType.PLUGIN })
336 const plugins = body.data
9c2e051c
C
337 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
338 }
339
ae2abfd3
C
340 await command.install({
341 path: PluginsCommand.getPluginTestPath('-broken'),
9c2e051c
C
342 expectedStatus: HttpStatusCode.BAD_REQUEST_400
343 })
344
345 await check()
346
9293139f 347 await killallServers([ server ])
254d3579 348 await server.run()
9c2e051c
C
349
350 await check()
351 })
352
c795e196 353 it('Should rebuild native modules on Node ABI change', async function () {
c543e414 354 this.timeout(60000)
94278907
C
355
356 const removeNativeModule = async () => {
357 await remove(join(baseNativeModule, 'build'))
358 await remove(join(baseNativeModule, 'prebuilds'))
359 }
360
c795e196
C
361 await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
362
363 await makeGetRequest({
364 url: server.url,
365 path: '/plugins/test-native/router',
366 expectedStatus: HttpStatusCode.NO_CONTENT_204
367 })
368
369 const query = `UPDATE "application" SET "nodeABIVersion" = 1`
d102de1b 370 await sqlCommand.updateQuery(query)
c795e196
C
371
372 const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
c795e196 373
94278907 374 await removeNativeModule()
c795e196
C
375 await server.kill()
376 await server.run()
377
c543e414
C
378 await wait(3000)
379
94278907
C
380 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.true
381 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.true
c795e196
C
382
383 await makeGetRequest({
384 url: server.url,
385 path: '/plugins/test-native/router',
386 expectedStatus: HttpStatusCode.NO_CONTENT_204
387 })
94278907
C
388
389 await removeNativeModule()
390
391 await server.kill()
392 await server.run()
393
394 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.false
395 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.false
396
397 await makeGetRequest({
398 url: server.url,
399 path: '/plugins/test-native/router',
400 expectedStatus: HttpStatusCode.NOT_FOUND_404
401 })
c795e196
C
402 })
403
9b474844 404 after(async function () {
d102de1b
C
405 await sqlCommand.cleanup()
406
9b474844
C
407 await cleanupTests([ server ])
408 })
409})