1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
13 getPluginRegisteredSettings,
21 setAccessTokensToServers,
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 { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
38 import { ServerConfig } from '../../../../shared/models/server'
39 import { User } from '../../../../shared/models/users'
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 install a plugin and a theme', async function () {
123 await installPlugin({
125 accessToken: server.accessToken,
126 npmName: 'peertube-plugin-hello-world'
129 await installPlugin({
131 accessToken: server.accessToken,
132 npmName: 'peertube-theme-background-red'
136 it('Should have the plugin loaded in the configuration', async function () {
137 const res = await getConfig(server.url)
138 const config: ServerConfig = res.body
140 const theme = config.theme.registered.find(r => r.name === 'background-red')
141 expect(theme).to.not.be.undefined
143 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
144 expect(plugin).to.not.be.undefined
147 it('Should update the default theme in the configuration', async function () {
148 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
150 const res = await getConfig(server.url)
151 const config: ServerConfig = res.body
153 expect(config.theme.default).to.equal('background-red')
156 it('Should update my default theme', async function () {
159 accessToken: server.accessToken,
160 theme: 'background-red'
163 const res = await getMyUserInformation(server.url, server.accessToken)
164 expect((res.body as User).theme).to.equal('background-red')
167 it('Should list plugins and themes', async function () {
169 const res = await listPlugins({
171 accessToken: server.accessToken,
174 pluginType: PluginType.THEME
176 const data: PeerTubePlugin[] = res.body.data
178 expect(res.body.total).to.be.at.least(1)
179 expect(data).to.have.lengthOf(1)
180 expect(data[0].name).to.equal('background-red')
184 const res = await listPlugins({
186 accessToken: server.accessToken,
191 const data: PeerTubePlugin[] = res.body.data
193 expect(data[0].name).to.equal('background-red')
194 expect(data[1].name).to.equal('hello-world')
198 const res = await listPlugins({
200 accessToken: server.accessToken,
205 const data: PeerTubePlugin[] = res.body.data
207 expect(data[0].name).to.equal('hello-world')
211 it('Should get registered settings', async function () {
212 const res = await getPluginRegisteredSettings({
214 accessToken: server.accessToken,
215 npmName: 'peertube-plugin-hello-world'
218 const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
220 expect(registeredSettings).to.have.length.at.least(1)
222 const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
223 expect(adminNameSettings).to.not.be.undefined
226 it('Should get public settings', async function () {
227 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
229 const publicSettings = (res.body as PublicServerSetting).publicSettings
231 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
232 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
233 expect(publicSettings['user-name']).to.be.null
236 it('Should update the settings', async function () {
241 await updatePluginSettings({
243 accessToken: server.accessToken,
244 npmName: 'peertube-plugin-hello-world',
249 it('Should have watched settings changes', async function () {
252 await waitUntilLog(server, 'Settings changed!')
255 it('Should get a plugin and a theme', async function () {
257 const res = await getPlugin({
259 accessToken: server.accessToken,
260 npmName: 'peertube-plugin-hello-world'
263 const plugin: PeerTubePlugin = res.body
265 expect(plugin.type).to.equal(PluginType.PLUGIN)
266 expect(plugin.name).to.equal('hello-world')
267 expect(plugin.description).to.exist
268 expect(plugin.homepage).to.exist
269 expect(plugin.uninstalled).to.be.false
270 expect(plugin.enabled).to.be.true
271 expect(plugin.description).to.exist
272 expect(plugin.version).to.exist
273 expect(plugin.peertubeEngine).to.exist
274 expect(plugin.createdAt).to.exist
276 expect(plugin.settings).to.not.be.undefined
277 expect(plugin.settings['admin-name']).to.equal('Cid')
281 const res = await getPlugin({
283 accessToken: server.accessToken,
284 npmName: 'peertube-theme-background-red'
287 const plugin: PeerTubePlugin = res.body
289 expect(plugin.type).to.equal(PluginType.THEME)
290 expect(plugin.name).to.equal('background-red')
291 expect(plugin.description).to.exist
292 expect(plugin.homepage).to.exist
293 expect(plugin.uninstalled).to.be.false
294 expect(plugin.enabled).to.be.true
295 expect(plugin.description).to.exist
296 expect(plugin.version).to.exist
297 expect(plugin.peertubeEngine).to.exist
298 expect(plugin.createdAt).to.exist
300 expect(plugin.settings).to.be.null
304 it('Should update the plugin and the theme', async function () {
307 // Wait the scheduler that get the latest plugins versions
310 // Fake update our plugin version
311 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
313 // Fake update package.json
314 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
315 const oldVersion = packageJSON.version
317 packageJSON.version = '0.0.1'
318 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
320 // Restart the server to take into account this change
321 killallServers([ server ])
322 await reRunServer(server)
325 const res = await listPlugins({
327 accessToken: server.accessToken,
328 pluginType: PluginType.PLUGIN
331 const plugin: PeerTubePlugin = res.body.data[0]
333 expect(plugin.version).to.equal('0.0.1')
334 expect(plugin.latestVersion).to.exist
335 expect(plugin.latestVersion).to.not.equal('0.0.1')
341 accessToken: server.accessToken,
342 npmName: 'peertube-plugin-hello-world'
345 const res = await listPlugins({
347 accessToken: server.accessToken,
348 pluginType: PluginType.PLUGIN
351 const plugin: PeerTubePlugin = res.body.data[0]
353 expect(plugin.version).to.equal(oldVersion)
355 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
356 expect(updatedPackageJSON.version).to.equal(oldVersion)
360 it('Should uninstall the plugin', async function () {
361 await uninstallPlugin({
363 accessToken: server.accessToken,
364 npmName: 'peertube-plugin-hello-world'
367 const res = await listPlugins({
369 accessToken: server.accessToken,
370 pluginType: PluginType.PLUGIN
373 expect(res.body.total).to.equal(0)
374 expect(res.body.data).to.have.lengthOf(0)
377 it('Should list uninstalled plugins', async function () {
378 const res = await listPlugins({
380 accessToken: server.accessToken,
381 pluginType: PluginType.PLUGIN,
385 expect(res.body.total).to.equal(1)
386 expect(res.body.data).to.have.lengthOf(1)
388 const plugin: PeerTubePlugin = res.body.data[0]
389 expect(plugin.name).to.equal('hello-world')
390 expect(plugin.enabled).to.be.false
391 expect(plugin.uninstalled).to.be.true
394 it('Should uninstall the theme', async function () {
395 await uninstallPlugin({
397 accessToken: server.accessToken,
398 npmName: 'peertube-theme-background-red'
402 it('Should have updated the configuration', async function () {
403 // get /config (default theme + registered themes + registered plugins)
404 const res = await getConfig(server.url)
405 const config: ServerConfig = res.body
407 expect(config.theme.default).to.equal('default')
409 const theme = config.theme.registered.find(r => r.name === 'background-red')
410 expect(theme).to.be.undefined
412 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
413 expect(plugin).to.be.undefined
416 it('Should have updated the user theme', async function () {
417 const res = await getMyUserInformation(server.url, server.accessToken)
418 expect((res.body as User).theme).to.equal('instance-default')
421 after(async function () {
422 await closeAllSequelize([ server ])
423 await cleanupTests([ server ])