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'
16 setAccessTokensToServers,
18 testHelloWorldRegisteredSettings,
19 updateCustomSubConfig,
23 } from '@shared/extra-utils'
24 import { PluginType, ServerConfig, User } from '@shared/models'
26 const expect = chai.expect
28 describe('Test plugins', function () {
29 let server: ServerInfo = null
30 let command: PluginsCommand
32 before(async function () {
35 const configOverride = {
37 index: { check_latest_versions_interval: '5 seconds' }
40 server = await flushAndRunServer(1, configOverride)
41 await setAccessTokensToServers([ server ])
44 it('Should list and search available plugins and themes', async function () {
48 const body = await command.listAvailable({
51 pluginType: PluginType.THEME,
52 search: 'background-red'
55 expect(body.total).to.be.at.least(1)
56 expect(body.data).to.have.lengthOf(1)
60 const body1 = await command.listAvailable({
65 expect(body1.total).to.be.at.least(2)
67 const data1 = body1.data
68 expect(data1).to.have.lengthOf(2)
70 const body2 = await command.listAvailable({
75 expect(body2.total).to.be.at.least(2)
77 const data2 = body2.data
78 expect(data2).to.have.lengthOf(2)
80 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
84 const body = await command.listAvailable({
87 pluginType: PluginType.THEME,
88 search: 'background-red',
89 currentPeerTubeEngine: '1.0.0'
92 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
93 expect(p).to.be.undefined
97 it('Should install a plugin and a theme', async function () {
100 await command.install({ npmName: 'peertube-plugin-hello-world' })
101 await command.install({ npmName: 'peertube-theme-background-red' })
104 it('Should have the plugin loaded in the configuration', async function () {
105 const res = await getConfig(server.url)
106 const config: ServerConfig = res.body
108 const theme = config.theme.registered.find(r => r.name === 'background-red')
109 expect(theme).to.not.be.undefined
111 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
112 expect(plugin).to.not.be.undefined
115 it('Should update the default theme in the configuration', async function () {
116 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
118 const res = await getConfig(server.url)
119 const config: ServerConfig = res.body
121 expect(config.theme.default).to.equal('background-red')
124 it('Should update my default theme', async function () {
127 accessToken: server.accessToken,
128 theme: 'background-red'
131 const res = await getMyUserInformation(server.url, server.accessToken)
132 expect((res.body as User).theme).to.equal('background-red')
135 it('Should list plugins and themes', async function () {
137 const body = await command.list({
140 pluginType: PluginType.THEME
142 expect(body.total).to.be.at.least(1)
144 const data = body.data
145 expect(data).to.have.lengthOf(1)
146 expect(data[0].name).to.equal('background-red')
150 const body = await command.list({
157 expect(data[0].name).to.equal('background-red')
158 expect(data[1].name).to.equal('hello-world')
162 const body = await command.list({
168 expect(body.data[0].name).to.equal('hello-world')
172 it('Should get registered settings', async function () {
173 await testHelloWorldRegisteredSettings(server)
176 it('Should get public settings', async function () {
177 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
178 const publicSettings = body.publicSettings
180 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
181 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
182 expect(publicSettings['user-name']).to.be.null
185 it('Should update the settings', async function () {
190 await command.updateSettings({
191 npmName: 'peertube-plugin-hello-world',
196 it('Should have watched settings changes', async function () {
199 await waitUntilLog(server, 'Settings changed!')
202 it('Should get a plugin and a theme', async function () {
204 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
206 expect(plugin.type).to.equal(PluginType.PLUGIN)
207 expect(plugin.name).to.equal('hello-world')
208 expect(plugin.description).to.exist
209 expect(plugin.homepage).to.exist
210 expect(plugin.uninstalled).to.be.false
211 expect(plugin.enabled).to.be.true
212 expect(plugin.description).to.exist
213 expect(plugin.version).to.exist
214 expect(plugin.peertubeEngine).to.exist
215 expect(plugin.createdAt).to.exist
217 expect(plugin.settings).to.not.be.undefined
218 expect(plugin.settings['admin-name']).to.equal('Cid')
222 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
224 expect(plugin.type).to.equal(PluginType.THEME)
225 expect(plugin.name).to.equal('background-red')
226 expect(plugin.description).to.exist
227 expect(plugin.homepage).to.exist
228 expect(plugin.uninstalled).to.be.false
229 expect(plugin.enabled).to.be.true
230 expect(plugin.description).to.exist
231 expect(plugin.version).to.exist
232 expect(plugin.peertubeEngine).to.exist
233 expect(plugin.createdAt).to.exist
235 expect(plugin.settings).to.be.null
239 it('Should update the plugin and the theme', async function () {
242 // Wait the scheduler that get the latest plugins versions
245 // Fake update our plugin version
246 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
248 // Fake update package.json
249 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
250 const oldVersion = packageJSON.version
252 packageJSON.version = '0.0.1'
253 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
255 // Restart the server to take into account this change
256 killallServers([ server ])
257 await reRunServer(server)
260 const body = await command.list({ pluginType: PluginType.PLUGIN })
262 const plugin = body.data[0]
263 expect(plugin.version).to.equal('0.0.1')
264 expect(plugin.latestVersion).to.exist
265 expect(plugin.latestVersion).to.not.equal('0.0.1')
269 await command.update({ npmName: 'peertube-plugin-hello-world' })
271 const body = await command.list({ pluginType: PluginType.PLUGIN })
273 const plugin = body.data[0]
274 expect(plugin.version).to.equal(oldVersion)
276 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
277 expect(updatedPackageJSON.version).to.equal(oldVersion)
281 it('Should uninstall the plugin', async function () {
282 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
284 const body = await command.list({ pluginType: PluginType.PLUGIN })
285 expect(body.total).to.equal(0)
286 expect(body.data).to.have.lengthOf(0)
289 it('Should list uninstalled plugins', async function () {
290 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
291 expect(body.total).to.equal(1)
292 expect(body.data).to.have.lengthOf(1)
294 const plugin = body.data[0]
295 expect(plugin.name).to.equal('hello-world')
296 expect(plugin.enabled).to.be.false
297 expect(plugin.uninstalled).to.be.true
300 it('Should uninstall the theme', async function () {
301 await command.uninstall({ npmName: 'peertube-theme-background-red' })
304 it('Should have updated the configuration', async function () {
305 // get /config (default theme + registered themes + registered plugins)
306 const res = await getConfig(server.url)
307 const config: ServerConfig = res.body
309 expect(config.theme.default).to.equal('default')
311 const theme = config.theme.registered.find(r => r.name === 'background-red')
312 expect(theme).to.be.undefined
314 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
315 expect(plugin).to.be.undefined
318 it('Should have updated the user theme', async function () {
319 const res = await getMyUserInformation(server.url, server.accessToken)
320 expect((res.body as User).theme).to.equal('instance-default')
323 it('Should not install a broken plugin', async function () {
326 async function check () {
327 const body = await command.list({ pluginType: PluginType.PLUGIN })
328 const plugins = body.data
329 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
332 await command.install({
333 path: PluginsCommand.getPluginTestPath('-broken'),
334 expectedStatus: HttpStatusCode.BAD_REQUEST_400
339 killallServers([ server ])
340 await reRunServer(server)
345 after(async function () {
346 await closeAllSequelize([ server ])
347 await cleanupTests([ server ])