]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/proxy.ts
shared/ typescript types dir server-commands
[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 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 createMultipleServers,
8 doubleFollow,
9 FIXTURE_URLS,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 waitJobs
14 } from '@shared/server-commands'
15 import { MockProxy } from '@shared/server-commands/mock-servers/mock-proxy'
16 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
17
18 const expect = chai.expect
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://localhost: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://localhost:' + 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: HttpStatusCode = 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(40000)
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(40000)
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 after(async function () {
125 await proxy.terminate()
126
127 await cleanupTests(servers)
128 })
129 })