Cleanup tests
[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 { About } from '../../../../shared/models/server/about.model'
6 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
7 import {
8   deleteCustomConfig,
9   getAbout,
10   getVideo,
11   killallServers,
12   login,
13   reRunServer,
14   uploadVideo,
15   userLogin,
16   viewVideo,
17   wait
18 } from '../../../../shared/extra-utils'
19 const expect = chai.expect
20
21 import {
22   getConfig,
23   flushTests,
24   flushAndRunServer,
25   registerUser, getCustomConfig, setAccessTokensToServers, updateCustomConfig
26 } from '../../../../shared/extra-utils/index'
27
28 describe('Test application behind a reverse proxy', function () {
29   let server = null
30   let videoId
31
32   before(async function () {
33     this.timeout(30000)
34     server = await flushAndRunServer(1)
35     await setAccessTokensToServers([ server ])
36
37     const { body } = await uploadVideo(server.url, server.accessToken, {})
38     videoId = body.video.uuid
39   })
40
41   it('Should view a video only once with the same IP by default', async function () {
42     this.timeout(20000)
43
44     await viewVideo(server.url, videoId)
45     await viewVideo(server.url, videoId)
46
47     // Wait the repeatable job
48     await wait(8000)
49
50     const { body } = await getVideo(server.url, videoId)
51     expect(body.views).to.equal(1)
52   })
53
54   it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
55     this.timeout(20000)
56
57     await viewVideo(server.url, videoId, 204, '0.0.0.1,127.0.0.1')
58     await viewVideo(server.url, videoId, 204, '0.0.0.2,127.0.0.1')
59
60     // Wait the repeatable job
61     await wait(8000)
62
63     const { body } = await getVideo(server.url, videoId)
64     expect(body.views).to.equal(3)
65   })
66
67   it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
68     this.timeout(20000)
69
70     await viewVideo(server.url, videoId, 204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1')
71     await viewVideo(server.url, videoId, 204, '0.0.0.5,0.0.0.3,127.0.0.1')
72
73     // Wait the repeatable job
74     await wait(8000)
75
76     const { body } = await getVideo(server.url, videoId)
77     expect(body.views).to.equal(4)
78   })
79
80   it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
81     this.timeout(20000)
82
83     await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.6,127.0.0.1')
84     await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.7,127.0.0.1')
85
86     // Wait the repeatable job
87     await wait(8000)
88
89     const { body } = await getVideo(server.url, videoId)
90     expect(body.views).to.equal(6)
91   })
92
93   it('Should rate limit logins', async function () {
94     const user = { username: 'root', password: 'fail' }
95
96     for (let i = 0; i < 19; i++) {
97       await userLogin(server, user, 400)
98     }
99
100     await userLogin(server, user, 429)
101   })
102
103   after(function () {
104     killallServers([ server ])
105   })
106 })