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'
15 setAccessTokensToServers,
17 testHelloWorldRegisteredSettings,
21 } from '@shared/extra-utils'
22 import { PluginType, User } from '@shared/models'
24 const expect = chai.expect
26 describe('Test plugins', function () {
27 let server: ServerInfo = null
28 let command: PluginsCommand
30 before(async function () {
33 const configOverride = {
35 index: { check_latest_versions_interval: '5 seconds' }
38 server = await flushAndRunServer(1, configOverride)
39 await setAccessTokensToServers([ server ])
41 command = server.pluginsCommand
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 config = await server.configCommand.getConfig()
107 const theme = config.theme.registered.find(r => r.name === 'background-red')
108 expect(theme).to.not.be.undefined
110 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
111 expect(plugin).to.not.be.undefined
114 it('Should update the default theme in the configuration', async function () {
115 await server.configCommand.updateCustomSubConfig({
117 theme: { default: 'background-red' }
121 const config = await server.configCommand.getConfig()
122 expect(config.theme.default).to.equal('background-red')
125 it('Should update my default theme', async function () {
128 accessToken: server.accessToken,
129 theme: 'background-red'
132 const res = await getMyUserInformation(server.url, server.accessToken)
133 expect((res.body as User).theme).to.equal('background-red')
136 it('Should list plugins and themes', async function () {
138 const body = await command.list({
141 pluginType: PluginType.THEME
143 expect(body.total).to.be.at.least(1)
145 const data = body.data
146 expect(data).to.have.lengthOf(1)
147 expect(data[0].name).to.equal('background-red')
151 const body = await command.list({
158 expect(data[0].name).to.equal('background-red')
159 expect(data[1].name).to.equal('hello-world')
163 const body = await command.list({
169 expect(body.data[0].name).to.equal('hello-world')
173 it('Should get registered settings', async function () {
174 await testHelloWorldRegisteredSettings(server)
177 it('Should get public settings', async function () {
178 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
179 const publicSettings = body.publicSettings
181 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
182 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
183 expect(publicSettings['user-name']).to.be.null
186 it('Should update the settings', async function () {
191 await command.updateSettings({
192 npmName: 'peertube-plugin-hello-world',
197 it('Should have watched settings changes', async function () {
200 await waitUntilLog(server, 'Settings changed!')
203 it('Should get a plugin and a theme', async function () {
205 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
207 expect(plugin.type).to.equal(PluginType.PLUGIN)
208 expect(plugin.name).to.equal('hello-world')
209 expect(plugin.description).to.exist
210 expect(plugin.homepage).to.exist
211 expect(plugin.uninstalled).to.be.false
212 expect(plugin.enabled).to.be.true
213 expect(plugin.description).to.exist
214 expect(plugin.version).to.exist
215 expect(plugin.peertubeEngine).to.exist
216 expect(plugin.createdAt).to.exist
218 expect(plugin.settings).to.not.be.undefined
219 expect(plugin.settings['admin-name']).to.equal('Cid')
223 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
225 expect(plugin.type).to.equal(PluginType.THEME)
226 expect(plugin.name).to.equal('background-red')
227 expect(plugin.description).to.exist
228 expect(plugin.homepage).to.exist
229 expect(plugin.uninstalled).to.be.false
230 expect(plugin.enabled).to.be.true
231 expect(plugin.description).to.exist
232 expect(plugin.version).to.exist
233 expect(plugin.peertubeEngine).to.exist
234 expect(plugin.createdAt).to.exist
236 expect(plugin.settings).to.be.null
240 it('Should update the plugin and the theme', async function () {
243 // Wait the scheduler that get the latest plugins versions
246 // Fake update our plugin version
247 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
249 // Fake update package.json
250 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
251 const oldVersion = packageJSON.version
253 packageJSON.version = '0.0.1'
254 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
256 // Restart the server to take into account this change
257 killallServers([ server ])
258 await reRunServer(server)
261 const body = await command.list({ pluginType: PluginType.PLUGIN })
263 const plugin = body.data[0]
264 expect(plugin.version).to.equal('0.0.1')
265 expect(plugin.latestVersion).to.exist
266 expect(plugin.latestVersion).to.not.equal('0.0.1')
270 await command.update({ npmName: 'peertube-plugin-hello-world' })
272 const body = await command.list({ pluginType: PluginType.PLUGIN })
274 const plugin = body.data[0]
275 expect(plugin.version).to.equal(oldVersion)
277 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
278 expect(updatedPackageJSON.version).to.equal(oldVersion)
282 it('Should uninstall the plugin', async function () {
283 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
285 const body = await command.list({ pluginType: PluginType.PLUGIN })
286 expect(body.total).to.equal(0)
287 expect(body.data).to.have.lengthOf(0)
290 it('Should list uninstalled plugins', async function () {
291 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
292 expect(body.total).to.equal(1)
293 expect(body.data).to.have.lengthOf(1)
295 const plugin = body.data[0]
296 expect(plugin.name).to.equal('hello-world')
297 expect(plugin.enabled).to.be.false
298 expect(plugin.uninstalled).to.be.true
301 it('Should uninstall the theme', async function () {
302 await command.uninstall({ npmName: 'peertube-theme-background-red' })
305 it('Should have updated the configuration', async function () {
306 const config = await server.configCommand.getConfig()
308 expect(config.theme.default).to.equal('default')
310 const theme = config.theme.registered.find(r => r.name === 'background-red')
311 expect(theme).to.be.undefined
313 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
314 expect(plugin).to.be.undefined
317 it('Should have updated the user theme', async function () {
318 const res = await getMyUserInformation(server.url, server.accessToken)
319 expect((res.body as User).theme).to.equal('instance-default')
322 it('Should not install a broken plugin', async function () {
325 async function check () {
326 const body = await command.list({ pluginType: PluginType.PLUGIN })
327 const plugins = body.data
328 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
331 await command.install({
332 path: PluginsCommand.getPluginTestPath('-broken'),
333 expectedStatus: HttpStatusCode.BAD_REQUEST_400
338 killallServers([ server ])
339 await reRunServer(server)
344 after(async function () {
345 await closeAllSequelize([ server ])
346 await cleanupTests([ server ])