1 /* tslint:disable:no-unused-expression */
4 import * as chai from 'chai'
9 getConfig, getMyUserInformation, getPluginPackageJSON,
11 getPluginRegisteredSettings,
13 installPlugin, killallServers,
15 listPlugins, reRunServer,
17 setAccessTokensToServers,
18 setPluginVersion, uninstallPlugin,
19 updateCustomSubConfig, updateMyUser, updatePluginPackageJSON, updatePlugin,
22 } from '../../../../shared/extra-utils'
23 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
24 import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
25 import { ServerConfig } from '../../../../shared/models/server'
26 import { RegisteredSettings } from '../../../../shared/models/plugins/register-setting.model'
27 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
28 import { User } from '../../../../shared/models/users'
29 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
31 const expect = chai.expect
33 describe('Test plugins', function () {
34 let server: ServerInfo = null
36 before(async function () {
39 const configOverride = {
41 index: { check_latest_versions_interval: '5 seconds' }
44 server = await flushAndRunServer(1, configOverride)
45 await setAccessTokensToServers([ server ])
48 it('Should list and search available plugins and themes', async function () {
52 const res = await listAvailablePlugins({
54 accessToken: server.accessToken,
57 pluginType: PluginType.THEME,
58 search: 'background-red'
61 expect(res.body.total).to.be.at.least(1)
62 expect(res.body.data).to.have.lengthOf(1)
66 const res1 = await listAvailablePlugins({
68 accessToken: server.accessToken,
73 const data1: PeerTubePluginIndex[] = res1.body.data
75 expect(res1.body.total).to.be.at.least(2)
76 expect(data1).to.have.lengthOf(2)
78 const res2 = await listAvailablePlugins({
80 accessToken: server.accessToken,
85 const data2: PeerTubePluginIndex[] = res2.body.data
87 expect(res2.body.total).to.be.at.least(2)
88 expect(data2).to.have.lengthOf(2)
90 expect(data1[0].npmName).to.not.equal(data2[ 0 ].npmName)
94 const res = await listAvailablePlugins({
96 accessToken: server.accessToken,
99 pluginType: PluginType.THEME,
100 search: 'background-red',
101 currentPeerTubeEngine: '1.0.0'
103 const data: PeerTubePluginIndex[] = res.body.data
105 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
106 expect(p).to.be.undefined
110 it('Should have an empty global css', async function () {
111 const res = await getPluginsCSS(server.url)
113 expect(res.text).to.be.empty
116 it('Should install a plugin and a theme', async function () {
119 await installPlugin({
121 accessToken: server.accessToken,
122 npmName: 'peertube-plugin-hello-world'
125 await installPlugin({
127 accessToken: server.accessToken,
128 npmName: 'peertube-theme-background-red'
132 it('Should have the correct global css', async function () {
133 const res = await getPluginsCSS(server.url)
135 expect(res.text).to.contain('--mainBackgroundColor')
138 it('Should have the plugin loaded in the configuration', async function () {
139 const res = await getConfig(server.url)
140 const config: ServerConfig = res.body
142 const theme = config.theme.registered.find(r => r.name === 'background-red')
143 expect(theme).to.not.be.undefined
145 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
146 expect(plugin).to.not.be.undefined
149 it('Should update the default theme in the configuration', async function () {
150 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
152 const res = await getConfig(server.url)
153 const config: ServerConfig = res.body
155 expect(config.theme.default).to.equal('background-red')
158 it('Should update my default theme', async function () {
161 accessToken: server.accessToken,
162 theme: 'background-red'
165 const res = await getMyUserInformation(server.url, server.accessToken)
166 expect((res.body as User).theme).to.equal('background-red')
169 it('Should list plugins and themes', async function () {
171 const res = await listPlugins({
173 accessToken: server.accessToken,
176 pluginType: PluginType.THEME
178 const data: PeerTubePlugin[] = res.body.data
180 expect(res.body.total).to.be.at.least(1)
181 expect(data).to.have.lengthOf(1)
182 expect(data[0].name).to.equal('background-red')
186 const res = await listPlugins({
188 accessToken: server.accessToken,
193 const data: PeerTubePlugin[] = res.body.data
195 expect(data[0].name).to.equal('background-red')
196 expect(data[1].name).to.equal('hello-world')
200 const res = await listPlugins({
202 accessToken: server.accessToken,
207 const data: PeerTubePlugin[] = res.body.data
209 expect(data[0].name).to.equal('hello-world')
213 it('Should get registered settings', async function () {
214 const res = await getPluginRegisteredSettings({
216 accessToken: server.accessToken,
217 npmName: 'peertube-plugin-hello-world'
220 const settings = (res.body as RegisteredSettings).settings
222 expect(settings).to.have.length.at.least(1)
224 const adminNameSettings = settings.find(s => s.name === 'admin-name')
225 expect(adminNameSettings).to.not.be.undefined
228 it('Should update the settings', async function () {
233 await updatePluginSettings({
235 accessToken: server.accessToken,
236 npmName: 'peertube-plugin-hello-world',
241 it('Should get a plugin and a theme', async function () {
243 const res = await getPlugin({
245 accessToken: server.accessToken,
246 npmName: 'peertube-plugin-hello-world'
249 const plugin: PeerTubePlugin = res.body
251 expect(plugin.type).to.equal(PluginType.PLUGIN)
252 expect(plugin.name).to.equal('hello-world')
253 expect(plugin.description).to.exist
254 expect(plugin.homepage).to.exist
255 expect(plugin.uninstalled).to.be.false
256 expect(plugin.enabled).to.be.true
257 expect(plugin.description).to.exist
258 expect(plugin.version).to.exist
259 expect(plugin.peertubeEngine).to.exist
260 expect(plugin.createdAt).to.exist
262 expect(plugin.settings).to.not.be.undefined
263 expect(plugin.settings['admin-name']).to.equal('Cid')
267 const res = await getPlugin({
269 accessToken: server.accessToken,
270 npmName: 'peertube-theme-background-red'
273 const plugin: PeerTubePlugin = res.body
275 expect(plugin.type).to.equal(PluginType.THEME)
276 expect(plugin.name).to.equal('background-red')
277 expect(plugin.description).to.exist
278 expect(plugin.homepage).to.exist
279 expect(plugin.uninstalled).to.be.false
280 expect(plugin.enabled).to.be.true
281 expect(plugin.description).to.exist
282 expect(plugin.version).to.exist
283 expect(plugin.peertubeEngine).to.exist
284 expect(plugin.createdAt).to.exist
286 expect(plugin.settings).to.be.null
290 it('Should update the plugin and the theme', async function () {
293 // Wait the scheduler that get the latest plugins versions
296 // Fake update our plugin version
297 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
299 // Fake update package.json
300 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
301 const oldVersion = packageJSON.version
303 packageJSON.version = '0.0.1'
304 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
306 // Restart the server to take into account this change
307 killallServers([ server ])
308 await reRunServer(server)
311 const res = await listPlugins({
313 accessToken: server.accessToken,
314 pluginType: PluginType.PLUGIN
317 const plugin: PeerTubePlugin = res.body.data[0]
319 expect(plugin.version).to.equal('0.0.1')
320 expect(plugin.latestVersion).to.exist
321 expect(plugin.latestVersion).to.not.equal('0.0.1')
327 accessToken: server.accessToken,
328 npmName: 'peertube-plugin-hello-world'
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(oldVersion)
341 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
342 expect(updatedPackageJSON.version).to.equal(oldVersion)
346 it('Should uninstall the plugin', async function () {
347 await uninstallPlugin({
349 accessToken: server.accessToken,
350 npmName: 'peertube-plugin-hello-world'
353 const res = await listPlugins({
355 accessToken: server.accessToken,
356 pluginType: PluginType.PLUGIN
359 expect(res.body.total).to.equal(0)
360 expect(res.body.data).to.have.lengthOf(0)
363 it('Should have an empty global css', async function () {
364 const res = await getPluginsCSS(server.url)
366 expect(res.text).to.be.empty
369 it('Should list uninstalled plugins', async function () {
370 const res = await listPlugins({
372 accessToken: server.accessToken,
373 pluginType: PluginType.PLUGIN,
377 expect(res.body.total).to.equal(1)
378 expect(res.body.data).to.have.lengthOf(1)
380 const plugin: PeerTubePlugin = res.body.data[0]
381 expect(plugin.name).to.equal('hello-world')
382 expect(plugin.enabled).to.be.false
383 expect(plugin.uninstalled).to.be.true
386 it('Should uninstall the theme', async function () {
387 await uninstallPlugin({
389 accessToken: server.accessToken,
390 npmName: 'peertube-theme-background-red'
394 it('Should have updated the configuration', async function () {
395 // get /config (default theme + registered themes + registered plugins)
396 const res = await getConfig(server.url)
397 const config: ServerConfig = res.body
399 expect(config.theme.default).to.equal('default')
401 const theme = config.theme.registered.find(r => r.name === 'background-red')
402 expect(theme).to.be.undefined
404 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
405 expect(plugin).to.be.undefined
408 it('Should have updated the user theme', async function () {
409 const res = await getMyUserInformation(server.url, server.accessToken)
410 expect((res.body as User).theme).to.equal('instance-default')
413 after(async function () {
414 await closeAllSequelize([ server ])
415 await cleanupTests([ server ])