]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/proxy.ts
c40a4f9b8ff75d8bfbb431469ef98975b91b1770
[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 { FIXTURE_URLS, MockProxy } from '@server/tests/shared'
6 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createMultipleServers,
10 doubleFollow,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 waitJobs
15 } from '@shared/server-commands'
16
17 const expect = chai.expect
18
19 describe('Test proxy', function () {
20 let servers: PeerTubeServer[] = []
21 let proxy: MockProxy
22
23 const goodEnv = { HTTP_PROXY: '' }
24 const badEnv = { HTTP_PROXY: 'http://localhost:9000' }
25
26 before(async function () {
27 this.timeout(120000)
28
29 proxy = new MockProxy()
30
31 const proxyPort = await proxy.initialize()
32 servers = await createMultipleServers(2)
33
34 goodEnv.HTTP_PROXY = 'http://localhost:' + proxyPort
35
36 await setAccessTokensToServers(servers)
37 await setDefaultVideoChannel(servers)
38 await doubleFollow(servers[0], servers[1])
39 })
40
41 describe('Federation', function () {
42
43 it('Should succeed federation with the appropriate proxy config', async function () {
44 this.timeout(40000)
45
46 await servers[0].kill()
47 await servers[0].run({}, { env: goodEnv })
48
49 await servers[0].videos.quickUpload({ name: 'video 1' })
50
51 await waitJobs(servers)
52
53 for (const server of servers) {
54 const { total, data } = await server.videos.list()
55 expect(total).to.equal(1)
56 expect(data).to.have.lengthOf(1)
57 }
58 })
59
60 it('Should fail federation with a wrong proxy config', async function () {
61 this.timeout(40000)
62
63 await servers[0].kill()
64 await servers[0].run({}, { env: badEnv })
65
66 await servers[0].videos.quickUpload({ name: 'video 2' })
67
68 await waitJobs(servers)
69
70 {
71 const { total, data } = await servers[0].videos.list()
72 expect(total).to.equal(2)
73 expect(data).to.have.lengthOf(2)
74 }
75
76 {
77 const { total, data } = await servers[1].videos.list()
78 expect(total).to.equal(1)
79 expect(data).to.have.lengthOf(1)
80 }
81 })
82 })
83
84 describe('Videos import', async function () {
85
86 function quickImport (expectedStatus: HttpStatusCode = HttpStatusCode.OK_200) {
87 return servers[0].imports.importVideo({
88 attributes: {
89 name: 'video import',
90 channelId: servers[0].store.channel.id,
91 privacy: VideoPrivacy.PUBLIC,
92 targetUrl: FIXTURE_URLS.peertube_long
93 },
94 expectedStatus
95 })
96 }
97
98 it('Should succeed import with the appropriate proxy config', async function () {
99 this.timeout(40000)
100
101 await servers[0].kill()
102 await servers[0].run({}, { env: goodEnv })
103
104 await quickImport()
105
106 await waitJobs(servers)
107
108 const { total, data } = await servers[0].videos.list()
109 expect(total).to.equal(3)
110 expect(data).to.have.lengthOf(3)
111 })
112
113 it('Should fail import with a wrong proxy config', async function () {
114 this.timeout(40000)
115
116 await servers[0].kill()
117 await servers[0].run({}, { env: badEnv })
118
119 await quickImport(HttpStatusCode.BAD_REQUEST_400)
120 })
121 })
122
123 after(async function () {
124 await proxy.terminate()
125
126 await cleanupTests(servers)
127 })
128 })