1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
20 setAccessTokensToServers,
22 testHelloWorldRegisteredSettings,
24 updateCustomSubConfig,
27 updatePluginPackageJSON,
31 } from '../../../../shared/extra-utils'
32 import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
33 import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugin.model'
34 import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
35 import { PluginType } from '../../../../shared/models/plugins/plugin.type'
36 import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
37 import { ServerConfig } from '../../../../shared/models/server'
38 import { User } from '../../../../shared/models/users'
40 const expect = chai.expect
42 describe('Test plugins', function () {
43 let server: ServerInfo = null
45 before(async function () {
48 const configOverride = {
50 index: { check_latest_versions_interval: '5 seconds' }
53 server = await flushAndRunServer(1, configOverride)
54 await setAccessTokensToServers([ server ])
57 it('Should list and search available plugins and themes', async function () {
61 const res = await listAvailablePlugins({
63 accessToken: server.accessToken,
66 pluginType: PluginType.THEME,
67 search: 'background-red'
70 expect(res.body.total).to.be.at.least(1)
71 expect(res.body.data).to.have.lengthOf(1)
75 const res1 = await listAvailablePlugins({
77 accessToken: server.accessToken,
82 const data1: PeerTubePluginIndex[] = res1.body.data
84 expect(res1.body.total).to.be.at.least(2)
85 expect(data1).to.have.lengthOf(2)
87 const res2 = await listAvailablePlugins({
89 accessToken: server.accessToken,
94 const data2: PeerTubePluginIndex[] = res2.body.data
96 expect(res2.body.total).to.be.at.least(2)
97 expect(data2).to.have.lengthOf(2)
99 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
103 const res = await listAvailablePlugins({
105 accessToken: server.accessToken,
108 pluginType: PluginType.THEME,
109 search: 'background-red',
110 currentPeerTubeEngine: '1.0.0'
112 const data: PeerTubePluginIndex[] = res.body.data
114 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
115 expect(p).to.be.undefined
119 it('Should install a plugin and a theme', async function () {
122 await installPlugin({
124 accessToken: server.accessToken,
125 npmName: 'peertube-plugin-hello-world'
128 await installPlugin({
130 accessToken: server.accessToken,
131 npmName: 'peertube-theme-background-red'
135 it('Should have the plugin loaded in the configuration', async function () {
136 const res = await getConfig(server.url)
137 const config: ServerConfig = res.body
139 const theme = config.theme.registered.find(r => r.name === 'background-red')
140 expect(theme).to.not.be.undefined
142 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
143 expect(plugin).to.not.be.undefined
146 it('Should update the default theme in the configuration', async function () {
147 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
149 const res = await getConfig(server.url)
150 const config: ServerConfig = res.body
152 expect(config.theme.default).to.equal('background-red')
155 it('Should update my default theme', async function () {
158 accessToken: server.accessToken,
159 theme: 'background-red'
162 const res = await getMyUserInformation(server.url, server.accessToken)
163 expect((res.body as User).theme).to.equal('background-red')
166 it('Should list plugins and themes', async function () {
168 const res = await listPlugins({
170 accessToken: server.accessToken,
173 pluginType: PluginType.THEME
175 const data: PeerTubePlugin[] = res.body.data
177 expect(res.body.total).to.be.at.least(1)
178 expect(data).to.have.lengthOf(1)
179 expect(data[0].name).to.equal('background-red')
183 const res = await listPlugins({
185 accessToken: server.accessToken,
190 const data: PeerTubePlugin[] = res.body.data
192 expect(data[0].name).to.equal('background-red')
193 expect(data[1].name).to.equal('hello-world')
197 const res = await listPlugins({
199 accessToken: server.accessToken,
204 const data: PeerTubePlugin[] = res.body.data
206 expect(data[0].name).to.equal('hello-world')
210 it('Should get registered settings', async function () {
211 await testHelloWorldRegisteredSettings(server)
214 it('Should get public settings', async function () {
215 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
217 const publicSettings = (res.body as PublicServerSetting).publicSettings
219 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
220 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
221 expect(publicSettings['user-name']).to.be.null
224 it('Should update the settings', async function () {
229 await updatePluginSettings({
231 accessToken: server.accessToken,
232 npmName: 'peertube-plugin-hello-world',
237 it('Should have watched settings changes', async function () {
240 await waitUntilLog(server, 'Settings changed!')
243 it('Should get a plugin and a theme', async function () {
245 const res = await getPlugin({
247 accessToken: server.accessToken,
248 npmName: 'peertube-plugin-hello-world'
251 const plugin: PeerTubePlugin = res.body
253 expect(plugin.type).to.equal(PluginType.PLUGIN)
254 expect(plugin.name).to.equal('hello-world')
255 expect(plugin.description).to.exist
256 expect(plugin.homepage).to.exist
257 expect(plugin.uninstalled).to.be.false
258 expect(plugin.enabled).to.be.true
259 expect(plugin.description).to.exist
260 expect(plugin.version).to.exist
261 expect(plugin.peertubeEngine).to.exist
262 expect(plugin.createdAt).to.exist
264 expect(plugin.settings).to.not.be.undefined
265 expect(plugin.settings['admin-name']).to.equal('Cid')
269 const res = await getPlugin({
271 accessToken: server.accessToken,
272 npmName: 'peertube-theme-background-red'
275 const plugin: PeerTubePlugin = res.body
277 expect(plugin.type).to.equal(PluginType.THEME)
278 expect(plugin.name).to.equal('background-red')
279 expect(plugin.description).to.exist
280 expect(plugin.homepage).to.exist
281 expect(plugin.uninstalled).to.be.false
282 expect(plugin.enabled).to.be.true
283 expect(plugin.description).to.exist
284 expect(plugin.version).to.exist
285 expect(plugin.peertubeEngine).to.exist
286 expect(plugin.createdAt).to.exist
288 expect(plugin.settings).to.be.null
292 it('Should update the plugin and the theme', async function () {
295 // Wait the scheduler that get the latest plugins versions
298 // Fake update our plugin version
299 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
301 // Fake update package.json
302 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
303 const oldVersion = packageJSON.version
305 packageJSON.version = '0.0.1'
306 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
308 // Restart the server to take into account this change
309 killallServers([ server ])
310 await reRunServer(server)
313 const res = await listPlugins({
315 accessToken: server.accessToken,
316 pluginType: PluginType.PLUGIN
319 const plugin: PeerTubePlugin = res.body.data[0]
321 expect(plugin.version).to.equal('0.0.1')
322 expect(plugin.latestVersion).to.exist
323 expect(plugin.latestVersion).to.not.equal('0.0.1')
329 accessToken: server.accessToken,
330 npmName: 'peertube-plugin-hello-world'
333 const res = await listPlugins({
335 accessToken: server.accessToken,
336 pluginType: PluginType.PLUGIN
339 const plugin: PeerTubePlugin = res.body.data[0]
341 expect(plugin.version).to.equal(oldVersion)
343 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
344 expect(updatedPackageJSON.version).to.equal(oldVersion)
348 it('Should uninstall the plugin', async function () {
349 await uninstallPlugin({
351 accessToken: server.accessToken,
352 npmName: 'peertube-plugin-hello-world'
355 const res = await listPlugins({
357 accessToken: server.accessToken,
358 pluginType: PluginType.PLUGIN
361 expect(res.body.total).to.equal(0)
362 expect(res.body.data).to.have.lengthOf(0)
365 it('Should list uninstalled plugins', async function () {
366 const res = await listPlugins({
368 accessToken: server.accessToken,
369 pluginType: PluginType.PLUGIN,
373 expect(res.body.total).to.equal(1)
374 expect(res.body.data).to.have.lengthOf(1)
376 const plugin: PeerTubePlugin = res.body.data[0]
377 expect(plugin.name).to.equal('hello-world')
378 expect(plugin.enabled).to.be.false
379 expect(plugin.uninstalled).to.be.true
382 it('Should uninstall the theme', async function () {
383 await uninstallPlugin({
385 accessToken: server.accessToken,
386 npmName: 'peertube-theme-background-red'
390 it('Should have updated the configuration', async function () {
391 // get /config (default theme + registered themes + registered plugins)
392 const res = await getConfig(server.url)
393 const config: ServerConfig = res.body
395 expect(config.theme.default).to.equal('default')
397 const theme = config.theme.registered.find(r => r.name === 'background-red')
398 expect(theme).to.be.undefined
400 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
401 expect(plugin).to.be.undefined
404 it('Should have updated the user theme', async function () {
405 const res = await getMyUserInformation(server.url, server.accessToken)
406 expect((res.body as User).theme).to.equal('instance-default')
409 after(async function () {
410 await closeAllSequelize([ server ])
411 await cleanupTests([ server ])