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