1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { expectNotStartWith, expectStartWith, FIXTURE_URLS, MockProxy } from '@server/tests/shared'
5 import { areObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 const expect = chai.expect
20 describe('Test proxy', function () {
21 let servers: PeerTubeServer[] = []
24 const goodEnv = { HTTP_PROXY: '' }
25 const badEnv = { HTTP_PROXY: 'http://localhost:9000' }
27 before(async function () {
30 proxy = new MockProxy()
32 const proxyPort = await proxy.initialize()
33 servers = await createMultipleServers(2)
35 goodEnv.HTTP_PROXY = 'http://localhost:' + proxyPort
37 await setAccessTokensToServers(servers)
38 await setDefaultVideoChannel(servers)
39 await doubleFollow(servers[0], servers[1])
42 describe('Federation', function () {
44 it('Should succeed federation with the appropriate proxy config', async function () {
47 await servers[0].kill()
48 await servers[0].run({}, { env: goodEnv })
50 await servers[0].videos.quickUpload({ name: 'video 1' })
52 await waitJobs(servers)
54 for (const server of servers) {
55 const { total, data } = await server.videos.list()
56 expect(total).to.equal(1)
57 expect(data).to.have.lengthOf(1)
61 it('Should fail federation with a wrong proxy config', async function () {
64 await servers[0].kill()
65 await servers[0].run({}, { env: badEnv })
67 await servers[0].videos.quickUpload({ name: 'video 2' })
69 await waitJobs(servers)
72 const { total, data } = await servers[0].videos.list()
73 expect(total).to.equal(2)
74 expect(data).to.have.lengthOf(2)
78 const { total, data } = await servers[1].videos.list()
79 expect(total).to.equal(1)
80 expect(data).to.have.lengthOf(1)
85 describe('Videos import', async function () {
87 function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
88 return servers[0].imports.importVideo({
91 channelId: servers[0].store.channel.id,
92 privacy: VideoPrivacy.PUBLIC,
93 targetUrl: FIXTURE_URLS.peertube_long
99 it('Should succeed import with the appropriate proxy config', async function () {
102 await servers[0].kill()
103 await servers[0].run({}, { env: goodEnv })
107 await waitJobs(servers)
109 const { total, data } = await servers[0].videos.list()
110 expect(total).to.equal(3)
111 expect(data).to.have.lengthOf(3)
114 it('Should fail import with a wrong proxy config', async function () {
117 await servers[0].kill()
118 await servers[0].run({}, { env: badEnv })
120 await quickImport(HttpStatusCode.BAD_REQUEST_400)
124 describe('Object storage', function () {
125 if (areObjectStorageTestsDisabled()) return
127 before(async function () {
130 await ObjectStorageCommand.prepareDefaultBuckets()
133 it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
136 await servers[0].kill()
137 await servers[0].run(ObjectStorageCommand.getDefaultConfig(), { env: goodEnv })
139 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
140 await waitJobs(servers)
142 const video = await servers[0].videos.get({ id: uuid })
144 expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
147 it('Should fail to upload to object storage with a wrong proxy config', async function () {
150 await servers[0].kill()
151 await servers[0].run(ObjectStorageCommand.getDefaultConfig(), { env: badEnv })
153 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
154 await waitJobs(servers)
156 const video = await servers[0].videos.get({ id: uuid })
158 expectNotStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl())
162 after(async function () {
163 await proxy.terminate()
165 await cleanupTests(servers)