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

import 'mocha'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../shared/extra-utils/server/servers'
import {
  getPluginTestPath,
  installPlugin,
  makeGetRequest,
  makePostBodyRequest,
  setAccessTokensToServers, uninstallPlugin
} from '../../../shared/extra-utils'
import { expect } from 'chai'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'

describe('Test plugin helpers', function () {
  let server: ServerInfo
  const basePaths = [
    '/plugins/test-five/router/',
    '/plugins/test-five/0.0.1/router/'
  ]

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

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

    await installPlugin({
      url: server.url,
      accessToken: server.accessToken,
      path: getPluginTestPath('-five')
    })
  })

  it('Should answer "pong"', async function () {
    for (const path of basePaths) {
      const res = await makeGetRequest({
        url: server.url,
        path: path + 'ping',
        statusCodeExpected: HttpStatusCode.OK_200
      })

      expect(res.body.message).to.equal('pong')
    }
  })

  it('Should check if authenticated', async function () {
    for (const path of basePaths) {
      const res = await makeGetRequest({
        url: server.url,
        path: path + 'is-authenticated',
        token: server.accessToken,
        statusCodeExpected: 200
      })

      expect(res.body.isAuthenticated).to.equal(undefined)

      const secRes = await makeGetRequest({
        url: server.url,
        path: path + 'is-authenticated',
        statusCodeExpected: 200
      })

      expect(secRes.body.isAuthenticated).to.equal(false)
    }
  })

  it('Should mirror post body', async function () {
    const body = {
      hello: 'world',
      riri: 'fifi',
      loulou: 'picsou'
    }

    for (const path of basePaths) {
      const res = await makePostBodyRequest({
        url: server.url,
        path: path + 'form/post/mirror',
        fields: body,
        statusCodeExpected: HttpStatusCode.OK_200
      })

      expect(res.body).to.deep.equal(body)
    }
  })

  it('Should remove the plugin and remove the routes', async function () {
    await uninstallPlugin({
      url: server.url,
      accessToken: server.accessToken,
      npmName: 'peertube-plugin-test-five'
    })

    for (const path of basePaths) {
      await makeGetRequest({
        url: server.url,
        path: path + 'ping',
        statusCodeExpected: HttpStatusCode.NOT_FOUND_404
      })

      await makePostBodyRequest({
        url: server.url,
        path: path + 'ping',
        fields: {},
        statusCodeExpected: HttpStatusCode.NOT_FOUND_404
      })
    }
  })

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