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'
14 setAccessTokensToServers,
15 testHelloWorldRegisteredSettings,
19 } from '@shared/extra-utils'
20 import { PluginType, User } from '@shared/models'
22 const expect = chai.expect
24 describe('Test plugins', function () {
25 let server: ServerInfo = null
26 let command: PluginsCommand
28 before(async function () {
31 const configOverride = {
33 index: { check_latest_versions_interval: '5 seconds' }
36 server = await flushAndRunServer(1, configOverride)
37 await setAccessTokensToServers([ server ])
39 command = server.pluginsCommand
42 it('Should list and search available plugins and themes', async function () {
46 const body = await command.listAvailable({
49 pluginType: PluginType.THEME,
50 search: 'background-red'
53 expect(body.total).to.be.at.least(1)
54 expect(body.data).to.have.lengthOf(1)
58 const body1 = await command.listAvailable({
63 expect(body1.total).to.be.at.least(2)
65 const data1 = body1.data
66 expect(data1).to.have.lengthOf(2)
68 const body2 = await command.listAvailable({
73 expect(body2.total).to.be.at.least(2)
75 const data2 = body2.data
76 expect(data2).to.have.lengthOf(2)
78 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
82 const body = await command.listAvailable({
85 pluginType: PluginType.THEME,
86 search: 'background-red',
87 currentPeerTubeEngine: '1.0.0'
90 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
91 expect(p).to.be.undefined
95 it('Should install a plugin and a theme', async function () {
98 await command.install({ npmName: 'peertube-plugin-hello-world' })
99 await command.install({ npmName: 'peertube-theme-background-red' })
102 it('Should have the plugin loaded in the configuration', async function () {
103 const config = await server.configCommand.getConfig()
105 const theme = config.theme.registered.find(r => r.name === 'background-red')
106 expect(theme).to.not.be.undefined
108 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
109 expect(plugin).to.not.be.undefined
112 it('Should update the default theme in the configuration', async function () {
113 await server.configCommand.updateCustomSubConfig({
115 theme: { default: 'background-red' }
119 const config = await server.configCommand.getConfig()
120 expect(config.theme.default).to.equal('background-red')
123 it('Should update my default theme', async function () {
126 accessToken: server.accessToken,
127 theme: 'background-red'
130 const res = await getMyUserInformation(server.url, server.accessToken)
131 expect((res.body as User).theme).to.equal('background-red')
134 it('Should list plugins and themes', async function () {
136 const body = await command.list({
139 pluginType: PluginType.THEME
141 expect(body.total).to.be.at.least(1)
143 const data = body.data
144 expect(data).to.have.lengthOf(1)
145 expect(data[0].name).to.equal('background-red')
149 const { data } = await command.list({
155 expect(data[0].name).to.equal('background-red')
156 expect(data[1].name).to.equal('hello-world')
160 const body = await command.list({
166 expect(body.data[0].name).to.equal('hello-world')
170 it('Should get registered settings', async function () {
171 await testHelloWorldRegisteredSettings(server)
174 it('Should get public settings', async function () {
175 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
176 const publicSettings = body.publicSettings
178 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
179 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
180 expect(publicSettings['user-name']).to.be.null
183 it('Should update the settings', async function () {
188 await command.updateSettings({
189 npmName: 'peertube-plugin-hello-world',
194 it('Should have watched settings changes', async function () {
197 await waitUntilLog(server, 'Settings changed!')
200 it('Should get a plugin and a theme', async function () {
202 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
204 expect(plugin.type).to.equal(PluginType.PLUGIN)
205 expect(plugin.name).to.equal('hello-world')
206 expect(plugin.description).to.exist
207 expect(plugin.homepage).to.exist
208 expect(plugin.uninstalled).to.be.false
209 expect(plugin.enabled).to.be.true
210 expect(plugin.description).to.exist
211 expect(plugin.version).to.exist
212 expect(plugin.peertubeEngine).to.exist
213 expect(plugin.createdAt).to.exist
215 expect(plugin.settings).to.not.be.undefined
216 expect(plugin.settings['admin-name']).to.equal('Cid')
220 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
222 expect(plugin.type).to.equal(PluginType.THEME)
223 expect(plugin.name).to.equal('background-red')
224 expect(plugin.description).to.exist
225 expect(plugin.homepage).to.exist
226 expect(plugin.uninstalled).to.be.false
227 expect(plugin.enabled).to.be.true
228 expect(plugin.description).to.exist
229 expect(plugin.version).to.exist
230 expect(plugin.peertubeEngine).to.exist
231 expect(plugin.createdAt).to.exist
233 expect(plugin.settings).to.be.null
237 it('Should update the plugin and the theme', async function () {
240 // Wait the scheduler that get the latest plugins versions
243 // Fake update our plugin version
244 await server.sqlCommand.setPluginVersion('hello-world', '0.0.1')
246 // Fake update package.json
247 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
248 const oldVersion = packageJSON.version
250 packageJSON.version = '0.0.1'
251 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
253 // Restart the server to take into account this change
254 await killallServers([ server ])
255 await reRunServer(server)
258 const body = await command.list({ pluginType: PluginType.PLUGIN })
260 const plugin = body.data[0]
261 expect(plugin.version).to.equal('0.0.1')
262 expect(plugin.latestVersion).to.exist
263 expect(plugin.latestVersion).to.not.equal('0.0.1')
267 await command.update({ npmName: 'peertube-plugin-hello-world' })
269 const body = await command.list({ pluginType: PluginType.PLUGIN })
271 const plugin = body.data[0]
272 expect(plugin.version).to.equal(oldVersion)
274 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
275 expect(updatedPackageJSON.version).to.equal(oldVersion)
279 it('Should uninstall the plugin', async function () {
280 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
282 const body = await command.list({ pluginType: PluginType.PLUGIN })
283 expect(body.total).to.equal(0)
284 expect(body.data).to.have.lengthOf(0)
287 it('Should list uninstalled plugins', async function () {
288 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
289 expect(body.total).to.equal(1)
290 expect(body.data).to.have.lengthOf(1)
292 const plugin = body.data[0]
293 expect(plugin.name).to.equal('hello-world')
294 expect(plugin.enabled).to.be.false
295 expect(plugin.uninstalled).to.be.true
298 it('Should uninstall the theme', async function () {
299 await command.uninstall({ npmName: 'peertube-theme-background-red' })
302 it('Should have updated the configuration', async function () {
303 const config = await server.configCommand.getConfig()
305 expect(config.theme.default).to.equal('default')
307 const theme = config.theme.registered.find(r => r.name === 'background-red')
308 expect(theme).to.be.undefined
310 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
311 expect(plugin).to.be.undefined
314 it('Should have updated the user theme', async function () {
315 const res = await getMyUserInformation(server.url, server.accessToken)
316 expect((res.body as User).theme).to.equal('instance-default')
319 it('Should not install a broken plugin', async function () {
322 async function check () {
323 const body = await command.list({ pluginType: PluginType.PLUGIN })
324 const plugins = body.data
325 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
328 await command.install({
329 path: PluginsCommand.getPluginTestPath('-broken'),
330 expectedStatus: HttpStatusCode.BAD_REQUEST_400
335 await killallServers([ server ])
336 await reRunServer(server)
341 after(async function () {
342 await cleanupTests([ server ])