]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/plugins.ts
Fix client html cache on theme update
[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 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
99 const theme = config.theme.registered.find(r => r.name === 'background-red')
100 expect(theme).to.not.be.undefined
101 expect(theme.npmName).to.equal('peertube-theme-background-red')
102
103 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
104 expect(plugin).to.not.be.undefined
105 expect(plugin.npmName).to.equal('peertube-plugin-hello-world')
106 }
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 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
117 expect(config.theme.default).to.equal('background-red')
118 }
119 })
120
121 it('Should update my default theme', async function () {
122 await server.users.updateMe({ theme: 'background-red' })
123
124 const user = await server.users.getMyInfo()
125 expect(user.theme).to.equal('background-red')
126 })
127
128 it('Should list plugins and themes', async function () {
129 {
130 const body = await command.list({
131 count: 1,
132 start: 0,
133 pluginType: PluginType.THEME
134 })
135 expect(body.total).to.be.at.least(1)
136
137 const data = body.data
138 expect(data).to.have.lengthOf(1)
139 expect(data[0].name).to.equal('background-red')
140 }
141
142 {
143 const { data } = await command.list({
144 count: 2,
145 start: 0,
146 sort: 'name'
147 })
148
149 expect(data[0].name).to.equal('background-red')
150 expect(data[1].name).to.equal('hello-world')
151 }
152
153 {
154 const body = await command.list({
155 count: 2,
156 start: 1,
157 sort: 'name'
158 })
159
160 expect(body.data[0].name).to.equal('hello-world')
161 }
162 })
163
164 it('Should get registered settings', async function () {
165 await testHelloWorldRegisteredSettings(server)
166 })
167
168 it('Should get public settings', async function () {
169 const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' })
170 const publicSettings = body.publicSettings
171
172 expect(Object.keys(publicSettings)).to.have.lengthOf(1)
173 expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
174 expect(publicSettings['user-name']).to.be.null
175 })
176
177 it('Should update the settings', async function () {
178 const settings = {
179 'admin-name': 'Cid'
180 }
181
182 await command.updateSettings({
183 npmName: 'peertube-plugin-hello-world',
184 settings
185 })
186 })
187
188 it('Should have watched settings changes', async function () {
189 this.timeout(10000)
190
191 await server.servers.waitUntilLog('Settings changed!')
192 })
193
194 it('Should get a plugin and a theme', async function () {
195 {
196 const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' })
197
198 expect(plugin.type).to.equal(PluginType.PLUGIN)
199 expect(plugin.name).to.equal('hello-world')
200 expect(plugin.description).to.exist
201 expect(plugin.homepage).to.exist
202 expect(plugin.uninstalled).to.be.false
203 expect(plugin.enabled).to.be.true
204 expect(plugin.description).to.exist
205 expect(plugin.version).to.exist
206 expect(plugin.peertubeEngine).to.exist
207 expect(plugin.createdAt).to.exist
208
209 expect(plugin.settings).to.not.be.undefined
210 expect(plugin.settings['admin-name']).to.equal('Cid')
211 }
212
213 {
214 const plugin = await command.get({ npmName: 'peertube-theme-background-red' })
215
216 expect(plugin.type).to.equal(PluginType.THEME)
217 expect(plugin.name).to.equal('background-red')
218 expect(plugin.description).to.exist
219 expect(plugin.homepage).to.exist
220 expect(plugin.uninstalled).to.be.false
221 expect(plugin.enabled).to.be.true
222 expect(plugin.description).to.exist
223 expect(plugin.version).to.exist
224 expect(plugin.peertubeEngine).to.exist
225 expect(plugin.createdAt).to.exist
226
227 expect(plugin.settings).to.be.null
228 }
229 })
230
231 it('Should update the plugin and the theme', async function () {
232 this.timeout(180000)
233
234 // Wait the scheduler that get the latest plugins versions
235 await wait(6000)
236
237 async function testUpdate (type: 'plugin' | 'theme', name: string) {
238 // Fake update our plugin version
239 await server.sql.setPluginVersion(name, '0.0.1')
240
241 // Fake update package.json
242 const packageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
243 const oldVersion = packageJSON.version
244
245 packageJSON.version = '0.0.1'
246 await command.updatePackageJSON(`peertube-${type}-${name}`, packageJSON)
247
248 // Restart the server to take into account this change
249 await killallServers([ server ])
250 await server.run()
251
252 const checkConfig = async (version: string) => {
253 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
254 expect(config[type].registered.find(r => r.name === name).version).to.equal(version)
255 }
256 }
257
258 const getPluginFromAPI = async () => {
259 const body = await command.list({ pluginType: type === 'plugin' ? PluginType.PLUGIN : PluginType.THEME })
260
261 return body.data.find(p => p.name === name)
262 }
263
264 {
265 const plugin = await getPluginFromAPI()
266 expect(plugin.version).to.equal('0.0.1')
267 expect(plugin.latestVersion).to.exist
268 expect(plugin.latestVersion).to.not.equal('0.0.1')
269
270 await checkConfig('0.0.1')
271 }
272
273 {
274 await command.update({ npmName: `peertube-${type}-${name}` })
275
276 const plugin = await getPluginFromAPI()
277 expect(plugin.version).to.equal(oldVersion)
278
279 const updatedPackageJSON = await command.getPackageJSON(`peertube-${type}-${name}`)
280 expect(updatedPackageJSON.version).to.equal(oldVersion)
281
282 await checkConfig(oldVersion)
283 }
284 }
285
286 await testUpdate('theme', 'background-red')
287 await testUpdate('plugin', 'hello-world')
288 })
289
290 it('Should uninstall the plugin', async function () {
291 await command.uninstall({ npmName: 'peertube-plugin-hello-world' })
292
293 const body = await command.list({ pluginType: PluginType.PLUGIN })
294 expect(body.total).to.equal(0)
295 expect(body.data).to.have.lengthOf(0)
296 })
297
298 it('Should list uninstalled plugins', async function () {
299 const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true })
300 expect(body.total).to.equal(1)
301 expect(body.data).to.have.lengthOf(1)
302
303 const plugin = body.data[0]
304 expect(plugin.name).to.equal('hello-world')
305 expect(plugin.enabled).to.be.false
306 expect(plugin.uninstalled).to.be.true
307 })
308
309 it('Should uninstall the theme', async function () {
310 await command.uninstall({ npmName: 'peertube-theme-background-red' })
311 })
312
313 it('Should have updated the configuration', async function () {
314 for (const config of [ await server.config.getConfig(), await server.config.getIndexHTMLConfig() ]) {
315 expect(config.theme.default).to.equal('default')
316
317 const theme = config.theme.registered.find(r => r.name === 'background-red')
318 expect(theme).to.be.undefined
319
320 const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
321 expect(plugin).to.be.undefined
322 }
323 })
324
325 it('Should have updated the user theme', async function () {
326 const user = await server.users.getMyInfo()
327 expect(user.theme).to.equal('instance-default')
328 })
329
330 it('Should not install a broken plugin', async function () {
331 this.timeout(60000)
332
333 async function check () {
334 const body = await command.list({ pluginType: PluginType.PLUGIN })
335 const plugins = body.data
336 expect(plugins.find(p => p.name === 'test-broken')).to.not.exist
337 }
338
339 await command.install({
340 path: PluginsCommand.getPluginTestPath('-broken'),
341 expectedStatus: HttpStatusCode.BAD_REQUEST_400
342 })
343
344 await check()
345
346 await killallServers([ server ])
347 await server.run()
348
349 await check()
350 })
351
352 after(async function () {
353 await cleanupTests([ server ])
354 })
355 })