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
102 expect(theme.npmName).to.equal('peertube-theme-background-red')
104 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
105 expect(plugin).to.not.be.undefined
106 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 const config = await server.config.getConfig()
117 expect(config.theme.default).to.equal('background-red')
120 it('Should update my default theme', async function () {
121 await server.users.updateMe({ theme: 'background-red' })
123 const user = await server.users.getMyInfo()
124 expect(user.theme).to.equal('background-red')
127 it('Should list plugins and themes', async function () {
129 const body = await command.list({
132 pluginType: PluginType.THEME
134 expect(body.total).to.be.at.least(1)
136 const data = body.data
137 expect(data).to.have.lengthOf(1)
138 expect(data[0].name).to.equal('background-red')
142 const { data } = await command.list({
148 expect(data[0].name).to.equal('background-red')
149 expect(data[1].name).to.equal('hello-world')
153 const body = await command.list({
159 expect(body.data[0].name).to.equal('hello-world')
163 it('Should get registered settings', async function () {
164 await testHelloWorldRegisteredSettings(server)
167 it('Should get public settings', async function () {
168 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
169 const publicSettings = body.publicSettings
171 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
172 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
173 expect(publicSettings['user-name']).to.be.null
176 it('Should update the settings', async function () {
181 await command.updateSettings({
182 npmName: 'peertube-plugin-hello-world',
187 it('Should have watched settings changes', async function () {
190 await server.servers.waitUntilLog('Settings changed!')
193 it('Should get a plugin and a theme', async function () {
195 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
197 expect(plugin.type).to.equal(PluginType.PLUGIN)
198 expect(plugin.name).to.equal('hello-world')
199 expect(plugin.description).to.exist
200 expect(plugin.homepage).to.exist
201 expect(plugin.uninstalled).to.be.false
202 expect(plugin.enabled).to.be.true
203 expect(plugin.description).to.exist
204 expect(plugin.version).to.exist
205 expect(plugin.peertubeEngine).to.exist
206 expect(plugin.createdAt).to.exist
208 expect(plugin.settings).to.not.be.undefined
209 expect(plugin.settings['admin-name']).to.equal('Cid')
213 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
215 expect(plugin.type).to.equal(PluginType.THEME)
216 expect(plugin.name).to.equal('background-red')
217 expect(plugin.description).to.exist
218 expect(plugin.homepage).to.exist
219 expect(plugin.uninstalled).to.be.false
220 expect(plugin.enabled).to.be.true
221 expect(plugin.description).to.exist
222 expect(plugin.version).to.exist
223 expect(plugin.peertubeEngine).to.exist
224 expect(plugin.createdAt).to.exist
226 expect(plugin.settings).to.be.null
230 it('Should update the plugin and the theme', async function () {
233 // Wait the scheduler that get the latest plugins versions
236 // Fake update our plugin version
237 await server.sql.setPluginVersion('hello-world', '0.0.1')
239 // Fake update package.json
240 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
241 const oldVersion = packageJSON.version
243 packageJSON.version = '0.0.1'
244 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
246 // Restart the server to take into account this change
247 await killallServers([ server ])
251 const body = await command.list({ pluginType: PluginType.PLUGIN })
253 const plugin = body.data[0]
254 expect(plugin.version).to.equal('0.0.1')
255 expect(plugin.latestVersion).to.exist
256 expect(plugin.latestVersion).to.not.equal('0.0.1')
260 await command.update({ npmName: 'peertube-plugin-hello-world' })
262 const body = await command.list({ pluginType: PluginType.PLUGIN })
264 const plugin = body.data[0]
265 expect(plugin.version).to.equal(oldVersion)
267 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
268 expect(updatedPackageJSON.version).to.equal(oldVersion)
272 it('Should uninstall the plugin', async function () {
273 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
275 const body = await command.list({ pluginType: PluginType.PLUGIN })
276 expect(body.total).to.equal(0)
277 expect(body.data).to.have.lengthOf(0)
280 it('Should list uninstalled plugins', async function () {
281 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
282 expect(body.total).to.equal(1)
283 expect(body.data).to.have.lengthOf(1)
285 const plugin = body.data[0]
286 expect(plugin.name).to.equal('hello-world')
287 expect(plugin.enabled).to.be.false
288 expect(plugin.uninstalled).to.be.true
291 it('Should uninstall the theme', async function () {
292 await command.uninstall({ npmName: 'peertube-theme-background-red' })
295 it('Should have updated the configuration', async function () {
296 const config = await server.config.getConfig()
298 expect(config.theme.default).to.equal('default')
300 const theme = config.theme.registered.find(r => r.name === 'background-red')
301 expect(theme).to.be.undefined
303 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
304 expect(plugin).to.be.undefined
307 it('Should have updated the user theme', async function () {
308 const user = await server.users.getMyInfo()
309 expect(user.theme).to.equal('instance-default')
312 it('Should not install a broken plugin', async function () {
315 async function check () {
316 const body = await command.list({ pluginType: PluginType.PLUGIN })
317 const plugins = body.data
318 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
321 await command.install({
322 path: PluginsCommand.getPluginTestPath('-broken'),
323 expectedStatus: HttpStatusCode.BAD_REQUEST_400
328 await killallServers([ server ])
334 after(async function () {
335 await cleanupTests([ server ])