1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { testHelloWorldRegisteredSettings } from '@server/tests/shared'
6 import { wait } from '@shared/core-utils'
7 import { HttpStatusCode, PluginType } from '@shared/models'
14 setAccessTokensToServers
15 } from '@shared/server-commands'
17 const expect = chai.expect
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 const config = await server.config.getConfig()
100 const theme = config.theme.registered.find(r => r.name === 'background-red')
101 expect(theme).to.not.be.undefined
103 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
104 expect(plugin).to.not.be.undefined
107 it('Should update the default theme in the configuration', async function () {
108 await server.config.updateCustomSubConfig({
110 theme: { default: 'background-red' }
114 const config = await server.config.getConfig()
115 expect(config.theme.default).to.equal('background-red')
118 it('Should update my default theme', async function () {
119 await server.users.updateMe({ theme: 'background-red' })
121 const user = await server.users.getMyInfo()
122 expect(user.theme).to.equal('background-red')
125 it('Should list plugins and themes', async function () {
127 const body = await command.list({
130 pluginType: PluginType.THEME
132 expect(body.total).to.be.at.least(1)
134 const data = body.data
135 expect(data).to.have.lengthOf(1)
136 expect(data[0].name).to.equal('background-red')
140 const { data } = await command.list({
146 expect(data[0].name).to.equal('background-red')
147 expect(data[1].name).to.equal('hello-world')
151 const body = await command.list({
157 expect(body.data[0].name).to.equal('hello-world')
161 it('Should get registered settings', async function () {
162 await testHelloWorldRegisteredSettings(server)
165 it('Should get public settings', async function () {
166 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
167 const publicSettings = body.publicSettings
169 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
170 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
171 expect(publicSettings['user-name']).to.be.null
174 it('Should update the settings', async function () {
179 await command.updateSettings({
180 npmName: 'peertube-plugin-hello-world',
185 it('Should have watched settings changes', async function () {
188 await server.servers.waitUntilLog('Settings changed!')
191 it('Should get a plugin and a theme', async function () {
193 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
195 expect(plugin.type).to.equal(PluginType.PLUGIN)
196 expect(plugin.name).to.equal('hello-world')
197 expect(plugin.description).to.exist
198 expect(plugin.homepage).to.exist
199 expect(plugin.uninstalled).to.be.false
200 expect(plugin.enabled).to.be.true
201 expect(plugin.description).to.exist
202 expect(plugin.version).to.exist
203 expect(plugin.peertubeEngine).to.exist
204 expect(plugin.createdAt).to.exist
206 expect(plugin.settings).to.not.be.undefined
207 expect(plugin.settings['admin-name']).to.equal('Cid')
211 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
213 expect(plugin.type).to.equal(PluginType.THEME)
214 expect(plugin.name).to.equal('background-red')
215 expect(plugin.description).to.exist
216 expect(plugin.homepage).to.exist
217 expect(plugin.uninstalled).to.be.false
218 expect(plugin.enabled).to.be.true
219 expect(plugin.description).to.exist
220 expect(plugin.version).to.exist
221 expect(plugin.peertubeEngine).to.exist
222 expect(plugin.createdAt).to.exist
224 expect(plugin.settings).to.be.null
228 it('Should update the plugin and the theme', async function () {
231 // Wait the scheduler that get the latest plugins versions
234 // Fake update our plugin version
235 await server.sql.setPluginVersion('hello-world', '0.0.1')
237 // Fake update package.json
238 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
239 const oldVersion = packageJSON.version
241 packageJSON.version = '0.0.1'
242 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
244 // Restart the server to take into account this change
245 await killallServers([ server ])
249 const body = await command.list({ pluginType: PluginType.PLUGIN })
251 const plugin = body.data[0]
252 expect(plugin.version).to.equal('0.0.1')
253 expect(plugin.latestVersion).to.exist
254 expect(plugin.latestVersion).to.not.equal('0.0.1')
258 await command.update({ npmName: 'peertube-plugin-hello-world' })
260 const body = await command.list({ pluginType: PluginType.PLUGIN })
262 const plugin = body.data[0]
263 expect(plugin.version).to.equal(oldVersion)
265 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
266 expect(updatedPackageJSON.version).to.equal(oldVersion)
270 it('Should uninstall the plugin', async function () {
271 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
273 const body = await command.list({ pluginType: PluginType.PLUGIN })
274 expect(body.total).to.equal(0)
275 expect(body.data).to.have.lengthOf(0)
278 it('Should list uninstalled plugins', async function () {
279 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
280 expect(body.total).to.equal(1)
281 expect(body.data).to.have.lengthOf(1)
283 const plugin = body.data[0]
284 expect(plugin.name).to.equal('hello-world')
285 expect(plugin.enabled).to.be.false
286 expect(plugin.uninstalled).to.be.true
289 it('Should uninstall the theme', async function () {
290 await command.uninstall({ npmName: 'peertube-theme-background-red' })
293 it('Should have updated the configuration', async function () {
294 const config = await server.config.getConfig()
296 expect(config.theme.default).to.equal('default')
298 const theme = config.theme.registered.find(r => r.name === 'background-red')
299 expect(theme).to.be.undefined
301 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
302 expect(plugin).to.be.undefined
305 it('Should have updated the user theme', async function () {
306 const user = await server.users.getMyInfo()
307 expect(user.theme).to.equal('instance-default')
310 it('Should not install a broken plugin', async function () {
313 async function check () {
314 const body = await command.list({ pluginType: PluginType.PLUGIN })
315 const plugins = body.data
316 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
319 await command.install({
320 path: PluginsCommand.getPluginTestPath('-broken'),
321 expectedStatus: HttpStatusCode.BAD_REQUEST_400
326 await killallServers([ server ])
332 after(async function () {
333 await cleanupTests([ server ])