aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/plugins/plugin-storage.ts
blob: c0596819ebf9fc7aac2876eb50db4c97f7b168ed (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { expect } from 'chai'
import { pathExists, readdir, readFile } from 'fs-extra'
import { join } from 'path'
import {
  cleanupTests,
  createSingleServer,
  makeGetRequest,
  PeerTubeServer,
  PluginsCommand,
  setAccessTokensToServers
} from '@shared/extra-utils'
import { HttpStatusCode } from '@shared/models'

describe('Test plugin storage', function () {
  let server: PeerTubeServer

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

    server = await createSingleServer(1)
    await setAccessTokensToServers([ server ])

    await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-six') })
  })

  describe('DB storage', function () {
    it('Should correctly store a subkey', async function () {
      await server.servers.waitUntilLog('superkey stored value is toto')
    })

    it('Should correctly retrieve an array as array from the storage.', async function () {
      await server.servers.waitUntilLog('storedArrayKey isArray is true')
      await server.servers.waitUntilLog('storedArrayKey stored value is toto, toto2')
    })
  })

  describe('Disk storage', function () {
    let dataPath: string
    let pluginDataPath: string

    async function getFileContent () {
      const files = await readdir(pluginDataPath)
      expect(files).to.have.lengthOf(1)

      return readFile(join(pluginDataPath, files[0]), 'utf8')
    }

    before(function () {
      dataPath = server.servers.buildDirectory('plugins/data')
      pluginDataPath = join(dataPath, 'peertube-plugin-test-six')
    })

    it('Should have created the directory on install', async function () {
      const dataPath = server.servers.buildDirectory('plugins/data')
      const pluginDataPath = join(dataPath, 'peertube-plugin-test-six')

      expect(await pathExists(dataPath)).to.be.true
      expect(await pathExists(pluginDataPath)).to.be.true
      expect(await readdir(pluginDataPath)).to.have.lengthOf(0)
    })

    it('Should have created a file', async function () {
      await makeGetRequest({
        url: server.url,
        token: server.accessToken,
        path: '/plugins/test-six/router/create-file',
        expectedStatus: HttpStatusCode.OK_200
      })

      const content = await getFileContent()
      expect(content).to.equal('Prince Ali')
    })

    it('Should still have the file after an uninstallation', async function () {
      await server.plugins.uninstall({ npmName: 'peertube-plugin-test-six' })

      const content = await getFileContent()
      expect(content).to.equal('Prince Ali')
    })

    it('Should still have the file after the reinstallation', async function () {
      await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-six') })

      const content = await getFileContent()
      expect(content).to.equal('Prince Ali')
    })
  })

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