1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { expectNotStartWith, expectStartWith, FIXTURE_URLS, MockProxy } from '@server/tests/shared'
5 import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 describe('Test proxy', function () {
19 let servers: PeerTubeServer[] = []
22 const goodEnv = { HTTP_PROXY: '' }
23 const badEnv = { HTTP_PROXY: 'http://127.0.0.1:9000' }
25 before(async function () {
28 proxy = new MockProxy()
30 const proxyPort = await proxy.initialize()
31 servers = await createMultipleServers(2)
33 goodEnv.HTTP_PROXY = 'http://127.0.0.1:' + proxyPort
35 await setAccessTokensToServers(servers)
36 await setDefaultVideoChannel(servers)
37 await doubleFollow(servers[0], servers[1])
40 describe('Federation', function () {
42 it('Should succeed federation with the appropriate proxy config', async function () {
45 await servers[0].kill()
46 await servers[0].run({}, { env: goodEnv })
48 await servers[0].videos.quickUpload({ name: 'video 1' })
50 await waitJobs(servers)
52 for (const server of servers) {
53 const { total, data } = await server.videos.list()
54 expect(total).to.equal(1)
55 expect(data).to.have.lengthOf(1)
59 it('Should fail federation with a wrong proxy config', async function () {
62 await servers[0].kill()
63 await servers[0].run({}, { env: badEnv })
65 await servers[0].videos.quickUpload({ name: 'video 2' })
67 await waitJobs(servers)
70 const { total, data } = await servers[0].videos.list()
71 expect(total).to.equal(2)
72 expect(data).to.have.lengthOf(2)
76 const { total, data } = await servers[1].videos.list()
77 expect(total).to.equal(1)
78 expect(data).to.have.lengthOf(1)
83 describe('Videos import', async function () {
85 function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
86 return servers[0].imports.importVideo({
89 channelId: servers[0].store.channel.id,
90 privacy: VideoPrivacy.PUBLIC,
91 targetUrl: FIXTURE_URLS.peertube_long
97 it('Should succeed import with the appropriate proxy config', async function () {
100 await servers[0].kill()
101 await servers[0].run({}, { env: goodEnv })
105 await waitJobs(servers)
107 const { total, data } = await servers[0].videos.list()
108 expect(total).to.equal(3)
109 expect(data).to.have.lengthOf(3)
112 it('Should fail import with a wrong proxy config', async function () {
115 await servers[0].kill()
116 await servers[0].run({}, { env: badEnv })
118 await quickImport(HttpStatusCode.BAD_REQUEST_400)
122 describe('Object storage', function () {
123 if (areMockObjectStorageTestsDisabled()) return
125 before(async function () {
128 await ObjectStorageCommand.prepareDefaultMockBuckets()
131 it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
134 await servers[0].kill()
135 await servers[0].run(ObjectStorageCommand.getDefaultMockConfig(), { env: goodEnv })
137 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
138 await waitJobs(servers)
140 const video = await servers[0].videos.get({ id: uuid })
142 expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
145 it('Should fail to upload to object storage with a wrong proxy config', async function () {
148 await servers[0].kill()
149 await servers[0].run(ObjectStorageCommand.getDefaultMockConfig(), { env: badEnv })
151 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
152 await waitJobs(servers, { skipDelayed: true })
154 const video = await servers[0].videos.get({ id: uuid })
156 expectNotStartWith(video.files[0].fileUrl, ObjectStorageCommand.getMockWebTorrentBaseUrl())
160 after(async function () {
161 await proxy.terminate()
163 await cleanupTests(servers)