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