1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
12 setAccessTokensToServers,
13 testHelloWorldRegisteredSettings,
15 } from '@shared/extra-utils'
16 import { PluginType } from '@shared/models'
18 const expect = chai.expect
20 describe('Test plugins', function () {
21 let server: PeerTubeServer = null
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
38 it('Should list and search available plugins and themes', async function () {
42 const body = await command.listAvailable({
45 pluginType: PluginType.THEME,
46 search: 'background-red'
49 expect(body.total).to.be.at.least(1)
50 expect(body.data).to.have.lengthOf(1)
54 const body1 = await command.listAvailable({
59 expect(body1.total).to.be.at.least(2)
61 const data1 = body1.data
62 expect(data1).to.have.lengthOf(2)
64 const body2 = await command.listAvailable({
69 expect(body2.total).to.be.at.least(2)
71 const data2 = body2.data
72 expect(data2).to.have.lengthOf(2)
74 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
78 const body = await command.listAvailable({
81 pluginType: PluginType.THEME,
82 search: 'background-red',
83 currentPeerTubeEngine: '1.0.0'
86 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
87 expect(p).to.be.undefined
91 it('Should install a plugin and a theme', async function () {
94 await command.install({ npmName: 'peertube-plugin-hello-world' })
95 await command.install({ npmName: 'peertube-theme-background-red' })
98 it('Should have the plugin loaded in the configuration', async function () {
99 const config = await server.config.getConfig()
101 const theme = config.theme.registered.find(r => r.name === 'background-red')
102 expect(theme).to.not.be.undefined
104 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
105 expect(plugin).to.not.be.undefined
108 it('Should update the default theme in the configuration', async function () {
109 await server.config.updateCustomSubConfig({
111 theme: { default: 'background-red' }
115 const config = await server.config.getConfig()
116 expect(config.theme.default).to.equal('background-red')
119 it('Should update my default theme', async function () {
120 await server.users.updateMe({ theme: 'background-red' })
122 const user = await server.users.getMyInfo()
123 expect(user.theme).to.equal('background-red')
126 it('Should list plugins and themes', async function () {
128 const body = await command.list({
131 pluginType: PluginType.THEME
133 expect(body.total).to.be.at.least(1)
135 const data = body.data
136 expect(data).to.have.lengthOf(1)
137 expect(data[0].name).to.equal('background-red')
141 const { data } = await command.list({
147 expect(data[0].name).to.equal('background-red')
148 expect(data[1].name).to.equal('hello-world')
152 const body = await command.list({
158 expect(body.data[0].name).to.equal('hello-world')
162 it('Should get registered settings', async function () {
163 await testHelloWorldRegisteredSettings(server)
166 it('Should get public settings', async function () {
167 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
168 const publicSettings = body.publicSettings
170 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
171 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
172 expect(publicSettings['user-name']).to.be.null
175 it('Should update the settings', async function () {
180 await command.updateSettings({
181 npmName: 'peertube-plugin-hello-world',
186 it('Should have watched settings changes', async function () {
189 await server.servers.waitUntilLog('Settings changed!')
192 it('Should get a plugin and a theme', async function () {
194 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
196 expect(plugin.type).to.equal(PluginType.PLUGIN)
197 expect(plugin.name).to.equal('hello-world')
198 expect(plugin.description).to.exist
199 expect(plugin.homepage).to.exist
200 expect(plugin.uninstalled).to.be.false
201 expect(plugin.enabled).to.be.true
202 expect(plugin.description).to.exist
203 expect(plugin.version).to.exist
204 expect(plugin.peertubeEngine).to.exist
205 expect(plugin.createdAt).to.exist
207 expect(plugin.settings).to.not.be.undefined
208 expect(plugin.settings['admin-name']).to.equal('Cid')
212 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
214 expect(plugin.type).to.equal(PluginType.THEME)
215 expect(plugin.name).to.equal('background-red')
216 expect(plugin.description).to.exist
217 expect(plugin.homepage).to.exist
218 expect(plugin.uninstalled).to.be.false
219 expect(plugin.enabled).to.be.true
220 expect(plugin.description).to.exist
221 expect(plugin.version).to.exist
222 expect(plugin.peertubeEngine).to.exist
223 expect(plugin.createdAt).to.exist
225 expect(plugin.settings).to.be.null
229 it('Should update the plugin and the theme', async function () {
232 // Wait the scheduler that get the latest plugins versions
235 // Fake update our plugin version
236 await server.sql.setPluginVersion('hello-world', '0.0.1')
238 // Fake update package.json
239 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
240 const oldVersion = packageJSON.version
242 packageJSON.version = '0.0.1'
243 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
245 // Restart the server to take into account this change
246 await killallServers([ server ])
250 const body = await command.list({ pluginType: PluginType.PLUGIN })
252 const plugin = body.data[0]
253 expect(plugin.version).to.equal('0.0.1')
254 expect(plugin.latestVersion).to.exist
255 expect(plugin.latestVersion).to.not.equal('0.0.1')
259 await command.update({ npmName: 'peertube-plugin-hello-world' })
261 const body = await command.list({ pluginType: PluginType.PLUGIN })
263 const plugin = body.data[0]
264 expect(plugin.version).to.equal(oldVersion)
266 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
267 expect(updatedPackageJSON.version).to.equal(oldVersion)
271 it('Should uninstall the plugin', async function () {
272 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
274 const body = await command.list({ pluginType: PluginType.PLUGIN })
275 expect(body.total).to.equal(0)
276 expect(body.data).to.have.lengthOf(0)
279 it('Should list uninstalled plugins', async function () {
280 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
281 expect(body.total).to.equal(1)
282 expect(body.data).to.have.lengthOf(1)
284 const plugin = body.data[0]
285 expect(plugin.name).to.equal('hello-world')
286 expect(plugin.enabled).to.be.false
287 expect(plugin.uninstalled).to.be.true
290 it('Should uninstall the theme', async function () {
291 await command.uninstall({ npmName: 'peertube-theme-background-red' })
294 it('Should have updated the configuration', async function () {
295 const config = await server.config.getConfig()
297 expect(config.theme.default).to.equal('default')
299 const theme = config.theme.registered.find(r => r.name === 'background-red')
300 expect(theme).to.be.undefined
302 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
303 expect(plugin).to.be.undefined
306 it('Should have updated the user theme', async function () {
307 const user = await server.users.getMyInfo()
308 expect(user.theme).to.equal('instance-default')
311 it('Should not install a broken plugin', async function () {
314 async function check () {
315 const body = await command.list({ pluginType: PluginType.PLUGIN })
316 const plugins = body.data
317 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
320 await command.install({
321 path: PluginsCommand.getPluginTestPath('-broken'),
322 expectedStatus: HttpStatusCode.BAD_REQUEST_400
327 await killallServers([ server ])
333 after(async function () {
334 await cleanupTests([ server ])