]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/proxy.ts
Prevent object storage mock conflicts
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / proxy.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
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'
7 import {
8 cleanupTests,
9 createMultipleServers,
10 doubleFollow,
11 ObjectStorageCommand,
12 PeerTubeServer,
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
15 waitJobs
16 } from '@shared/server-commands'
17
18 describe('Test proxy', function () {
19 let servers: PeerTubeServer[] = []
20 let proxy: MockProxy
21
22 const goodEnv = { HTTP_PROXY: '' }
23 const badEnv = { HTTP_PROXY: 'http://127.0.0.1:9000' }
24
25 before(async function () {
26 this.timeout(120000)
27
28 proxy = new MockProxy()
29
30 const proxyPort = await proxy.initialize()
31 servers = await createMultipleServers(2)
32
33 goodEnv.HTTP_PROXY = 'http://127.0.0.1:' + proxyPort
34
35 await setAccessTokensToServers(servers)
36 await setDefaultVideoChannel(servers)
37 await doubleFollow(servers[0], servers[1])
38 })
39
40 describe('Federation', function () {
41
42 it('Should succeed federation with the appropriate proxy config', async function () {
43 this.timeout(40000)
44
45 await servers[0].kill()
46 await servers[0].run({}, { env: goodEnv })
47
48 await servers[0].videos.quickUpload({ name: 'video 1' })
49
50 await waitJobs(servers)
51
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)
56 }
57 })
58
59 it('Should fail federation with a wrong proxy config', async function () {
60 this.timeout(40000)
61
62 await servers[0].kill()
63 await servers[0].run({}, { env: badEnv })
64
65 await servers[0].videos.quickUpload({ name: 'video 2' })
66
67 await waitJobs(servers)
68
69 {
70 const { total, data } = await servers[0].videos.list()
71 expect(total).to.equal(2)
72 expect(data).to.have.lengthOf(2)
73 }
74
75 {
76 const { total, data } = await servers[1].videos.list()
77 expect(total).to.equal(1)
78 expect(data).to.have.lengthOf(1)
79 }
80 })
81 })
82
83 describe('Videos import', async function () {
84
85 function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
86 return servers[0].imports.importVideo({
87 attributes: {
88 name: 'video import',
89 channelId: servers[0].store.channel.id,
90 privacy: VideoPrivacy.PUBLIC,
91 targetUrl: FIXTURE_URLS.peertube_long
92 },
93 expectedStatus
94 })
95 }
96
97 it('Should succeed import with the appropriate proxy config', async function () {
98 this.timeout(240000)
99
100 await servers[0].kill()
101 await servers[0].run({}, { env: goodEnv })
102
103 await quickImport()
104
105 await waitJobs(servers)
106
107 const { total, data } = await servers[0].videos.list()
108 expect(total).to.equal(3)
109 expect(data).to.have.lengthOf(3)
110 })
111
112 it('Should fail import with a wrong proxy config', async function () {
113 this.timeout(120000)
114
115 await servers[0].kill()
116 await servers[0].run({}, { env: badEnv })
117
118 await quickImport(HttpStatusCode.BAD_REQUEST_400)
119 })
120 })
121
122 describe('Object storage', function () {
123 if (areMockObjectStorageTestsDisabled()) return
124
125 const objectStorage = new ObjectStorageCommand()
126
127 before(async function () {
128 this.timeout(30000)
129
130 await objectStorage.prepareDefaultMockBuckets()
131 })
132
133 it('Should succeed to upload to object storage with the appropriate proxy config', async function () {
134 this.timeout(120000)
135
136 await servers[0].kill()
137 await servers[0].run(objectStorage.getDefaultMockConfig(), { env: goodEnv })
138
139 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
140 await waitJobs(servers)
141
142 const video = await servers[0].videos.get({ id: uuid })
143
144 expectStartWith(video.files[0].fileUrl, objectStorage.getMockWebVideosBaseUrl())
145 })
146
147 it('Should fail to upload to object storage with a wrong proxy config', async function () {
148 this.timeout(120000)
149
150 await servers[0].kill()
151 await servers[0].run(objectStorage.getDefaultMockConfig(), { env: badEnv })
152
153 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
154 await waitJobs(servers, { skipDelayed: true })
155
156 const video = await servers[0].videos.get({ id: uuid })
157
158 expectNotStartWith(video.files[0].fileUrl, objectStorage.getMockWebVideosBaseUrl())
159 })
160
161 after(async function () {
162 await objectStorage.cleanupMock()
163 })
164 })
165
166 after(async function () {
167 await proxy.terminate()
168
169 await cleanupTests(servers)
170 })
171 })