]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/plugins.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / plugins.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
9b474844
C
2
3import 'mocha'
4import * as chai from 'chai'
c55e3d72
C
5import { testHelloWorldRegisteredSettings } from '@server/tests/shared'
6import { wait } from '@shared/core-utils'
7import { HttpStatusCode, PluginType } from '@shared/models'
9b474844
C
8import {
9 cleanupTests,
254d3579 10 createSingleServer,
a1587156 11 killallServers,
254d3579 12 PeerTubeServer,
4c7e60bc 13 PluginsCommand,
c55e3d72 14 setAccessTokensToServers
bf54587a 15} from '@shared/server-commands'
9b474844
C
16
17const expect = chai.expect
18
19describe('Test plugins', function () {
254d3579 20 let server: PeerTubeServer = null
ae2abfd3 21 let command: PluginsCommand
9b474844
C
22
23 before(async function () {
24 this.timeout(30000)
25
09071200
C
26 const configOverride = {
27 plugins: {
28 index: { check_latest_versions_interval: '5 seconds' }
29 }
30 }
254d3579 31 server = await createSingleServer(1, configOverride)
9b474844 32 await setAccessTokensToServers([ server ])
a5461888 33
89d241a7 34 command = server.plugins
09071200
C
35 })
36
37 it('Should list and search available plugins and themes', async function () {
38 this.timeout(30000)
9b474844
C
39
40 {
ae2abfd3 41 const body = await command.listAvailable({
09071200
C
42 count: 1,
43 start: 0,
44 pluginType: PluginType.THEME,
45 search: 'background-red'
46 })
47
ae2abfd3
C
48 expect(body.total).to.be.at.least(1)
49 expect(body.data).to.have.lengthOf(1)
9b474844
C
50 }
51
52 {
ae2abfd3 53 const body1 = await command.listAvailable({
09071200
C
54 count: 2,
55 start: 0,
56 sort: 'npmName'
57 })
ae2abfd3 58 expect(body1.total).to.be.at.least(2)
09071200 59
ae2abfd3 60 const data1 = body1.data
09071200
C
61 expect(data1).to.have.lengthOf(2)
62
ae2abfd3 63 const body2 = await command.listAvailable({
09071200
C
64 count: 2,
65 start: 0,
66 sort: '-npmName'
67 })
ae2abfd3 68 expect(body2.total).to.be.at.least(2)
09071200 69
ae2abfd3 70 const data2 = body2.data
09071200
C
71 expect(data2).to.have.lengthOf(2)
72
a1587156 73 expect(data1[0].npmName).to.not.equal(data2[0].npmName)
9b474844 74 }
9b474844 75
09071200 76 {
ae2abfd3 77 const body = await command.listAvailable({
09071200
C
78 count: 10,
79 start: 0,
80 pluginType: PluginType.THEME,
81 search: 'background-red',
82 currentPeerTubeEngine: '1.0.0'
83 })
9b474844 84
ae2abfd3 85 const p = body.data.find(p => p.npmName === 'peertube-theme-background-red')
09071200
C
86 expect(p).to.be.undefined
87 }
9b474844
C
88 })
89
9b474844 90 it('Should install a plugin and a theme', async function () {
09071200
C
91 this.timeout(30000)
92
ae2abfd3
C
93 await command.install({ npmName: 'peertube-plugin-hello-world' })
94 await command.install({ npmName: 'peertube-theme-background-red' })
9b474844
C
95 })
96
9b474844 97 it('Should have the plugin loaded in the configuration', async function () {
89d241a7 98 const config = await server.config.getConfig()
09071200
C
99
100 const theme = config.theme.registered.find(r => r.name === 'background-red')
101 expect(theme).to.not.be.undefined
102
103 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
104 expect(plugin).to.not.be.undefined
9b474844
C
105 })
106
107 it('Should update the default theme in the configuration', async function () {
89d241a7 108 await server.config.updateCustomSubConfig({
65e6e260
C
109 newConfig: {
110 theme: { default: 'background-red' }
111 }
112 })
09071200 113
89d241a7 114 const config = await server.config.getConfig()
09071200 115 expect(config.theme.default).to.equal('background-red')
9b474844
C
116 })
117
09071200 118 it('Should update my default theme', async function () {
89d241a7 119 await server.users.updateMe({ theme: 'background-red' })
09071200 120
89d241a7 121 const user = await server.users.getMyInfo()
7926c5f9 122 expect(user.theme).to.equal('background-red')
9b474844
C
123 })
124
09071200
C
125 it('Should list plugins and themes', async function () {
126 {
ae2abfd3 127 const body = await command.list({
09071200
C
128 count: 1,
129 start: 0,
130 pluginType: PluginType.THEME
131 })
ae2abfd3 132 expect(body.total).to.be.at.least(1)
09071200 133
ae2abfd3 134 const data = body.data
09071200
C
135 expect(data).to.have.lengthOf(1)
136 expect(data[0].name).to.equal('background-red')
137 }
138
139 {
dd0ebb71 140 const { data } = await command.list({
09071200
C
141 count: 2,
142 start: 0,
143 sort: 'name'
144 })
09071200
C
145
146 expect(data[0].name).to.equal('background-red')
147 expect(data[1].name).to.equal('hello-world')
148 }
149
150 {
ae2abfd3 151 const body = await command.list({
09071200
C
152 count: 2,
153 start: 1,
154 sort: 'name'
155 })
09071200 156
ae2abfd3 157 expect(body.data[0].name).to.equal('hello-world')
09071200 158 }
9b474844
C
159 })
160
161 it('Should get registered settings', async function () {
353f8bc0 162 await testHelloWorldRegisteredSettings(server)
9b474844
C
163 })
164
ba211e73 165 it('Should get public settings', async function () {
ae2abfd3
C
166 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
167 const publicSettings = body.publicSettings
ba211e73
C
168
169 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
170 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
171 expect(publicSettings['user-name']).to.be.null
172 })
173
9b474844 174 it('Should update the settings', async function () {
09071200
C
175 const settings = {
176 'admin-name': 'Cid'
177 }
178
ae2abfd3 179 await command.updateSettings({
09071200
C
180 npmName: 'peertube-plugin-hello-world',
181 settings
182 })
183 })
184
a5896799
C
185 it('Should have watched settings changes', async function () {
186 this.timeout(10000)
187
89d241a7 188 await server.servers.waitUntilLog('Settings changed!')
a5896799
C
189 })
190
09071200
C
191 it('Should get a plugin and a theme', async function () {
192 {
ae2abfd3 193 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
09071200
C
194
195 expect(plugin.type).to.equal(PluginType.PLUGIN)
196 expect(plugin.name).to.equal('hello-world')
197 expect(plugin.description).to.exist
198 expect(plugin.homepage).to.exist
199 expect(plugin.uninstalled).to.be.false
200 expect(plugin.enabled).to.be.true
201 expect(plugin.description).to.exist
202 expect(plugin.version).to.exist
203 expect(plugin.peertubeEngine).to.exist
204 expect(plugin.createdAt).to.exist
205
206 expect(plugin.settings).to.not.be.undefined
207 expect(plugin.settings['admin-name']).to.equal('Cid')
208 }
209
210 {
ae2abfd3 211 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
9b474844 212
09071200
C
213 expect(plugin.type).to.equal(PluginType.THEME)
214 expect(plugin.name).to.equal('background-red')
215 expect(plugin.description).to.exist
216 expect(plugin.homepage).to.exist
217 expect(plugin.uninstalled).to.be.false
218 expect(plugin.enabled).to.be.true
219 expect(plugin.description).to.exist
220 expect(plugin.version).to.exist
221 expect(plugin.peertubeEngine).to.exist
222 expect(plugin.createdAt).to.exist
223
224 expect(plugin.settings).to.be.null
225 }
9b474844
C
226 })
227
228 it('Should update the plugin and the theme', async function () {
31a91119 229 this.timeout(90000)
09071200
C
230
231 // Wait the scheduler that get the latest plugins versions
232 await wait(6000)
233
234 // Fake update our plugin version
89d241a7 235 await server.sql.setPluginVersion('hello-world', '0.0.1')
09071200
C
236
237 // Fake update package.json
ae2abfd3 238 const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
09071200
C
239 const oldVersion = packageJSON.version
240
241 packageJSON.version = '0.0.1'
ae2abfd3 242 await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON)
09071200
C
243
244 // Restart the server to take into account this change
9293139f 245 await killallServers([ server ])
254d3579 246 await server.run()
09071200
C
247
248 {
ae2abfd3 249 const body = await command.list({ pluginType: PluginType.PLUGIN })
09071200 250
ae2abfd3 251 const plugin = body.data[0]
09071200
C
252 expect(plugin.version).to.equal('0.0.1')
253 expect(plugin.latestVersion).to.exist
254 expect(plugin.latestVersion).to.not.equal('0.0.1')
255 }
256
257 {
ae2abfd3 258 await command.update({ npmName: 'peertube-plugin-hello-world' })
09071200 259
ae2abfd3 260 const body = await command.list({ pluginType: PluginType.PLUGIN })
09071200 261
ae2abfd3 262 const plugin = body.data[0]
09071200
C
263 expect(plugin.version).to.equal(oldVersion)
264
ae2abfd3 265 const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world')
09071200
C
266 expect(updatedPackageJSON.version).to.equal(oldVersion)
267 }
9b474844
C
268 })
269
270 it('Should uninstall the plugin', async function () {
ae2abfd3 271 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
09071200 272
ae2abfd3
C
273 const body = await command.list({ pluginType: PluginType.PLUGIN })
274 expect(body.total).to.equal(0)
275 expect(body.data).to.have.lengthOf(0)
9b474844
C
276 })
277
9b474844 278 it('Should list uninstalled plugins', async function () {
ae2abfd3
C
279 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
280 expect(body.total).to.equal(1)
281 expect(body.data).to.have.lengthOf(1)
09071200 282
ae2abfd3 283 const plugin = body.data[0]
09071200
C
284 expect(plugin.name).to.equal('hello-world')
285 expect(plugin.enabled).to.be.false
286 expect(plugin.uninstalled).to.be.true
9b474844
C
287 })
288
289 it('Should uninstall the theme', async function () {
ae2abfd3 290 await command.uninstall({ npmName: 'peertube-theme-background-red' })
9b474844
C
291 })
292
293 it('Should have updated the configuration', async function () {
89d241a7 294 const config = await server.config.getConfig()
09071200
C
295
296 expect(config.theme.default).to.equal('default')
297
298 const theme = config.theme.registered.find(r => r.name === 'background-red')
299 expect(theme).to.be.undefined
300
301 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
302 expect(plugin).to.be.undefined
303 })
304
305 it('Should have updated the user theme', async function () {
89d241a7 306 const user = await server.users.getMyInfo()
7926c5f9 307 expect(user.theme).to.equal('instance-default')
9b474844
C
308 })
309
9c2e051c
C
310 it('Should not install a broken plugin', async function () {
311 this.timeout(60000)
312
313 async function check () {
ae2abfd3
C
314 const body = await command.list({ pluginType: PluginType.PLUGIN })
315 const plugins = body.data
9c2e051c
C
316 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
317 }
318
ae2abfd3
C
319 await command.install({
320 path: PluginsCommand.getPluginTestPath('-broken'),
9c2e051c
C
321 expectedStatus: HttpStatusCode.BAD_REQUEST_400
322 })
323
324 await check()
325
9293139f 326 await killallServers([ server ])
254d3579 327 await server.run()
9c2e051c
C
328
329 await check()
330 })
331
9b474844
C
332 after(async function () {
333 await cleanupTests([ server ])
334 })
335})