Merge branch 'release/v1.3.0' into develop
[oweals/peertube.git] / server / tests / api / server / reverse-proxy.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { cleanupTests, getVideo, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils'
6 import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
7
8 const expect = chai.expect
9
10 describe('Test application behind a reverse proxy', function () {
11   let server = null
12   let videoId
13
14   before(async function () {
15     this.timeout(30000)
16     server = await flushAndRunServer(1)
17     await setAccessTokensToServers([ server ])
18
19     const { body } = await uploadVideo(server.url, server.accessToken, {})
20     videoId = body.video.uuid
21   })
22
23   it('Should view a video only once with the same IP by default', async function () {
24     this.timeout(20000)
25
26     await viewVideo(server.url, videoId)
27     await viewVideo(server.url, videoId)
28
29     // Wait the repeatable job
30     await wait(8000)
31
32     const { body } = await getVideo(server.url, videoId)
33     expect(body.views).to.equal(1)
34   })
35
36   it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
37     this.timeout(20000)
38
39     await viewVideo(server.url, videoId, 204, '0.0.0.1,127.0.0.1')
40     await viewVideo(server.url, videoId, 204, '0.0.0.2,127.0.0.1')
41
42     // Wait the repeatable job
43     await wait(8000)
44
45     const { body } = await getVideo(server.url, videoId)
46     expect(body.views).to.equal(3)
47   })
48
49   it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
50     this.timeout(20000)
51
52     await viewVideo(server.url, videoId, 204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1')
53     await viewVideo(server.url, videoId, 204, '0.0.0.5,0.0.0.3,127.0.0.1')
54
55     // Wait the repeatable job
56     await wait(8000)
57
58     const { body } = await getVideo(server.url, videoId)
59     expect(body.views).to.equal(4)
60   })
61
62   it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
63     this.timeout(20000)
64
65     await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.6,127.0.0.1')
66     await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.7,127.0.0.1')
67
68     // Wait the repeatable job
69     await wait(8000)
70
71     const { body } = await getVideo(server.url, videoId)
72     expect(body.views).to.equal(6)
73   })
74
75   it('Should rate limit logins', async function () {
76     const user = { username: 'root', password: 'fail' }
77
78     for (let i = 0; i < 19; i++) {
79       await userLogin(server, user, 400)
80     }
81
82     await userLogin(server, user, 429)
83   })
84
85   after(async function () {
86     await cleanupTests([ server ])
87   })
88 })