1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
13 getPluginRegisteredSettings,
22 setAccessTokensToServers,
25 updateCustomSubConfig,
28 updatePluginPackageJSON,
31 } from '../../../../shared/extra-utils'
32 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
33 import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
34 import { ServerConfig } from '../../../../shared/models/server'
35 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
36 import { User } from '../../../../shared/models/users'
37 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
38 import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
39 import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
41 const expect = chai.expect
43 describe('Test plugins', function () {
44 let server: ServerInfo = null
46 before(async function () {
49 const configOverride = {
51 index: { check_latest_versions_interval: '5 seconds' }
54 server = await flushAndRunServer(1, configOverride)
55 await setAccessTokensToServers([ server ])
58 it('Should list and search available plugins and themes', async function () {
62 const res = await listAvailablePlugins({
64 accessToken: server.accessToken,
67 pluginType: PluginType.THEME,
68 search: 'background-red'
71 expect(res.body.total).to.be.at.least(1)
72 expect(res.body.data).to.have.lengthOf(1)
76 const res1 = await listAvailablePlugins({
78 accessToken: server.accessToken,
83 const data1: PeerTubePluginIndex[] = res1.body.data
85 expect(res1.body.total).to.be.at.least(2)
86 expect(data1).to.have.lengthOf(2)
88 const res2 = await listAvailablePlugins({
90 accessToken: server.accessToken,
95 const data2: PeerTubePluginIndex[] = res2.body.data
97 expect(res2.body.total).to.be.at.least(2)
98 expect(data2).to.have.lengthOf(2)
100 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
104 const res = await listAvailablePlugins({
106 accessToken: server.accessToken,
109 pluginType: PluginType.THEME,
110 search: 'background-red',
111 currentPeerTubeEngine: '1.0.0'
113 const data: PeerTubePluginIndex[] = res.body.data
115 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
116 expect(p).to.be.undefined
120 it('Should have an empty global css', async function () {
121 const res = await getPluginsCSS(server.url)
123 expect(res.text).to.be.empty
126 it('Should install a plugin and a theme', async function () {
129 await installPlugin({
131 accessToken: server.accessToken,
132 npmName: 'peertube-plugin-hello-world'
135 await installPlugin({
137 accessToken: server.accessToken,
138 npmName: 'peertube-theme-background-red'
142 it('Should have the correct global css', async function () {
143 const res = await getPluginsCSS(server.url)
145 expect(res.text).to.contain('--mainBackgroundColor')
148 it('Should have the plugin loaded in the configuration', async function () {
149 const res = await getConfig(server.url)
150 const config: ServerConfig = res.body
152 const theme = config.theme.registered.find(r => r.name === 'background-red')
153 expect(theme).to.not.be.undefined
155 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
156 expect(plugin).to.not.be.undefined
159 it('Should update the default theme in the configuration', async function () {
160 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
162 const res = await getConfig(server.url)
163 const config: ServerConfig = res.body
165 expect(config.theme.default).to.equal('background-red')
168 it('Should update my default theme', async function () {
171 accessToken: server.accessToken,
172 theme: 'background-red'
175 const res = await getMyUserInformation(server.url, server.accessToken)
176 expect((res.body as User).theme).to.equal('background-red')
179 it('Should list plugins and themes', async function () {
181 const res = await listPlugins({
183 accessToken: server.accessToken,
186 pluginType: PluginType.THEME
188 const data: PeerTubePlugin[] = res.body.data
190 expect(res.body.total).to.be.at.least(1)
191 expect(data).to.have.lengthOf(1)
192 expect(data[0].name).to.equal('background-red')
196 const res = await listPlugins({
198 accessToken: server.accessToken,
203 const data: PeerTubePlugin[] = res.body.data
205 expect(data[0].name).to.equal('background-red')
206 expect(data[1].name).to.equal('hello-world')
210 const res = await listPlugins({
212 accessToken: server.accessToken,
217 const data: PeerTubePlugin[] = res.body.data
219 expect(data[0].name).to.equal('hello-world')
223 it('Should get registered settings', async function () {
224 const res = await getPluginRegisteredSettings({
226 accessToken: server.accessToken,
227 npmName: 'peertube-plugin-hello-world'
230 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
232 expect(registeredSettings).to.have.length.at.least(1)
234 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
235 expect(adminNameSettings).to.not.be.undefined
238 it('Should get public settings', async function () {
239 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
241 const publicSettings = (res.body as PublicServerSetting).publicSettings
243 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
244 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
245 expect(publicSettings['user-name']).to.be.null
248 it('Should update the settings', async function () {
253 await updatePluginSettings({
255 accessToken: server.accessToken,
256 npmName: 'peertube-plugin-hello-world',
261 it('Should get a plugin and a theme', async function () {
263 const res = await getPlugin({
265 accessToken: server.accessToken,
266 npmName: 'peertube-plugin-hello-world'
269 const plugin: PeerTubePlugin = res.body
271 expect(plugin.type).to.equal(PluginType.PLUGIN)
272 expect(plugin.name).to.equal('hello-world')
273 expect(plugin.description).to.exist
274 expect(plugin.homepage).to.exist
275 expect(plugin.uninstalled).to.be.false
276 expect(plugin.enabled).to.be.true
277 expect(plugin.description).to.exist
278 expect(plugin.version).to.exist
279 expect(plugin.peertubeEngine).to.exist
280 expect(plugin.createdAt).to.exist
282 expect(plugin.settings).to.not.be.undefined
283 expect(plugin.settings['admin-name']).to.equal('Cid')
287 const res = await getPlugin({
289 accessToken: server.accessToken,
290 npmName: 'peertube-theme-background-red'
293 const plugin: PeerTubePlugin = res.body
295 expect(plugin.type).to.equal(PluginType.THEME)
296 expect(plugin.name).to.equal('background-red')
297 expect(plugin.description).to.exist
298 expect(plugin.homepage).to.exist
299 expect(plugin.uninstalled).to.be.false
300 expect(plugin.enabled).to.be.true
301 expect(plugin.description).to.exist
302 expect(plugin.version).to.exist
303 expect(plugin.peertubeEngine).to.exist
304 expect(plugin.createdAt).to.exist
306 expect(plugin.settings).to.be.null
310 it('Should update the plugin and the theme', async function () {
313 // Wait the scheduler that get the latest plugins versions
316 // Fake update our plugin version
317 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
319 // Fake update package.json
320 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
321 const oldVersion = packageJSON.version
323 packageJSON.version = '0.0.1'
324 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
326 // Restart the server to take into account this change
327 killallServers([ server ])
328 await reRunServer(server)
331 const res = await listPlugins({
333 accessToken: server.accessToken,
334 pluginType: PluginType.PLUGIN
337 const plugin: PeerTubePlugin = res.body.data[0]
339 expect(plugin.version).to.equal('0.0.1')
340 expect(plugin.latestVersion).to.exist
341 expect(plugin.latestVersion).to.not.equal('0.0.1')
347 accessToken: server.accessToken,
348 npmName: 'peertube-plugin-hello-world'
351 const res = await listPlugins({
353 accessToken: server.accessToken,
354 pluginType: PluginType.PLUGIN
357 const plugin: PeerTubePlugin = res.body.data[0]
359 expect(plugin.version).to.equal(oldVersion)
361 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
362 expect(updatedPackageJSON.version).to.equal(oldVersion)
366 it('Should uninstall the plugin', async function () {
367 await uninstallPlugin({
369 accessToken: server.accessToken,
370 npmName: 'peertube-plugin-hello-world'
373 const res = await listPlugins({
375 accessToken: server.accessToken,
376 pluginType: PluginType.PLUGIN
379 expect(res.body.total).to.equal(0)
380 expect(res.body.data).to.have.lengthOf(0)
383 it('Should have an empty global css', async function () {
384 const res = await getPluginsCSS(server.url)
386 expect(res.text).to.be.empty
389 it('Should list uninstalled plugins', async function () {
390 const res = await listPlugins({
392 accessToken: server.accessToken,
393 pluginType: PluginType.PLUGIN,
397 expect(res.body.total).to.equal(1)
398 expect(res.body.data).to.have.lengthOf(1)
400 const plugin: PeerTubePlugin = res.body.data[0]
401 expect(plugin.name).to.equal('hello-world')
402 expect(plugin.enabled).to.be.false
403 expect(plugin.uninstalled).to.be.true
406 it('Should uninstall the theme', async function () {
407 await uninstallPlugin({
409 accessToken: server.accessToken,
410 npmName: 'peertube-theme-background-red'
414 it('Should have updated the configuration', async function () {
415 // get /config (default theme + registered themes + registered plugins)
416 const res = await getConfig(server.url)
417 const config: ServerConfig = res.body
419 expect(config.theme.default).to.equal('default')
421 const theme = config.theme.registered.find(r => r.name === 'background-red')
422 expect(theme).to.be.undefined
424 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
425 expect(plugin).to.be.undefined
428 it('Should have updated the user theme', async function () {
429 const res = await getMyUserInformation(server.url, server.accessToken)
430 expect((res.body as User).theme).to.equal('instance-default')
433 after(async function () {
434 await closeAllSequelize([ server ])
435 await cleanupTests([ server ])