]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { | |
6 | cleanupTests, | |
7 | createSingleServer, | |
8 | killallServers, | |
9 | PeerTubeServer, | |
10 | PluginsCommand, | |
11 | setAccessTokensToServers, | |
12 | testHelloWorldRegisteredSettings, | |
13 | wait | |
14 | } from '@shared/extra-utils' | |
15 | import { HttpStatusCode, PluginType } from '@shared/models' | |
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 | ||
103 | const plugin = config.plugin.registered.find(r => r.name === 'hello-world') | |
104 | expect(plugin).to.not.be.undefined | |
105 | }) | |
106 | ||
107 | it('Should update the default theme in the configuration', async function () { | |
108 | await server.config.updateCustomSubConfig({ | |
109 | newConfig: { | |
110 | theme: { default: 'background-red' } | |
111 | } | |
112 | }) | |
113 | ||
114 | const config = await server.config.getConfig() | |
115 | expect(config.theme.default).to.equal('background-red') | |
116 | }) | |
117 | ||
118 | it('Should update my default theme', async function () { | |
119 | await server.users.updateMe({ theme: 'background-red' }) | |
120 | ||
121 | const user = await server.users.getMyInfo() | |
122 | expect(user.theme).to.equal('background-red') | |
123 | }) | |
124 | ||
125 | it('Should list plugins and themes', async function () { | |
126 | { | |
127 | const body = await command.list({ | |
128 | count: 1, | |
129 | start: 0, | |
130 | pluginType: PluginType.THEME | |
131 | }) | |
132 | expect(body.total).to.be.at.least(1) | |
133 | ||
134 | const data = body.data | |
135 | expect(data).to.have.lengthOf(1) | |
136 | expect(data[0].name).to.equal('background-red') | |
137 | } | |
138 | ||
139 | { | |
140 | const { data } = await command.list({ | |
141 | count: 2, | |
142 | start: 0, | |
143 | sort: 'name' | |
144 | }) | |
145 | ||
146 | expect(data[0].name).to.equal('background-red') | |
147 | expect(data[1].name).to.equal('hello-world') | |
148 | } | |
149 | ||
150 | { | |
151 | const body = await command.list({ | |
152 | count: 2, | |
153 | start: 1, | |
154 | sort: 'name' | |
155 | }) | |
156 | ||
157 | expect(body.data[0].name).to.equal('hello-world') | |
158 | } | |
159 | }) | |
160 | ||
161 | it('Should get registered settings', async function () { | |
162 | await testHelloWorldRegisteredSettings(server) | |
163 | }) | |
164 | ||
165 | it('Should get public settings', async function () { | |
166 | const body = await command.getPublicSettings({ npmName: 'peertube-plugin-hello-world' }) | |
167 | const publicSettings = body.publicSettings | |
168 | ||
169 | expect(Object.keys(publicSettings)).to.have.lengthOf(1) | |
170 | expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ]) | |
171 | expect(publicSettings['user-name']).to.be.null | |
172 | }) | |
173 | ||
174 | it('Should update the settings', async function () { | |
175 | const settings = { | |
176 | 'admin-name': 'Cid' | |
177 | } | |
178 | ||
179 | await command.updateSettings({ | |
180 | npmName: 'peertube-plugin-hello-world', | |
181 | settings | |
182 | }) | |
183 | }) | |
184 | ||
185 | it('Should have watched settings changes', async function () { | |
186 | this.timeout(10000) | |
187 | ||
188 | await server.servers.waitUntilLog('Settings changed!') | |
189 | }) | |
190 | ||
191 | it('Should get a plugin and a theme', async function () { | |
192 | { | |
193 | const plugin = await command.get({ npmName: 'peertube-plugin-hello-world' }) | |
194 | ||
195 | expect(plugin.type).to.equal(PluginType.PLUGIN) | |
196 | expect(plugin.name).to.equal('hello-world') | |
197 | expect(plugin.description).to.exist | |
198 | expect(plugin.homepage).to.exist | |
199 | expect(plugin.uninstalled).to.be.false | |
200 | expect(plugin.enabled).to.be.true | |
201 | expect(plugin.description).to.exist | |
202 | expect(plugin.version).to.exist | |
203 | expect(plugin.peertubeEngine).to.exist | |
204 | expect(plugin.createdAt).to.exist | |
205 | ||
206 | expect(plugin.settings).to.not.be.undefined | |
207 | expect(plugin.settings['admin-name']).to.equal('Cid') | |
208 | } | |
209 | ||
210 | { | |
211 | const plugin = await command.get({ npmName: 'peertube-theme-background-red' }) | |
212 | ||
213 | expect(plugin.type).to.equal(PluginType.THEME) | |
214 | expect(plugin.name).to.equal('background-red') | |
215 | expect(plugin.description).to.exist | |
216 | expect(plugin.homepage).to.exist | |
217 | expect(plugin.uninstalled).to.be.false | |
218 | expect(plugin.enabled).to.be.true | |
219 | expect(plugin.description).to.exist | |
220 | expect(plugin.version).to.exist | |
221 | expect(plugin.peertubeEngine).to.exist | |
222 | expect(plugin.createdAt).to.exist | |
223 | ||
224 | expect(plugin.settings).to.be.null | |
225 | } | |
226 | }) | |
227 | ||
228 | it('Should update the plugin and the theme', async function () { | |
229 | this.timeout(90000) | |
230 | ||
231 | // Wait the scheduler that get the latest plugins versions | |
232 | await wait(6000) | |
233 | ||
234 | // Fake update our plugin version | |
235 | await server.sql.setPluginVersion('hello-world', '0.0.1') | |
236 | ||
237 | // Fake update package.json | |
238 | const packageJSON = await command.getPackageJSON('peertube-plugin-hello-world') | |
239 | const oldVersion = packageJSON.version | |
240 | ||
241 | packageJSON.version = '0.0.1' | |
242 | await command.updatePackageJSON('peertube-plugin-hello-world', packageJSON) | |
243 | ||
244 | // Restart the server to take into account this change | |
245 | await killallServers([ server ]) | |
246 | await server.run() | |
247 | ||
248 | { | |
249 | const body = await command.list({ pluginType: PluginType.PLUGIN }) | |
250 | ||
251 | const plugin = body.data[0] | |
252 | expect(plugin.version).to.equal('0.0.1') | |
253 | expect(plugin.latestVersion).to.exist | |
254 | expect(plugin.latestVersion).to.not.equal('0.0.1') | |
255 | } | |
256 | ||
257 | { | |
258 | await command.update({ npmName: 'peertube-plugin-hello-world' }) | |
259 | ||
260 | const body = await command.list({ pluginType: PluginType.PLUGIN }) | |
261 | ||
262 | const plugin = body.data[0] | |
263 | expect(plugin.version).to.equal(oldVersion) | |
264 | ||
265 | const updatedPackageJSON = await command.getPackageJSON('peertube-plugin-hello-world') | |
266 | expect(updatedPackageJSON.version).to.equal(oldVersion) | |
267 | } | |
268 | }) | |
269 | ||
270 | it('Should uninstall the plugin', async function () { | |
271 | await command.uninstall({ npmName: 'peertube-plugin-hello-world' }) | |
272 | ||
273 | const body = await command.list({ pluginType: PluginType.PLUGIN }) | |
274 | expect(body.total).to.equal(0) | |
275 | expect(body.data).to.have.lengthOf(0) | |
276 | }) | |
277 | ||
278 | it('Should list uninstalled plugins', async function () { | |
279 | const body = await command.list({ pluginType: PluginType.PLUGIN, uninstalled: true }) | |
280 | expect(body.total).to.equal(1) | |
281 | expect(body.data).to.have.lengthOf(1) | |
282 | ||
283 | const plugin = body.data[0] | |
284 | expect(plugin.name).to.equal('hello-world') | |
285 | expect(plugin.enabled).to.be.false | |
286 | expect(plugin.uninstalled).to.be.true | |
287 | }) | |
288 | ||
289 | it('Should uninstall the theme', async function () { | |
290 | await command.uninstall({ npmName: 'peertube-theme-background-red' }) | |
291 | }) | |
292 | ||
293 | it('Should have updated the configuration', async function () { | |
294 | const config = await server.config.getConfig() | |
295 | ||
296 | expect(config.theme.default).to.equal('default') | |
297 | ||
298 | const theme = config.theme.registered.find(r => r.name === 'background-red') | |
299 | expect(theme).to.be.undefined | |
300 | ||
301 | const plugin = config.plugin.registered.find(r => r.name === 'hello-world') | |
302 | expect(plugin).to.be.undefined | |
303 | }) | |
304 | ||
305 | it('Should have updated the user theme', async function () { | |
306 | const user = await server.users.getMyInfo() | |
307 | expect(user.theme).to.equal('instance-default') | |
308 | }) | |
309 | ||
310 | it('Should not install a broken plugin', async function () { | |
311 | this.timeout(60000) | |
312 | ||
313 | async function check () { | |
314 | const body = await command.list({ pluginType: PluginType.PLUGIN }) | |
315 | const plugins = body.data | |
316 | expect(plugins.find(p => p.name === 'test-broken')).to.not.exist | |
317 | } | |
318 | ||
319 | await command.install({ | |
320 | path: PluginsCommand.getPluginTestPath('-broken'), | |
321 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
322 | }) | |
323 | ||
324 | await check() | |
325 | ||
326 | await killallServers([ server ]) | |
327 | await server.run() | |
328 | ||
329 | await check() | |
330 | }) | |
331 | ||
332 | after(async function () { | |
333 | await cleanupTests([ server ]) | |
334 | }) | |
335 | }) |