aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/server/plugins.ts
blob: 3c09acc9a5ece9607f378673ad8a8374903a1373 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import {
  cleanupTests,
  closeAllSequelize,
  flushAndRunServer,
  getConfig,
  getMyUserInformation,
  getPlugin,
  getPluginPackageJSON,
  getPublicSettings,
  installPlugin,
  killallServers,
  listAvailablePlugins,
  listPlugins,
  reRunServer,
  ServerInfo,
  setAccessTokensToServers,
  setPluginVersion,
  testHelloWorldRegisteredSettings,
  uninstallPlugin,
  updateCustomSubConfig,
  updateMyUser,
  updatePlugin,
  updatePluginPackageJSON,
  updatePluginSettings,
  wait,
  waitUntilLog
} from '@shared/extra-utils'
import { PeerTubePlugin, PeerTubePluginIndex, PluginPackageJson, PluginType, PublicServerSetting, ServerConfig, User } from '@shared/models'

const expect = chai.expect

describe('Test plugins', function () {
  let server: ServerInfo = null

  before(async function () {
    this.timeout(30000)

    const configOverride = {
      plugins: {
        index: { check_latest_versions_interval: '5 seconds' }
      }
    }
    server = await flushAndRunServer(1, configOverride)
    await setAccessTokensToServers([ server ])
  })

  it('Should list and search available plugins and themes', async function () {
    this.timeout(30000)

    {
      const res = await listAvailablePlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 1,
        start: 0,
        pluginType: PluginType.THEME,
        search: 'background-red'
      })

      expect(res.body.total).to.be.at.least(1)
      expect(res.body.data).to.have.lengthOf(1)
    }

    {
      const res1 = await listAvailablePlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 2,
        start: 0,
        sort: 'npmName'
      })
      const data1: PeerTubePluginIndex[] = res1.body.data

      expect(res1.body.total).to.be.at.least(2)
      expect(data1).to.have.lengthOf(2)

      const res2 = await listAvailablePlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 2,
        start: 0,
        sort: '-npmName'
      })
      const data2: PeerTubePluginIndex[] = res2.body.data

      expect(res2.body.total).to.be.at.least(2)
      expect(data2).to.have.lengthOf(2)

      expect(data1[0].npmName).to.not.equal(data2[0].npmName)
    }

    {
      const res = await listAvailablePlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 10,
        start: 0,
        pluginType: PluginType.THEME,
        search: 'background-red',
        currentPeerTubeEngine: '1.0.0'
      })
      const data: PeerTubePluginIndex[] = res.body.data

      const p = data.find(p => p.npmName === 'peertube-theme-background-red')
      expect(p).to.be.undefined
    }
  })

  it('Should install a plugin and a theme', async function () {
    this.timeout(30000)

    await installPlugin({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-plugin-hello-world'
    })

    await installPlugin({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-theme-background-red'
    })
  })

  it('Should have the plugin loaded in the configuration', async function () {
    const res = await getConfig(server.url)
    const config: ServerConfig = res.body

    const theme = config.theme.registered.find(r => r.name === 'background-red')
    expect(theme).to.not.be.undefined

    const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
    expect(plugin).to.not.be.undefined
  })

  it('Should update the default theme in the configuration', async function () {
    await updateCustomSubConfig(server.url, server.accessToken, { theme: { default: 'background-red' } })

    const res = await getConfig(server.url)
    const config: ServerConfig = res.body

    expect(config.theme.default).to.equal('background-red')
  })

  it('Should update my default theme', async function () {
    await updateMyUser({
      url: server.url,
      accessToken: server.accessToken,
      theme: 'background-red'
    })

    const res = await getMyUserInformation(server.url, server.accessToken)
    expect((res.body as User).theme).to.equal('background-red')
  })

  it('Should list plugins and themes', async function () {
    {
      const res = await listPlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 1,
        start: 0,
        pluginType: PluginType.THEME
      })
      const data: PeerTubePlugin[] = res.body.data

      expect(res.body.total).to.be.at.least(1)
      expect(data).to.have.lengthOf(1)
      expect(data[0].name).to.equal('background-red')
    }

    {
      const res = await listPlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 2,
        start: 0,
        sort: 'name'
      })
      const data: PeerTubePlugin[] = res.body.data

      expect(data[0].name).to.equal('background-red')
      expect(data[1].name).to.equal('hello-world')
    }

    {
      const res = await listPlugins({
        url: server.url,
        accessToken: server.accessToken,
        count: 2,
        start: 1,
        sort: 'name'
      })
      const data: PeerTubePlugin[] = res.body.data

      expect(data[0].name).to.equal('hello-world')
    }
  })

  it('Should get registered settings', async function () {
    await testHelloWorldRegisteredSettings(server)
  })

  it('Should get public settings', async function () {
    const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })

    const publicSettings = (res.body as PublicServerSetting).publicSettings

    expect(Object.keys(publicSettings)).to.have.lengthOf(1)
    expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
    expect(publicSettings['user-name']).to.be.null
  })

  it('Should update the settings', async function () {
    const settings = {
      'admin-name': 'Cid'
    }

    await updatePluginSettings({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-plugin-hello-world',
      settings
    })
  })

  it('Should have watched settings changes', async function () {
    this.timeout(10000)

    await waitUntilLog(server, 'Settings changed!')
  })

  it('Should get a plugin and a theme', async function () {
    {
      const res = await getPlugin({
        url: server.url,
        accessToken: server.accessToken,
        npmName: 'peertube-plugin-hello-world'
      })

      const plugin: PeerTubePlugin = res.body

      expect(plugin.type).to.equal(PluginType.PLUGIN)
      expect(plugin.name).to.equal('hello-world')
      expect(plugin.description).to.exist
      expect(plugin.homepage).to.exist
      expect(plugin.uninstalled).to.be.false
      expect(plugin.enabled).to.be.true
      expect(plugin.description).to.exist
      expect(plugin.version).to.exist
      expect(plugin.peertubeEngine).to.exist
      expect(plugin.createdAt).to.exist

      expect(plugin.settings).to.not.be.undefined
      expect(plugin.settings['admin-name']).to.equal('Cid')
    }

    {
      const res = await getPlugin({
        url: server.url,
        accessToken: server.accessToken,
        npmName: 'peertube-theme-background-red'
      })

      const plugin: PeerTubePlugin = res.body

      expect(plugin.type).to.equal(PluginType.THEME)
      expect(plugin.name).to.equal('background-red')
      expect(plugin.description).to.exist
      expect(plugin.homepage).to.exist
      expect(plugin.uninstalled).to.be.false
      expect(plugin.enabled).to.be.true
      expect(plugin.description).to.exist
      expect(plugin.version).to.exist
      expect(plugin.peertubeEngine).to.exist
      expect(plugin.createdAt).to.exist

      expect(plugin.settings).to.be.null
    }
  })

  it('Should update the plugin and the theme', async function () {
    this.timeout(30000)

    // Wait the scheduler that get the latest plugins versions
    await wait(6000)

    // Fake update our plugin version
    await setPluginVersion(server.internalServerNumber, 'hello-world', '0.0.1')

    // Fake update package.json
    const packageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
    const oldVersion = packageJSON.version

    packageJSON.version = '0.0.1'
    await updatePluginPackageJSON(server, 'peertube-plugin-hello-world', packageJSON)

    // Restart the server to take into account this change
    killallServers([ server ])
    await reRunServer(server)

    {
      const res = await listPlugins({
        url: server.url,
        accessToken: server.accessToken,
        pluginType: PluginType.PLUGIN
      })

      const plugin: PeerTubePlugin = res.body.data[0]

      expect(plugin.version).to.equal('0.0.1')
      expect(plugin.latestVersion).to.exist
      expect(plugin.latestVersion).to.not.equal('0.0.1')
    }

    {
      await updatePlugin({
        url: server.url,
        accessToken: server.accessToken,
        npmName: 'peertube-plugin-hello-world'
      })

      const res = await listPlugins({
        url: server.url,
        accessToken: server.accessToken,
        pluginType: PluginType.PLUGIN
      })

      const plugin: PeerTubePlugin = res.body.data[0]

      expect(plugin.version).to.equal(oldVersion)

      const updatedPackageJSON: PluginPackageJson = await getPluginPackageJSON(server, 'peertube-plugin-hello-world')
      expect(updatedPackageJSON.version).to.equal(oldVersion)
    }
  })

  it('Should uninstall the plugin', async function () {
    await uninstallPlugin({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-plugin-hello-world'
    })

    const res = await listPlugins({
      url: server.url,
      accessToken: server.accessToken,
      pluginType: PluginType.PLUGIN
    })

    expect(res.body.total).to.equal(0)
    expect(res.body.data).to.have.lengthOf(0)
  })

  it('Should list uninstalled plugins', async function () {
    const res = await listPlugins({
      url: server.url,
      accessToken: server.accessToken,
      pluginType: PluginType.PLUGIN,
      uninstalled: true
    })

    expect(res.body.total).to.equal(1)
    expect(res.body.data).to.have.lengthOf(1)

    const plugin: PeerTubePlugin = res.body.data[0]
    expect(plugin.name).to.equal('hello-world')
    expect(plugin.enabled).to.be.false
    expect(plugin.uninstalled).to.be.true
  })

  it('Should uninstall the theme', async function () {
    await uninstallPlugin({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-theme-background-red'
    })
  })

  it('Should have updated the configuration', async function () {
    // get /config (default theme + registered themes + registered plugins)
    const res = await getConfig(server.url)
    const config: ServerConfig = res.body

    expect(config.theme.default).to.equal('default')

    const theme = config.theme.registered.find(r => r.name === 'background-red')
    expect(theme).to.be.undefined

    const plugin = config.plugin.registered.find(r => r.name === 'hello-world')
    expect(plugin).to.be.undefined
  })

  it('Should have updated the user theme', async function () {
    const res = await getMyUserInformation(server.url, server.accessToken)
    expect((res.body as User).theme).to.equal('instance-default')
  })

  after(async function () {
    await closeAllSequelize([ server ])
    await cleanupTests([ server ])
  })
})