aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/server/proxy.ts
blob: c7d13f4abc3d49131a653afe0eb1d3d6c8c56b8b (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { HttpStatusCode, HttpStatusCodeType, VideoPrivacy } from '@peertube/peertube-models'
import { areMockObjectStorageTestsDisabled } from '@peertube/peertube-node-utils'
import {
  cleanupTests,
  createMultipleServers,
  doubleFollow,
  ObjectStorageCommand,
  PeerTubeServer,
  setAccessTokensToServers,
  setDefaultVideoChannel,
  waitJobs
} from '@peertube/peertube-server-commands'
import { FIXTURE_URLS } from '@tests/shared/tests.js'
import { expectStartWith, expectNotStartWith } from '@tests/shared/checks.js'
import { MockProxy } from '@tests/shared/mock-servers/mock-proxy.js'

describe('Test proxy', function () {
  let servers: PeerTubeServer[] = []
  let proxy: MockProxy

  const goodEnv = { HTTP_PROXY: '' }
  const badEnv = { HTTP_PROXY: 'http://127.0.0.1:9000' }

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

    proxy = new MockProxy()

    const proxyPort = await proxy.initialize()
    servers = await createMultipleServers(2)

    goodEnv.HTTP_PROXY = 'http://127.0.0.1:' + proxyPort

    await setAccessTokensToServers(servers)
    await setDefaultVideoChannel(servers)
    await doubleFollow(servers[0], servers[1])
  })

  describe('Federation', function () {

    it('Should succeed federation with the appropriate proxy config', async function () {
      this.timeout(40000)

      await servers[0].kill()
      await servers[0].run({}, { env: goodEnv })

      await servers[0].videos.quickUpload({ name: 'video 1' })

      await waitJobs(servers)

      for (const server of servers) {
        const { total, data } = await server.videos.list()
        expect(total).to.equal(1)
        expect(data).to.have.lengthOf(1)
      }
    })

    it('Should fail federation with a wrong proxy config', async function () {
      this.timeout(40000)

      await servers[0].kill()
      await servers[0].run({}, { env: badEnv })

      await servers[0].videos.quickUpload({ name: 'video 2' })

      await waitJobs(servers)

      {
        const { total, data } = await servers[0].videos.list()
        expect(total).to.equal(2)
        expect(data).to.have.lengthOf(2)
      }

      {
        const { total, data } = await servers[1].videos.list()
        expect(total).to.equal(1)
        expect(data).to.have.lengthOf(1)
      }
    })
  })

  describe('Videos import', async function () {

    function quickImport (expectedStatus: HttpStatusCodeType = HttpStatusCode.OK_200) {
      return servers[0].imports.importVideo({
        attributes: {
          name: 'video import',
          channelId: servers[0].store.channel.id,
          privacy: VideoPrivacy.PUBLIC,
          targetUrl: FIXTURE_URLS.peertube_long
        },
        expectedStatus
      })
    }

    it('Should succeed import with the appropriate proxy config', async function () {
      this.timeout(240000)

      await servers[0].kill()
      await servers[0].run({}, { env: goodEnv })

      await quickImport()

      await waitJobs(servers)

      const { total, data } = await servers[0].videos.list()
      expect(total).to.equal(3)
      expect(data).to.have.lengthOf(3)
    })

    it('Should fail import with a wrong proxy config', async function () {
      this.timeout(120000)

      await servers[0].kill()
      await servers[0].run({}, { env: badEnv })

      await quickImport(HttpStatusCode.BAD_REQUEST_400)
    })
  })

  describe('Object storage', function () {
    if (areMockObjectStorageTestsDisabled()) return

    const objectStorage = new ObjectStorageCommand()

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

      await objectStorage.prepareDefaultMockBuckets()
    })

    it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
      this.timeout(120000)

      await servers[0].kill()
      await servers[0].run(objectStorage.getDefaultMockConfig(), { env: goodEnv })

      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      await waitJobs(servers)

      const video = await servers[0].videos.get({ id: uuid })

      expectStartWith(video.files[0].fileUrl, objectStorage.getMockWebVideosBaseUrl())
    })

    it('Should fail to upload to object storage with a wrong proxy config', async function () {
      this.timeout(120000)

      await servers[0].kill()
      await servers[0].run(objectStorage.getDefaultMockConfig(), { env: badEnv })

      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      await waitJobs(servers, { skipDelayed: true })

      const video = await servers[0].videos.get({ id: uuid })

      expectNotStartWith(video.files[0].fileUrl, objectStorage.getMockWebVideosBaseUrl())
    })

    after(async function () {
      await objectStorage.cleanupMock()
    })
  })

  after(async function () {
    await proxy.terminate()

    await cleanupTests(servers)
  })
})