1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { expectNotStartWith, expectStartWith, FIXTURE_URLS, MockProxy } from '@server/tests/shared'
6 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
7 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
10 createMultipleServers,
14 setAccessTokensToServers,
15 setDefaultVideoChannel,
17 } from '@shared/server-commands'
19 const expect = chai.expect
21 describe('Test proxy', function () {
22 let servers: PeerTubeServer[] = []
25 const goodEnv = { HTTP_PROXY: '' }
26 const badEnv = { HTTP_PROXY: 'http://localhost:9000' }
28 before(async function () {
31 proxy = new MockProxy()
33 const proxyPort = await proxy.initialize()
34 servers = await createMultipleServers(2)
36 goodEnv.HTTP_PROXY = 'http://localhost:' + proxyPort
38 await setAccessTokensToServers(servers)
39 await setDefaultVideoChannel(servers)
40 await doubleFollow(servers[0], servers[1])
43 describe('Federation', function () {
45 it('Should succeed federation with the appropriate proxy config', async function () {
48 await servers[0].kill()
49 await servers[0].run({}, { env: goodEnv })
51 await servers[0].videos.quickUpload({ name: 'video 1' })
53 await waitJobs(servers)
55 for (const server of servers) {
56 const { total, data } = await server.videos.list()
57 expect(total).to.equal(1)
58 expect(data).to.have.lengthOf(1)
62 it('Should fail federation with a wrong proxy config', async function () {
65 await servers[0].kill()
66 await servers[0].run({}, { env: badEnv })
68 await servers[0].videos.quickUpload({ name: 'video 2' })
70 await waitJobs(servers)
73 const { total, data } = await servers[0].videos.list()
74 expect(total).to.equal(2)
75 expect(data).to.have.lengthOf(2)
79 const { total, data } = await servers[1].videos.list()
80 expect(total).to.equal(1)
81 expect(data).to.have.lengthOf(1)
86 describe('Videos import', async function () {
88 function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
89 return servers[0].imports.importVideo({
92 channelId: servers[0].store.channel.id,
93 privacy: VideoPrivacy.PUBLIC,
94 targetUrl: FIXTURE_URLS.peertube_long
100 it('Should succeed import with the appropriate proxy config', async function () {
103 await servers[0].kill()
104 await servers[0].run({}, { env: goodEnv })
108 await waitJobs(servers)
110 const { total, data } = await servers[0].videos.list()
111 expect(total).to.equal(3)
112 expect(data).to.have.lengthOf(3)
115 it('Should fail import with a wrong proxy config', async function () {
118 await servers[0].kill()
119 await servers[0].run({}, { env: badEnv })
121 await quickImport(HttpStatusCode.BAD_REQUEST_400)
125 describe('Object storage', function () {
126 if (areObjectStorageTestsDisabled()) return
128 before(async function () {
131 await ObjectStorageCommand.prepareDefaultBuckets()
134 it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
137 await servers[0].kill()
138 await servers[0].run(ObjectStorageCommand.getDefaultConfig(), { env: goodEnv })
140 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
141 await waitJobs(servers)
143 const video = await servers[0].videos.get({ id: uuid })
145 expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
148 it('Should fail to upload to object storage with a wrong proxy config', async function () {
151 await servers[0].kill()
152 await servers[0].run(ObjectStorageCommand.getDefaultConfig(), { env: badEnv })
154 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
155 await waitJobs(servers)
157 const video = await servers[0].videos.get({ id: uuid })
159 expectNotStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
163 after(async function () {
164 await proxy.terminate()
166 await cleanupTests(servers)