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