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