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 { PeerTubePlugin, PeerTubePluginIndex, PluginPackageJson, PluginType, PublicServerSetting, ServerConfig, User } from '@shared/models'
34 const expect = chai.expect
36 describe('Test plugins', function () {
37 let server: ServerInfo = null
39 before(async function () {
42 const configOverride = {
44 index: { check_latest_versions_interval: '5 seconds' }
47 server = await flushAndRunServer(1, configOverride)
48 await setAccessTokensToServers([ server ])
51 it('Should list and search available plugins and themes', async function () {
55 const res = await listAvailablePlugins({
57 accessToken: server.accessToken,
60 pluginType: PluginType.THEME,
61 search: 'background-red'
64 expect(res.body.total).to.be.at.least(1)
65 expect(res.body.data).to.have.lengthOf(1)
69 const res1 = await listAvailablePlugins({
71 accessToken: server.accessToken,
76 const data1: PeerTubePluginIndex[] = res1.body.data
78 expect(res1.body.total).to.be.at.least(2)
79 expect(data1).to.have.lengthOf(2)
81 const res2 = await listAvailablePlugins({
83 accessToken: server.accessToken,
88 const data2: PeerTubePluginIndex[] = res2.body.data
90 expect(res2.body.total).to.be.at.least(2)
91 expect(data2).to.have.lengthOf(2)
93 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
97 const res = await listAvailablePlugins({
99 accessToken: server.accessToken,
102 pluginType: PluginType.THEME,
103 search: 'background-red',
104 currentPeerTubeEngine: '1.0.0'
106 const data: PeerTubePluginIndex[] = res.body.data
108 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
109 expect(p).to.be.undefined
113 it('Should install a plugin and a theme', async function () {
116 await installPlugin({
118 accessToken: server.accessToken,
119 npmName: 'peertube-plugin-hello-world'
122 await installPlugin({
124 accessToken: server.accessToken,
125 npmName: 'peertube-theme-background-red'
129 it('Should have the plugin loaded in the configuration', async function () {
130 const res = await getConfig(server.url)
131 const config: ServerConfig = res.body
133 const theme = config.theme.registered.find(r => r.name === 'background-red')
134 expect(theme).to.not.be.undefined
136 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
137 expect(plugin).to.not.be.undefined
140 it('Should update the default theme in the configuration', async function () {
141 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
143 const res = await getConfig(server.url)
144 const config: ServerConfig = res.body
146 expect(config.theme.default).to.equal('background-red')
149 it('Should update my default theme', async function () {
152 accessToken: server.accessToken,
153 theme: 'background-red'
156 const res = await getMyUserInformation(server.url, server.accessToken)
157 expect((res.body as User).theme).to.equal('background-red')
160 it('Should list plugins and themes', async function () {
162 const res = await listPlugins({
164 accessToken: server.accessToken,
167 pluginType: PluginType.THEME
169 const data: PeerTubePlugin[] = res.body.data
171 expect(res.body.total).to.be.at.least(1)
172 expect(data).to.have.lengthOf(1)
173 expect(data[0].name).to.equal('background-red')
177 const res = await listPlugins({
179 accessToken: server.accessToken,
184 const data: PeerTubePlugin[] = res.body.data
186 expect(data[0].name).to.equal('background-red')
187 expect(data[1].name).to.equal('hello-world')
191 const res = await listPlugins({
193 accessToken: server.accessToken,
198 const data: PeerTubePlugin[] = res.body.data
200 expect(data[0].name).to.equal('hello-world')
204 it('Should get registered settings', async function () {
205 await testHelloWorldRegisteredSettings(server)
208 it('Should get public settings', async function () {
209 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
211 const publicSettings = (res.body as PublicServerSetting).publicSettings
213 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
214 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
215 expect(publicSettings['user-name']).to.be.null
218 it('Should update the settings', async function () {
223 await updatePluginSettings({
225 accessToken: server.accessToken,
226 npmName: 'peertube-plugin-hello-world',
231 it('Should have watched settings changes', async function () {
234 await waitUntilLog(server, 'Settings changed!')
237 it('Should get a plugin and a theme', async function () {
239 const res = await getPlugin({
241 accessToken: server.accessToken,
242 npmName: 'peertube-plugin-hello-world'
245 const plugin: PeerTubePlugin = res.body
247 expect(plugin.type).to.equal(PluginType.PLUGIN)
248 expect(plugin.name).to.equal('hello-world')
249 expect(plugin.description).to.exist
250 expect(plugin.homepage).to.exist
251 expect(plugin.uninstalled).to.be.false
252 expect(plugin.enabled).to.be.true
253 expect(plugin.description).to.exist
254 expect(plugin.version).to.exist
255 expect(plugin.peertubeEngine).to.exist
256 expect(plugin.createdAt).to.exist
258 expect(plugin.settings).to.not.be.undefined
259 expect(plugin.settings['admin-name']).to.equal('Cid')
263 const res = await getPlugin({
265 accessToken: server.accessToken,
266 npmName: 'peertube-theme-background-red'
269 const plugin: PeerTubePlugin = res.body
271 expect(plugin.type).to.equal(PluginType.THEME)
272 expect(plugin.name).to.equal('background-red')
273 expect(plugin.description).to.exist
274 expect(plugin.homepage).to.exist
275 expect(plugin.uninstalled).to.be.false
276 expect(plugin.enabled).to.be.true
277 expect(plugin.description).to.exist
278 expect(plugin.version).to.exist
279 expect(plugin.peertubeEngine).to.exist
280 expect(plugin.createdAt).to.exist
282 expect(plugin.settings).to.be.null
286 it('Should update the plugin and the theme', async function () {
289 // Wait the scheduler that get the latest plugins versions
292 // Fake update our plugin version
293 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
295 // Fake update package.json
296 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
297 const oldVersion = packageJSON.version
299 packageJSON.version = '0.0.1'
300 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
302 // Restart the server to take into account this change
303 killallServers([ server ])
304 await reRunServer(server)
307 const res = await listPlugins({
309 accessToken: server.accessToken,
310 pluginType: PluginType.PLUGIN
313 const plugin: PeerTubePlugin = res.body.data[0]
315 expect(plugin.version).to.equal('0.0.1')
316 expect(plugin.latestVersion).to.exist
317 expect(plugin.latestVersion).to.not.equal('0.0.1')
323 accessToken: server.accessToken,
324 npmName: 'peertube-plugin-hello-world'
327 const res = await listPlugins({
329 accessToken: server.accessToken,
330 pluginType: PluginType.PLUGIN
333 const plugin: PeerTubePlugin = res.body.data[0]
335 expect(plugin.version).to.equal(oldVersion)
337 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
338 expect(updatedPackageJSON.version).to.equal(oldVersion)
342 it('Should uninstall the plugin', async function () {
343 await uninstallPlugin({
345 accessToken: server.accessToken,
346 npmName: 'peertube-plugin-hello-world'
349 const res = await listPlugins({
351 accessToken: server.accessToken,
352 pluginType: PluginType.PLUGIN
355 expect(res.body.total).to.equal(0)
356 expect(res.body.data).to.have.lengthOf(0)
359 it('Should list uninstalled plugins', async function () {
360 const res = await listPlugins({
362 accessToken: server.accessToken,
363 pluginType: PluginType.PLUGIN,
367 expect(res.body.total).to.equal(1)
368 expect(res.body.data).to.have.lengthOf(1)
370 const plugin: PeerTubePlugin = res.body.data[0]
371 expect(plugin.name).to.equal('hello-world')
372 expect(plugin.enabled).to.be.false
373 expect(plugin.uninstalled).to.be.true
376 it('Should uninstall the theme', async function () {
377 await uninstallPlugin({
379 accessToken: server.accessToken,
380 npmName: 'peertube-theme-background-red'
384 it('Should have updated the configuration', async function () {
385 // get /config (default theme + registered themes + registered plugins)
386 const res = await getConfig(server.url)
387 const config: ServerConfig = res.body
389 expect(config.theme.default).to.equal('default')
391 const theme = config.theme.registered.find(r => r.name === 'background-red')
392 expect(theme).to.be.undefined
394 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
395 expect(plugin).to.be.undefined
398 it('Should have updated the user theme', async function () {
399 const res = await getMyUserInformation(server.url, server.accessToken)
400 expect((res.body as User).theme).to.equal('instance-default')
403 after(async function () {
404 await closeAllSequelize([ server ])
405 await cleanupTests([ server ])