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 { 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 = null
21 let command: PluginsCommand
23 before(async function () {
26 const configOverride = {
28 index: { check_latest_versions_interval: '5 seconds' }
31 server = await createSingleServer(1, configOverride)
32 await setAccessTokensToServers([ server ])
34 command = server.plugins
37 it('Should list and search available plugins and themes', async function () {
41 const body = await command.listAvailable({
44 pluginType: PluginType.THEME,
45 search: 'background-red'
48 expect(body.total).to.be.at.least(1)
49 expect(body.data).to.have.lengthOf(1)
53 const body1 = await command.listAvailable({
58 expect(body1.total).to.be.at.least(2)
60 const data1 = body1.data
61 expect(data1).to.have.lengthOf(2)
63 const body2 = await command.listAvailable({
68 expect(body2.total).to.be.at.least(2)
70 const data2 = body2.data
71 expect(data2).to.have.lengthOf(2)
73 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
77 const body = await command.listAvailable({
80 pluginType: PluginType.THEME,
81 search: 'background-red',
82 currentPeerTubeEngine: '1.0.0'
85 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
86 expect(p).to.be.undefined
90 it('Should install a plugin and a theme', async function () {
93 await command.install({ npmName: 'peertube-plugin-hello-world' })
94 await command.install({ npmName: 'peertube-theme-background-red' })
97 it('Should have the plugin loaded in the configuration', async function () {
98 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
99 const theme = config.theme.registered.find(r => r.name === 'background-red')
100 expect(theme).to.not.be.undefined
101 expect(theme.npmName).to.equal('peertube-theme-background-red')
103 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
104 expect(plugin).to.not.be.undefined
105 expect(plugin.npmName).to.equal('peertube-plugin-hello-world')
109 it('Should update the default theme in the configuration', async function () {
110 await server.config.updateCustomSubConfig({
112 theme: { default: 'background-red' }
116 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
117 expect(config.theme.default).to.equal('background-red')
121 it('Should update my default theme', async function () {
122 await server.users.updateMe({ theme: 'background-red' })
124 const user = await server.users.getMyInfo()
125 expect(user.theme).to.equal('background-red')
128 it('Should list plugins and themes', async function () {
130 const body = await command.list({
133 pluginType: PluginType.THEME
135 expect(body.total).to.be.at.least(1)
137 const data = body.data
138 expect(data).to.have.lengthOf(1)
139 expect(data[0].name).to.equal('background-red')
143 const { data } = await command.list({
149 expect(data[0].name).to.equal('background-red')
150 expect(data[1].name).to.equal('hello-world')
154 const body = await command.list({
160 expect(body.data[0].name).to.equal('hello-world')
164 it('Should get registered settings', async function () {
165 await testHelloWorldRegisteredSettings(server)
168 it('Should get public settings', async function () {
169 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
170 const publicSettings = body.publicSettings
172 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
173 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
174 expect(publicSettings['user-name']).to.be.null
177 it('Should update the settings', async function () {
182 await command.updateSettings({
183 npmName: 'peertube-plugin-hello-world',
188 it('Should have watched settings changes', async function () {
191 await server.servers.waitUntilLog('Settings changed!')
194 it('Should get a plugin and a theme', async function () {
196 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
198 expect(plugin.type).to.equal(PluginType.PLUGIN)
199 expect(plugin.name).to.equal('hello-world')
200 expect(plugin.description).to.exist
201 expect(plugin.homepage).to.exist
202 expect(plugin.uninstalled).to.be.false
203 expect(plugin.enabled).to.be.true
204 expect(plugin.description).to.exist
205 expect(plugin.version).to.exist
206 expect(plugin.peertubeEngine).to.exist
207 expect(plugin.createdAt).to.exist
209 expect(plugin.settings).to.not.be.undefined
210 expect(plugin.settings['admin-name']).to.equal('Cid')
214 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
216 expect(plugin.type).to.equal(PluginType.THEME)
217 expect(plugin.name).to.equal('background-red')
218 expect(plugin.description).to.exist
219 expect(plugin.homepage).to.exist
220 expect(plugin.uninstalled).to.be.false
221 expect(plugin.enabled).to.be.true
222 expect(plugin.description).to.exist
223 expect(plugin.version).to.exist
224 expect(plugin.peertubeEngine).to.exist
225 expect(plugin.createdAt).to.exist
227 expect(plugin.settings).to.be.null
231 it('Should update the plugin and the theme', async function () {
234 // Wait the scheduler that get the latest plugins versions
237 async function testUpdate (type: 'plugin' | 'theme', name: string) {
238 // Fake update our plugin version
239 await server.sql.setPluginVersion(name, '0.0.1')
241 // Fake update package.json
242 const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
243 const oldVersion = packageJSON.version
245 packageJSON.version = '0.0.1'
246 await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
248 // Restart the server to take into account this change
249 await killallServers([ server ])
252 const checkConfig = async (version: string) => {
253 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
254 expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
258 const getPluginFromAPI = async () => {
259 const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
261 return body.data.find(p => p.name === name)
265 const plugin = await getPluginFromAPI()
266 expect(plugin.version).to.equal('0.0.1')
267 expect(plugin.latestVersion).to.exist
268 expect(plugin.latestVersion).to.not.equal('0.0.1')
270 await checkConfig('0.0.1')
274 await command.update({ npmName: `peertube-${type}-${name}` })
276 const plugin = await getPluginFromAPI()
277 expect(plugin.version).to.equal(oldVersion)
279 const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
280 expect(updatedPackageJSON.version).to.equal(oldVersion)
282 await checkConfig(oldVersion)
286 await testUpdate('theme', 'background-red')
287 await testUpdate('plugin', 'hello-world')
290 it('Should uninstall the plugin', async function () {
291 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
293 const body = await command.list({ pluginType: PluginType.PLUGIN })
294 expect(body.total).to.equal(0)
295 expect(body.data).to.have.lengthOf(0)
298 it('Should list uninstalled plugins', async function () {
299 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
300 expect(body.total).to.equal(1)
301 expect(body.data).to.have.lengthOf(1)
303 const plugin = body.data[0]
304 expect(plugin.name).to.equal('hello-world')
305 expect(plugin.enabled).to.be.false
306 expect(plugin.uninstalled).to.be.true
309 it('Should uninstall the theme', async function () {
310 await command.uninstall({ npmName: 'peertube-theme-background-red' })
313 it('Should have updated the configuration', async function () {
314 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
315 expect(config.theme.default).to.equal('default')
317 const theme = config.theme.registered.find(r => r.name === 'background-red')
318 expect(theme).to.be.undefined
320 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
321 expect(plugin).to.be.undefined
325 it('Should have updated the user theme', async function () {
326 const user = await server.users.getMyInfo()
327 expect(user.theme).to.equal('instance-default')
330 it('Should not install a broken plugin', async function () {
333 async function check () {
334 const body = await command.list({ pluginType: PluginType.PLUGIN })
335 const plugins = body.data
336 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
339 await command.install({
340 path: PluginsCommand.getPluginTestPath('-broken'),
341 expectedStatus: HttpStatusCode.BAD_REQUEST_400
346 await killallServers([ server ])
352 it('Should rebuild native modules on Node ABI change', async function () {
355 const removeNativeModule = async () => {
356 await remove(join(baseNativeModule, 'build'))
357 await remove(join(baseNativeModule, 'prebuilds'))
360 await command.install({ path: PluginsCommand.getPluginTestPath('-native') })
362 await makeGetRequest({
364 path: '/plugins/test-native/router',
365 expectedStatus: HttpStatusCode.NO_CONTENT_204
368 const query = `UPDATE "application" SET "nodeABIVersion" = 1`
369 await server.sql.updateQuery(query)
371 const baseNativeModule = server.servers.buildDirectory(join('plugins', 'node_modules', 'a-native-example'))
373 await removeNativeModule()
379 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.true
380 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.true
382 await makeGetRequest({
384 path: '/plugins/test-native/router',
385 expectedStatus: HttpStatusCode.NO_CONTENT_204
388 await removeNativeModule()
393 expect(await pathExists(join(baseNativeModule, 'build'))).to.be.false
394 expect(await pathExists(join(baseNativeModule, 'prebuilds'))).to.be.false
396 await makeGetRequest({
398 path: '/plugins/test-native/router',
399 expectedStatus: HttpStatusCode.NOT_FOUND_404
403 after(async function () {
404 await cleanupTests([ server ])