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,
33 } from '../../../../shared/extra-utils'
34 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
35 import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
36 import { ServerConfig } from '../../../../shared/models/server'
37 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
38 import { User } from '../../../../shared/models/users'
39 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
40 import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
41 import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
43 const expect = chai.expect
45 describe('Test plugins', function () {
46 let server: ServerInfo = null
48 before(async function () {
51 const configOverride = {
53 index: { check_latest_versions_interval: '5 seconds' }
56 server = await flushAndRunServer(1, configOverride)
57 await setAccessTokensToServers([ server ])
60 it('Should list and search available plugins and themes', async function () {
64 const res = await listAvailablePlugins({
66 accessToken: server.accessToken,
69 pluginType: PluginType.THEME,
70 search: 'background-red'
73 expect(res.body.total).to.be.at.least(1)
74 expect(res.body.data).to.have.lengthOf(1)
78 const res1 = await listAvailablePlugins({
80 accessToken: server.accessToken,
85 const data1: PeerTubePluginIndex[] = res1.body.data
87 expect(res1.body.total).to.be.at.least(2)
88 expect(data1).to.have.lengthOf(2)
90 const res2 = await listAvailablePlugins({
92 accessToken: server.accessToken,
97 const data2: PeerTubePluginIndex[] = res2.body.data
99 expect(res2.body.total).to.be.at.least(2)
100 expect(data2).to.have.lengthOf(2)
102 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
106 const res = await listAvailablePlugins({
108 accessToken: server.accessToken,
111 pluginType: PluginType.THEME,
112 search: 'background-red',
113 currentPeerTubeEngine: '1.0.0'
115 const data: PeerTubePluginIndex[] = res.body.data
117 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
118 expect(p).to.be.undefined
122 it('Should have an empty global css', async function () {
124 const res = await getPluginsCSS(server.url)
125 expect(res.text).to.be.empty
128 for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) {
129 const res = await makeHTMLRequest(server.url, path)
130 expect(res.text).to.not.include('link rel="stylesheet" href="/plugins/global.css')
134 it('Should install a plugin and a theme', async function () {
137 await installPlugin({
139 accessToken: server.accessToken,
140 npmName: 'peertube-plugin-hello-world'
143 await installPlugin({
145 accessToken: server.accessToken,
146 npmName: 'peertube-theme-background-red'
150 it('Should have the correct global css', async function () {
152 const res = await getPluginsCSS(server.url)
153 expect(res.text).to.contain('background-color: red')
156 for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) {
157 const res = await makeHTMLRequest(server.url, path)
158 expect(res.text).to.include('link rel="stylesheet" href="/plugins/global.css')
162 it('Should have the plugin loaded in the configuration', async function () {
163 const res = await getConfig(server.url)
164 const config: ServerConfig = res.body
166 const theme = config.theme.registered.find(r => r.name === 'background-red')
167 expect(theme).to.not.be.undefined
169 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
170 expect(plugin).to.not.be.undefined
173 it('Should update the default theme in the configuration', async function () {
174 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
176 const res = await getConfig(server.url)
177 const config: ServerConfig = res.body
179 expect(config.theme.default).to.equal('background-red')
182 it('Should update my default theme', async function () {
185 accessToken: server.accessToken,
186 theme: 'background-red'
189 const res = await getMyUserInformation(server.url, server.accessToken)
190 expect((res.body as User).theme).to.equal('background-red')
193 it('Should list plugins and themes', async function () {
195 const res = await listPlugins({
197 accessToken: server.accessToken,
200 pluginType: PluginType.THEME
202 const data: PeerTubePlugin[] = res.body.data
204 expect(res.body.total).to.be.at.least(1)
205 expect(data).to.have.lengthOf(1)
206 expect(data[0].name).to.equal('background-red')
210 const res = await listPlugins({
212 accessToken: server.accessToken,
217 const data: PeerTubePlugin[] = res.body.data
219 expect(data[0].name).to.equal('background-red')
220 expect(data[1].name).to.equal('hello-world')
224 const res = await listPlugins({
226 accessToken: server.accessToken,
231 const data: PeerTubePlugin[] = res.body.data
233 expect(data[0].name).to.equal('hello-world')
237 it('Should get registered settings', async function () {
238 const res = await getPluginRegisteredSettings({
240 accessToken: server.accessToken,
241 npmName: 'peertube-plugin-hello-world'
244 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
246 expect(registeredSettings).to.have.length.at.least(1)
248 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
249 expect(adminNameSettings).to.not.be.undefined
252 it('Should get public settings', async function () {
253 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
255 const publicSettings = (res.body as PublicServerSetting).publicSettings
257 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
258 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
259 expect(publicSettings['user-name']).to.be.null
262 it('Should update the settings', async function () {
267 await updatePluginSettings({
269 accessToken: server.accessToken,
270 npmName: 'peertube-plugin-hello-world',
275 it('Should have watched settings changes', async function () {
278 await waitUntilLog(server, 'Settings changed!')
281 it('Should get a plugin and a theme', async function () {
283 const res = await getPlugin({
285 accessToken: server.accessToken,
286 npmName: 'peertube-plugin-hello-world'
289 const plugin: PeerTubePlugin = res.body
291 expect(plugin.type).to.equal(PluginType.PLUGIN)
292 expect(plugin.name).to.equal('hello-world')
293 expect(plugin.description).to.exist
294 expect(plugin.homepage).to.exist
295 expect(plugin.uninstalled).to.be.false
296 expect(plugin.enabled).to.be.true
297 expect(plugin.description).to.exist
298 expect(plugin.version).to.exist
299 expect(plugin.peertubeEngine).to.exist
300 expect(plugin.createdAt).to.exist
302 expect(plugin.settings).to.not.be.undefined
303 expect(plugin.settings['admin-name']).to.equal('Cid')
307 const res = await getPlugin({
309 accessToken: server.accessToken,
310 npmName: 'peertube-theme-background-red'
313 const plugin: PeerTubePlugin = res.body
315 expect(plugin.type).to.equal(PluginType.THEME)
316 expect(plugin.name).to.equal('background-red')
317 expect(plugin.description).to.exist
318 expect(plugin.homepage).to.exist
319 expect(plugin.uninstalled).to.be.false
320 expect(plugin.enabled).to.be.true
321 expect(plugin.description).to.exist
322 expect(plugin.version).to.exist
323 expect(plugin.peertubeEngine).to.exist
324 expect(plugin.createdAt).to.exist
326 expect(plugin.settings).to.be.null
330 it('Should update the plugin and the theme', async function () {
333 // Wait the scheduler that get the latest plugins versions
336 // Fake update our plugin version
337 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
339 // Fake update package.json
340 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
341 const oldVersion = packageJSON.version
343 packageJSON.version = '0.0.1'
344 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
346 // Restart the server to take into account this change
347 killallServers([ server ])
348 await reRunServer(server)
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('0.0.1')
360 expect(plugin.latestVersion).to.exist
361 expect(plugin.latestVersion).to.not.equal('0.0.1')
367 accessToken: server.accessToken,
368 npmName: 'peertube-plugin-hello-world'
371 const res = await listPlugins({
373 accessToken: server.accessToken,
374 pluginType: PluginType.PLUGIN
377 const plugin: PeerTubePlugin = res.body.data[0]
379 expect(plugin.version).to.equal(oldVersion)
381 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
382 expect(updatedPackageJSON.version).to.equal(oldVersion)
386 it('Should uninstall the plugin', async function () {
387 await uninstallPlugin({
389 accessToken: server.accessToken,
390 npmName: 'peertube-plugin-hello-world'
393 const res = await listPlugins({
395 accessToken: server.accessToken,
396 pluginType: PluginType.PLUGIN
399 expect(res.body.total).to.equal(0)
400 expect(res.body.data).to.have.lengthOf(0)
403 it('Should have an empty global css', async function () {
405 const res = await getPluginsCSS(server.url)
406 expect(res.text).to.be.empty
409 for (const path of [ '/', '/videos/embed/1', '/video-playlists/embed/1' ]) {
410 const res = await makeHTMLRequest(server.url, path)
411 expect(res.text).to.not.include('link rel="stylesheet" href="/plugins/global.css')
415 it('Should list uninstalled plugins', async function () {
416 const res = await listPlugins({
418 accessToken: server.accessToken,
419 pluginType: PluginType.PLUGIN,
423 expect(res.body.total).to.equal(1)
424 expect(res.body.data).to.have.lengthOf(1)
426 const plugin: PeerTubePlugin = res.body.data[0]
427 expect(plugin.name).to.equal('hello-world')
428 expect(plugin.enabled).to.be.false
429 expect(plugin.uninstalled).to.be.true
432 it('Should uninstall the theme', async function () {
433 await uninstallPlugin({
435 accessToken: server.accessToken,
436 npmName: 'peertube-theme-background-red'
440 it('Should have updated the configuration', async function () {
441 // get /config (default theme + registered themes + registered plugins)
442 const res = await getConfig(server.url)
443 const config: ServerConfig = res.body
445 expect(config.theme.default).to.equal('default')
447 const theme = config.theme.registered.find(r => r.name === 'background-red')
448 expect(theme).to.be.undefined
450 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
451 expect(plugin).to.be.undefined
454 it('Should have updated the user theme', async function () {
455 const res = await getMyUserInformation(server.url, server.accessToken)
456 expect((res.body as User).theme).to.equal('instance-default')
459 after(async function () {
460 await closeAllSequelize([ server ])
461 await cleanupTests([ server ])