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