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,
21 wait, getPublicSettings
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 { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
27 import { User } from '../../../../shared/models/users'
28 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
29 import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
30 import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
32 const expect = chai.expect
34 describe('Test plugins', function () {
35 let server: ServerInfo = null
37 before(async function () {
40 const configOverride = {
42 index: { check_latest_versions_interval: '5 seconds' }
45 server = await flushAndRunServer(1, configOverride)
46 await setAccessTokensToServers([ server ])
49 it('Should list and search available plugins and themes', async function () {
53 const res = await listAvailablePlugins({
55 accessToken: server.accessToken,
58 pluginType: PluginType.THEME,
59 search: 'background-red'
62 expect(res.body.total).to.be.at.least(1)
63 expect(res.body.data).to.have.lengthOf(1)
67 const res1 = await listAvailablePlugins({
69 accessToken: server.accessToken,
74 const data1: PeerTubePluginIndex[] = res1.body.data
76 expect(res1.body.total).to.be.at.least(2)
77 expect(data1).to.have.lengthOf(2)
79 const res2 = await listAvailablePlugins({
81 accessToken: server.accessToken,
86 const data2: PeerTubePluginIndex[] = res2.body.data
88 expect(res2.body.total).to.be.at.least(2)
89 expect(data2).to.have.lengthOf(2)
91 expect(data1[0].npmName).to.not.equal(data2[ 0 ].npmName)
95 const res = await listAvailablePlugins({
97 accessToken: server.accessToken,
100 pluginType: PluginType.THEME,
101 search: 'background-red',
102 currentPeerTubeEngine: '1.0.0'
104 const data: PeerTubePluginIndex[] = res.body.data
106 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
107 expect(p).to.be.undefined
111 it('Should have an empty global css', async function () {
112 const res = await getPluginsCSS(server.url)
114 expect(res.text).to.be.empty
117 it('Should install a plugin and a theme', async function () {
120 await installPlugin({
122 accessToken: server.accessToken,
123 npmName: 'peertube-plugin-hello-world'
126 await installPlugin({
128 accessToken: server.accessToken,
129 npmName: 'peertube-theme-background-red'
133 it('Should have the correct global css', async function () {
134 const res = await getPluginsCSS(server.url)
136 expect(res.text).to.contain('--mainBackgroundColor')
139 it('Should have the plugin loaded in the configuration', async function () {
140 const res = await getConfig(server.url)
141 const config: ServerConfig = res.body
143 const theme = config.theme.registered.find(r => r.name === 'background-red')
144 expect(theme).to.not.be.undefined
146 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
147 expect(plugin).to.not.be.undefined
150 it('Should update the default theme in the configuration', async function () {
151 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
153 const res = await getConfig(server.url)
154 const config: ServerConfig = res.body
156 expect(config.theme.default).to.equal('background-red')
159 it('Should update my default theme', async function () {
162 accessToken: server.accessToken,
163 theme: 'background-red'
166 const res = await getMyUserInformation(server.url, server.accessToken)
167 expect((res.body as User).theme).to.equal('background-red')
170 it('Should list plugins and themes', async function () {
172 const res = await listPlugins({
174 accessToken: server.accessToken,
177 pluginType: PluginType.THEME
179 const data: PeerTubePlugin[] = res.body.data
181 expect(res.body.total).to.be.at.least(1)
182 expect(data).to.have.lengthOf(1)
183 expect(data[0].name).to.equal('background-red')
187 const res = await listPlugins({
189 accessToken: server.accessToken,
194 const data: PeerTubePlugin[] = res.body.data
196 expect(data[0].name).to.equal('background-red')
197 expect(data[1].name).to.equal('hello-world')
201 const res = await listPlugins({
203 accessToken: server.accessToken,
208 const data: PeerTubePlugin[] = res.body.data
210 expect(data[0].name).to.equal('hello-world')
214 it('Should get registered settings', async function () {
215 const res = await getPluginRegisteredSettings({
217 accessToken: server.accessToken,
218 npmName: 'peertube-plugin-hello-world'
221 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
223 expect(registeredSettings).to.have.length.at.least(1)
225 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
226 expect(adminNameSettings).to.not.be.undefined
229 it('Should get public settings', async function () {
230 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
232 const publicSettings = (res.body as PublicServerSetting).publicSettings
234 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
235 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
236 expect(publicSettings['user-name']).to.be.null
239 it('Should update the settings', async function () {
244 await updatePluginSettings({
246 accessToken: server.accessToken,
247 npmName: 'peertube-plugin-hello-world',
252 it('Should get a plugin and a theme', async function () {
254 const res = await getPlugin({
256 accessToken: server.accessToken,
257 npmName: 'peertube-plugin-hello-world'
260 const plugin: PeerTubePlugin = res.body
262 expect(plugin.type).to.equal(PluginType.PLUGIN)
263 expect(plugin.name).to.equal('hello-world')
264 expect(plugin.description).to.exist
265 expect(plugin.homepage).to.exist
266 expect(plugin.uninstalled).to.be.false
267 expect(plugin.enabled).to.be.true
268 expect(plugin.description).to.exist
269 expect(plugin.version).to.exist
270 expect(plugin.peertubeEngine).to.exist
271 expect(plugin.createdAt).to.exist
273 expect(plugin.settings).to.not.be.undefined
274 expect(plugin.settings['admin-name']).to.equal('Cid')
278 const res = await getPlugin({
280 accessToken: server.accessToken,
281 npmName: 'peertube-theme-background-red'
284 const plugin: PeerTubePlugin = res.body
286 expect(plugin.type).to.equal(PluginType.THEME)
287 expect(plugin.name).to.equal('background-red')
288 expect(plugin.description).to.exist
289 expect(plugin.homepage).to.exist
290 expect(plugin.uninstalled).to.be.false
291 expect(plugin.enabled).to.be.true
292 expect(plugin.description).to.exist
293 expect(plugin.version).to.exist
294 expect(plugin.peertubeEngine).to.exist
295 expect(plugin.createdAt).to.exist
297 expect(plugin.settings).to.be.null
301 it('Should update the plugin and the theme', async function () {
304 // Wait the scheduler that get the latest plugins versions
307 // Fake update our plugin version
308 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
310 // Fake update package.json
311 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
312 const oldVersion = packageJSON.version
314 packageJSON.version = '0.0.1'
315 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
317 // Restart the server to take into account this change
318 killallServers([ server ])
319 await reRunServer(server)
322 const res = await listPlugins({
324 accessToken: server.accessToken,
325 pluginType: PluginType.PLUGIN
328 const plugin: PeerTubePlugin = res.body.data[0]
330 expect(plugin.version).to.equal('0.0.1')
331 expect(plugin.latestVersion).to.exist
332 expect(plugin.latestVersion).to.not.equal('0.0.1')
338 accessToken: server.accessToken,
339 npmName: 'peertube-plugin-hello-world'
342 const res = await listPlugins({
344 accessToken: server.accessToken,
345 pluginType: PluginType.PLUGIN
348 const plugin: PeerTubePlugin = res.body.data[0]
350 expect(plugin.version).to.equal(oldVersion)
352 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
353 expect(updatedPackageJSON.version).to.equal(oldVersion)
357 it('Should uninstall the plugin', async function () {
358 await uninstallPlugin({
360 accessToken: server.accessToken,
361 npmName: 'peertube-plugin-hello-world'
364 const res = await listPlugins({
366 accessToken: server.accessToken,
367 pluginType: PluginType.PLUGIN
370 expect(res.body.total).to.equal(0)
371 expect(res.body.data).to.have.lengthOf(0)
374 it('Should have an empty global css', async function () {
375 const res = await getPluginsCSS(server.url)
377 expect(res.text).to.be.empty
380 it('Should list uninstalled plugins', async function () {
381 const res = await listPlugins({
383 accessToken: server.accessToken,
384 pluginType: PluginType.PLUGIN,
388 expect(res.body.total).to.equal(1)
389 expect(res.body.data).to.have.lengthOf(1)
391 const plugin: PeerTubePlugin = res.body.data[0]
392 expect(plugin.name).to.equal('hello-world')
393 expect(plugin.enabled).to.be.false
394 expect(plugin.uninstalled).to.be.true
397 it('Should uninstall the theme', async function () {
398 await uninstallPlugin({
400 accessToken: server.accessToken,
401 npmName: 'peertube-theme-background-red'
405 it('Should have updated the configuration', async function () {
406 // get /config (default theme + registered themes + registered plugins)
407 const res = await getConfig(server.url)
408 const config: ServerConfig = res.body
410 expect(config.theme.default).to.equal('default')
412 const theme = config.theme.registered.find(r => r.name === 'background-red')
413 expect(theme).to.be.undefined
415 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
416 expect(plugin).to.be.undefined
419 it('Should have updated the user theme', async function () {
420 const res = await getMyUserInformation(server.url, server.accessToken)
421 expect((res.body as User).theme).to.equal('instance-default')
424 after(async function () {
425 await closeAllSequelize([ server ])
426 await cleanupTests([ server ])