1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
22 setAccessTokensToServers,
24 testHelloWorldRegisteredSettings,
26 updateCustomSubConfig,
29 updatePluginPackageJSON,
33 } from '@shared/extra-utils'
34 import { PeerTubePlugin, PeerTubePluginIndex, PluginPackageJson, PluginType, PublicServerSetting, ServerConfig, User } from '@shared/models'
36 const expect = chai.expect
38 describe('Test plugins', function () {
39 let server: ServerInfo = null
41 before(async function () {
44 const configOverride = {
46 index: { check_latest_versions_interval: '5 seconds' }
49 server = await flushAndRunServer(1, configOverride)
50 await setAccessTokensToServers([ server ])
53 it('Should list and search available plugins and themes', async function () {
57 const res = await listAvailablePlugins({
59 accessToken: server.accessToken,
62 pluginType: PluginType.THEME,
63 search: 'background-red'
66 expect(res.body.total).to.be.at.least(1)
67 expect(res.body.data).to.have.lengthOf(1)
71 const res1 = await listAvailablePlugins({
73 accessToken: server.accessToken,
78 const data1: PeerTubePluginIndex[] = res1.body.data
80 expect(res1.body.total).to.be.at.least(2)
81 expect(data1).to.have.lengthOf(2)
83 const res2 = await listAvailablePlugins({
85 accessToken: server.accessToken,
90 const data2: PeerTubePluginIndex[] = res2.body.data
92 expect(res2.body.total).to.be.at.least(2)
93 expect(data2).to.have.lengthOf(2)
95 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
99 const res = await listAvailablePlugins({
101 accessToken: server.accessToken,
104 pluginType: PluginType.THEME,
105 search: 'background-red',
106 currentPeerTubeEngine: '1.0.0'
108 const data: PeerTubePluginIndex[] = res.body.data
110 const p = data.find(p => p.npmName === 'peertube-theme-background-red')
111 expect(p).to.be.undefined
115 it('Should install a plugin and a theme', async function () {
118 await installPlugin({
120 accessToken: server.accessToken,
121 npmName: 'peertube-plugin-hello-world'
124 await installPlugin({
126 accessToken: server.accessToken,
127 npmName: 'peertube-theme-background-red'
131 it('Should have the plugin loaded in the configuration', async function () {
132 const res = await getConfig(server.url)
133 const config: ServerConfig = res.body
135 const theme = config.theme.registered.find(r => r.name === 'background-red')
136 expect(theme).to.not.be.undefined
138 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
139 expect(plugin).to.not.be.undefined
142 it('Should update the default theme in the configuration', async function () {
143 await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })
145 const res = await getConfig(server.url)
146 const config: ServerConfig = res.body
148 expect(config.theme.default).to.equal('background-red')
151 it('Should update my default theme', async function () {
154 accessToken: server.accessToken,
155 theme: 'background-red'
158 const res = await getMyUserInformation(server.url, server.accessToken)
159 expect((res.body as User).theme).to.equal('background-red')
162 it('Should list plugins and themes', async function () {
164 const res = await listPlugins({
166 accessToken: server.accessToken,
169 pluginType: PluginType.THEME
171 const data: PeerTubePlugin[] = res.body.data
173 expect(res.body.total).to.be.at.least(1)
174 expect(data).to.have.lengthOf(1)
175 expect(data[0].name).to.equal('background-red')
179 const res = await listPlugins({
181 accessToken: server.accessToken,
186 const data: PeerTubePlugin[] = res.body.data
188 expect(data[0].name).to.equal('background-red')
189 expect(data[1].name).to.equal('hello-world')
193 const res = await listPlugins({
195 accessToken: server.accessToken,
200 const data: PeerTubePlugin[] = res.body.data
202 expect(data[0].name).to.equal('hello-world')
206 it('Should get registered settings', async function () {
207 await testHelloWorldRegisteredSettings(server)
210 it('Should get public settings', async function () {
211 const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
213 const publicSettings = (res.body as PublicServerSetting).publicSettings
215 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
216 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
217 expect(publicSettings['user-name']).to.be.null
220 it('Should update the settings', async function () {
225 await updatePluginSettings({
227 accessToken: server.accessToken,
228 npmName: 'peertube-plugin-hello-world',
233 it('Should have watched settings changes', async function () {
236 await waitUntilLog(server, 'Settings changed!')
239 it('Should get a plugin and a theme', async function () {
241 const res = await getPlugin({
243 accessToken: server.accessToken,
244 npmName: 'peertube-plugin-hello-world'
247 const plugin: PeerTubePlugin = res.body
249 expect(plugin.type).to.equal(PluginType.PLUGIN)
250 expect(plugin.name).to.equal('hello-world')
251 expect(plugin.description).to.exist
252 expect(plugin.homepage).to.exist
253 expect(plugin.uninstalled).to.be.false
254 expect(plugin.enabled).to.be.true
255 expect(plugin.description).to.exist
256 expect(plugin.version).to.exist
257 expect(plugin.peertubeEngine).to.exist
258 expect(plugin.createdAt).to.exist
260 expect(plugin.settings).to.not.be.undefined
261 expect(plugin.settings['admin-name']).to.equal('Cid')
265 const res = await getPlugin({
267 accessToken: server.accessToken,
268 npmName: 'peertube-theme-background-red'
271 const plugin: PeerTubePlugin = res.body
273 expect(plugin.type).to.equal(PluginType.THEME)
274 expect(plugin.name).to.equal('background-red')
275 expect(plugin.description).to.exist
276 expect(plugin.homepage).to.exist
277 expect(plugin.uninstalled).to.be.false
278 expect(plugin.enabled).to.be.true
279 expect(plugin.description).to.exist
280 expect(plugin.version).to.exist
281 expect(plugin.peertubeEngine).to.exist
282 expect(plugin.createdAt).to.exist
284 expect(plugin.settings).to.be.null
288 it('Should update the plugin and the theme', async function () {
291 // Wait the scheduler that get the latest plugins versions
294 // Fake update our plugin version
295 await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')
297 // Fake update package.json
298 const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
299 const oldVersion = packageJSON.version
301 packageJSON.version = '0.0.1'
302 await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)
304 // Restart the server to take into account this change
305 killallServers([ server ])
306 await reRunServer(server)
309 const res = await listPlugins({
311 accessToken: server.accessToken,
312 pluginType: PluginType.PLUGIN
315 const plugin: PeerTubePlugin = res.body.data[0]
317 expect(plugin.version).to.equal('0.0.1')
318 expect(plugin.latestVersion).to.exist
319 expect(plugin.latestVersion).to.not.equal('0.0.1')
325 accessToken: server.accessToken,
326 npmName: 'peertube-plugin-hello-world'
329 const res = await listPlugins({
331 accessToken: server.accessToken,
332 pluginType: PluginType.PLUGIN
335 const plugin: PeerTubePlugin = res.body.data[0]
337 expect(plugin.version).to.equal(oldVersion)
339 const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
340 expect(updatedPackageJSON.version).to.equal(oldVersion)
344 it('Should uninstall the plugin', async function () {
345 await uninstallPlugin({
347 accessToken: server.accessToken,
348 npmName: 'peertube-plugin-hello-world'
351 const res = await listPlugins({
353 accessToken: server.accessToken,
354 pluginType: PluginType.PLUGIN
357 expect(res.body.total).to.equal(0)
358 expect(res.body.data).to.have.lengthOf(0)
361 it('Should list uninstalled plugins', async function () {
362 const res = await listPlugins({
364 accessToken: server.accessToken,
365 pluginType: PluginType.PLUGIN,
369 expect(res.body.total).to.equal(1)
370 expect(res.body.data).to.have.lengthOf(1)
372 const plugin: PeerTubePlugin = res.body.data[0]
373 expect(plugin.name).to.equal('hello-world')
374 expect(plugin.enabled).to.be.false
375 expect(plugin.uninstalled).to.be.true
378 it('Should uninstall the theme', async function () {
379 await uninstallPlugin({
381 accessToken: server.accessToken,
382 npmName: 'peertube-theme-background-red'
386 it('Should have updated the configuration', async function () {
387 // get /config (default theme + registered themes + registered plugins)
388 const res = await getConfig(server.url)
389 const config: ServerConfig = res.body
391 expect(config.theme.default).to.equal('default')
393 const theme = config.theme.registered.find(r => r.name === 'background-red')
394 expect(theme).to.be.undefined
396 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
397 expect(plugin).to.be.undefined
400 it('Should have updated the user theme', async function () {
401 const res = await getMyUserInformation(server.url, server.accessToken)
402 expect((res.body as User).theme).to.equal('instance-default')
405 it('Should not install a broken plugin', async function () {
408 async function check () {
409 const res = await listPlugins({
411 accessToken: server.accessToken,
412 pluginType: PluginType.PLUGIN
415 const plugins: PeerTubePlugin[] = res.body.data
417 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
420 await installPlugin({
422 accessToken: server.accessToken,
423 path: getPluginTestPath('-broken'),
424 expectedStatus: HttpStatusCode.BAD_REQUEST_400
429 killallServers([ server ])
430 await reRunServer(server)
435 after(async function () {
436 await closeAllSequelize([ server ])
437 await cleanupTests([ server ])