1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { pathExists, remove } from 'fs-extra'
5 import { join } from 'path'
6 import { SQLCommand, testHelloWorldRegisteredSettings } from '@server/tests/shared'
7 import { wait } from '@shared/core-utils'
8 import { HttpStatusCode, PluginType } from '@shared/models'
16 setAccessTokensToServers
17 } from '@shared/server-commands'
19 describe('Test plugins', function () {
20 let server: PeerTubeServer
21 let sqlCommand: SQLCommand
22 let command: PluginsCommand
24 before(async function () {
27 const configOverride = {
29 index: { check_latest_versions_interval: '5 seconds' }
32 server = await createSingleServer(1, configOverride)
33 await setAccessTokensToServers([ server ])
35 command = server.plugins
37 sqlCommand = new SQLCommand(server)
40 it('Should list and search available plugins and themes', async function () {
44 const body = await command.listAvailable({
47 pluginType: PluginType.THEME,
48 search: 'background-red'
51 expect(body.total).to.be.at.least(1)
52 expect(body.data).to.have.lengthOf(1)
56 const body1 = await command.listAvailable({
61 expect(body1.total).to.be.at.least(2)
63 const data1 = body1.data
64 expect(data1).to.have.lengthOf(2)
66 const body2 = await command.listAvailable({
71 expect(body2.total).to.be.at.least(2)
73 const data2 = body2.data
74 expect(data2).to.have.lengthOf(2)
76 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
80 const body = await command.listAvailable({
83 pluginType: PluginType.THEME,
84 search: 'background-red',
85 currentPeerTubeEngine: '1.0.0'
88 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
89 expect(p).to.be.undefined
93 it('Should install a plugin and a theme', async function () {
96 await command.install({ npmName: 'peertube-plugin-hello-world' })
97 await command.install({ npmName: 'peertube-theme-background-red' })
100 it('Should have the plugin loaded in the configuration', async function () {
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')
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')
112 it('Should update the default theme in the configuration', async function () {
113 await server.config.updateCustomSubConfig({
115 theme: { default: 'background-red' }
119 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
120 expect(config.theme.default).to.equal('background-red')
124 it('Should update my default theme', async function () {
125 await server.users.updateMe({ theme: 'background-red' })
127 const user = await server.users.getMyInfo()
128 expect(user.theme).to.equal('background-red')
131 it('Should list plugins and themes', async function () {
133 const body = await command.list({
136 pluginType: PluginType.THEME
138 expect(body.total).to.be.at.least(1)
140 const data = body.data
141 expect(data).to.have.lengthOf(1)
142 expect(data[0].name).to.equal('background-red')
146 const { data } = await command.list({
152 expect(data[0].name).to.equal('background-red')
153 expect(data[1].name).to.equal('hello-world')
157 const body = await command.list({
163 expect(body.data[0].name).to.equal('hello-world')
167 it('Should get registered settings', async function () {
168 await testHelloWorldRegisteredSettings(server)
171 it('Should get public settings', async function () {
172 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
173 const publicSettings = body.publicSettings
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
180 it('Should update the settings', async function () {
185 await command.updateSettings({
186 npmName: 'peertube-plugin-hello-world',
191 it('Should have watched settings changes', async function () {
194 await server.servers.waitUntilLog('Settings changed!')
197 it('Should get a plugin and a theme', async function () {
199 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
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
212 expect(plugin.settings).to.not.be.undefined
213 expect(plugin.settings['admin-name']).to.equal('Cid')
217 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
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
230 expect(plugin.settings).to.be.null
234 it('Should update the plugin and the theme', async function () {
237 // Wait the scheduler that get the latest plugins versions
240 async function testUpdate (type: 'plugin' | 'theme', name: string) {
241 // Fake update our plugin version
242 await sqlCommand.setPluginVersion(name, '0.0.1')
244 // Fake update package.json
245 const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
246 const oldVersion = packageJSON.version
248 packageJSON.version = '0.0.1'
249 await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
251 // Restart the server to take into account this change
252 await killallServers([ server ])
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)
261 const getPluginFromAPI = async () => {
262 const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
264 return body.data.find(p => p.name === name)
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')
273 await checkConfig('0.0.1')
277 await command.update({ npmName: `peertube-${type}-${name}` })
279 const plugin = await getPluginFromAPI()
280 expect(plugin.version).to.equal(oldVersion)
282 const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
283 expect(updatedPackageJSON.version).to.equal(oldVersion)
285 await checkConfig(oldVersion)
289 await testUpdate('theme', 'background-red')
290 await testUpdate('plugin', 'hello-world')
293 it('Should uninstall the plugin', async function () {
294 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
296 const body = await command.list({ pluginType: PluginType.PLUGIN })
297 expect(body.total).to.equal(0)
298 expect(body.data).to.have.lengthOf(0)
301 it('Should list uninstalled plugins', async function () {
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)
306 const plugin = body.data[0]
307 expect(plugin.name).to.equal('hello-world')
308 expect(plugin.enabled).to.be.false
309 expect(plugin.uninstalled).to.be.true
312 it('Should uninstall the theme', async function () {
313 await command.uninstall({ npmName: 'peertube-theme-background-red' })
316 it('Should have updated the configuration', async function () {
317 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
318 expect(config.theme.default).to.equal('default')
320 const theme = config.theme.registered.find(r => r.name === 'background-red')
321 expect(theme).to.be.undefined
323 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
324 expect(plugin).to.be.undefined
328 it('Should have updated the user theme', async function () {
329 const user = await server.users.getMyInfo()
330 expect(user.theme).to.equal('instance-default')
333 it('Should not install a broken plugin', async function () {
336 async function check () {
337 const body = await command.list({ pluginType: PluginType.PLUGIN })
338 const plugins = body.data
339 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
342 await command.install({
343 path: PluginsCommand.getPluginTestPath('-broken'),
344 expectedStatus: HttpStatusCode.BAD_REQUEST_400
349 await killallServers([ server ])
355 it('Should rebuild native modules on Node ABI change', async function () {
358 const removeNativeModule = async () => {
359 await remove(join(baseNativeModule, 'build'))
360 await remove(join(baseNativeModule, 'prebuilds'))
363 await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
365 await makeGetRequest({
367 path: '/plugins/test-native/router',
368 expectedStatus: HttpStatusCode.NO_CONTENT_204
371 const query = `UPDATE "application" SET "nodeABIVersion" = 1`
372 await sqlCommand.updateQuery(query)
374 const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
376 await removeNativeModule()
382 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.true
383 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.true
385 await makeGetRequest({
387 path: '/plugins/test-native/router',
388 expectedStatus: HttpStatusCode.NO_CONTENT_204
391 await removeNativeModule()
396 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.false
397 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.false
399 await makeGetRequest({
401 path: '/plugins/test-native/router',
402 expectedStatus: HttpStatusCode.NOT_FOUND_404
406 after(async function () {
407 await sqlCommand.cleanup()
409 await cleanupTests([ server ])