]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/plugins.ts
Automatically rebuild native modules on ABI change
[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
C
2
3import 'mocha'
4import * as chai from 'chai'
c795e196
C
5import { pathExists, remove } from 'fs-extra'
6import { join } from 'path'
c55e3d72
C
7import { testHelloWorldRegisteredSettings } from '@server/tests/shared'
8import { wait } from '@shared/core-utils'
9import { HttpStatusCode, PluginType } from '@shared/models'
9b474844
C
10import {
11 cleanupTests,
254d3579 12 createSingleServer,
a1587156 13 killallServers,
c795e196 14 makeGetRequest,
254d3579 15 PeerTubeServer,
4c7e60bc 16 PluginsCommand,
c55e3d72 17 setAccessTokensToServers
bf54587a 18} from '@shared/server-commands'
9b474844
C
19
20const expect = chai.expect
21
22describe('Test plugins', function () {
254d3579 23 let server: PeerTubeServer = null
ae2abfd3 24 let command: PluginsCommand
9b474844
C
25
26 before(async function () {
27 this.timeout(30000)
28
09071200
C
29 const configOverride = {
30 plugins: {
31 index: { check_latest_versions_interval: '5 seconds' }
32 }
33 }
254d3579 34 server = await createSingleServer(1, configOverride)
9b474844 35 await setAccessTokensToServers([ server ])
a5461888 36
89d241a7 37 command = server.plugins
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
C
191 it('Should have watched settings changes', async function () {
192 this.timeout(10000)
193
89d241a7 194 await server.servers.waitUntilLog('Settings changed!')
a5896799
C
195 })
196
09071200
C
197 it('Should get a plugin and a theme', async function () {
198 {
ae2abfd3 199 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
09071200
C
200
201 expect(plugin.type).to.equal(PluginType.PLUGIN)
202 expect(plugin.name).to.equal('hello-world')
203 expect(plugin.description).to.exist
204 expect(plugin.homepage).to.exist
205 expect(plugin.uninstalled).to.be.false
206 expect(plugin.enabled).to.be.true
207 expect(plugin.description).to.exist
208 expect(plugin.version).to.exist
209 expect(plugin.peertubeEngine).to.exist
210 expect(plugin.createdAt).to.exist
211
212 expect(plugin.settings).to.not.be.undefined
213 expect(plugin.settings['admin-name']).to.equal('Cid')
214 }
215
216 {
ae2abfd3 217 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
9b474844 218
09071200
C
219 expect(plugin.type).to.equal(PluginType.THEME)
220 expect(plugin.name).to.equal('background-red')
221 expect(plugin.description).to.exist
222 expect(plugin.homepage).to.exist
223 expect(plugin.uninstalled).to.be.false
224 expect(plugin.enabled).to.be.true
225 expect(plugin.description).to.exist
226 expect(plugin.version).to.exist
227 expect(plugin.peertubeEngine).to.exist
228 expect(plugin.createdAt).to.exist
229
230 expect(plugin.settings).to.be.null
231 }
9b474844
C
232 })
233
234 it('Should update the plugin and the theme', async function () {
2769876f 235 this.timeout(180000)
09071200
C
236
237 // Wait the scheduler that get the latest plugins versions
238 await wait(6000)
239
2769876f
C
240 async function testUpdate (type: 'plugin' | 'theme', name: string) {
241 // Fake update our plugin version
242 await server.sql.setPluginVersion(name, '0.0.1')
09071200 243
2769876f
C
244 // Fake update package.json
245 const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
246 const oldVersion = packageJSON.version
09071200 247
2769876f
C
248 packageJSON.version = '0.0.1'
249 await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
09071200 250
2769876f
C
251 // Restart the server to take into account this change
252 await killallServers([ server ])
253 await server.run()
09071200 254
2769876f
C
255 const checkConfig = async (version: string) => {
256 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
257 expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
258 }
259 }
09071200 260
2769876f
C
261 const getPluginFromAPI = async () => {
262 const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
09071200 263
2769876f
C
264 return body.data.find(p => p.name === name)
265 }
09071200 266
2769876f
C
267 {
268 const plugin = await getPluginFromAPI()
269 expect(plugin.version).to.equal('0.0.1')
270 expect(plugin.latestVersion).to.exist
271 expect(plugin.latestVersion).to.not.equal('0.0.1')
272
273 await checkConfig('0.0.1')
274 }
275
276 {
277 await command.update({ npmName: `peertube-${type}-${name}` })
09071200 278
2769876f
C
279 const plugin = await getPluginFromAPI()
280 expect(plugin.version).to.equal(oldVersion)
09071200 281
2769876f
C
282 const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
283 expect(updatedPackageJSON.version).to.equal(oldVersion)
284
285 await checkConfig(oldVersion)
286 }
09071200 287 }
2769876f
C
288
289 await testUpdate('theme', 'background-red')
290 await testUpdate('plugin', 'hello-world')
9b474844
C
291 })
292
293 it('Should uninstall the plugin', async function () {
ae2abfd3 294 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
09071200 295
ae2abfd3
C
296 const body = await command.list({ pluginType: PluginType.PLUGIN })
297 expect(body.total).to.equal(0)
298 expect(body.data).to.have.lengthOf(0)
9b474844
C
299 })
300
9b474844 301 it('Should list uninstalled plugins', async function () {
ae2abfd3
C
302 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
303 expect(body.total).to.equal(1)
304 expect(body.data).to.have.lengthOf(1)
09071200 305
ae2abfd3 306 const plugin = body.data[0]
09071200
C
307 expect(plugin.name).to.equal('hello-world')
308 expect(plugin.enabled).to.be.false
309 expect(plugin.uninstalled).to.be.true
9b474844
C
310 })
311
312 it('Should uninstall the theme', async function () {
ae2abfd3 313 await command.uninstall({ npmName: 'peertube-theme-background-red' })
9b474844
C
314 })
315
316 it('Should have updated the configuration', async function () {
2769876f
C
317 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
318 expect(config.theme.default).to.equal('default')
09071200 319
2769876f
C
320 const theme = config.theme.registered.find(r => r.name === 'background-red')
321 expect(theme).to.be.undefined
09071200 322
2769876f
C
323 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
324 expect(plugin).to.be.undefined
325 }
09071200
C
326 })
327
328 it('Should have updated the user theme', async function () {
89d241a7 329 const user = await server.users.getMyInfo()
7926c5f9 330 expect(user.theme).to.equal('instance-default')
9b474844
C
331 })
332
9c2e051c
C
333 it('Should not install a broken plugin', async function () {
334 this.timeout(60000)
335
336 async function check () {
ae2abfd3
C
337 const body = await command.list({ pluginType: PluginType.PLUGIN })
338 const plugins = body.data
9c2e051c
C
339 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
340 }
341
ae2abfd3
C
342 await command.install({
343 path: PluginsCommand.getPluginTestPath('-broken'),
9c2e051c
C
344 expectedStatus: HttpStatusCode.BAD_REQUEST_400
345 })
346
347 await check()
348
9293139f 349 await killallServers([ server ])
254d3579 350 await server.run()
9c2e051c
C
351
352 await check()
353 })
354
c795e196
C
355 it('Should rebuild native modules on Node ABI change', async function () {
356 await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
357
358 await makeGetRequest({
359 url: server.url,
360 path: '/plugins/test-native/router',
361 expectedStatus: HttpStatusCode.NO_CONTENT_204
362 })
363
364 const query = `UPDATE "application" SET "nodeABIVersion" = 1`
365 await server.sql.updateQuery(query)
366
367 const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
368 await remove(join(baseNativeModule, 'build'))
369 await remove(join(baseNativeModule, 'prebuilds'))
370
371 await server.kill()
372 await server.run()
373
374 await pathExists(join(baseNativeModule, 'build'))
375 await pathExists(join(baseNativeModule, 'prebuilds'))
376
377 await makeGetRequest({
378 url: server.url,
379 path: '/plugins/test-native/router',
380 expectedStatus: HttpStatusCode.NO_CONTENT_204
381 })
382 })
383
9b474844
C
384 after(async function () {
385 await cleanupTests([ server ])
386 })
387})