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