aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/server/reverse-proxy.ts
blob: b6b33a884f7ec1a51c4d9857040f0123378445b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* tslint:disable:no-unused-expression */

import 'mocha'
import * as chai from 'chai'
import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils'
import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'

const expect = chai.expect

describe('Test application behind a reverse proxy', function () {
  let server = null
  let videoId

  before(async function () {
    this.timeout(30000)

    const config = {
      rates_limit: {
        api: {
          max: 50,
          window: 5000
        },
        signup: {
          max: 3,
          window: 5000
        },
        login: {
          max: 20
        }
      },
      signup: {
        limit: 20
      }
    }

    server = await flushAndRunServer(1, config)
    await setAccessTokensToServers([ server ])

    const { body } = await uploadVideo(server.url, server.accessToken, {})
    videoId = body.video.uuid
  })

  it('Should view a video only once with the same IP by default', async function () {
    this.timeout(20000)

    await viewVideo(server.url, videoId)
    await viewVideo(server.url, videoId)

    // Wait the repeatable job
    await wait(8000)

    const { body } = await getVideo(server.url, videoId)
    expect(body.views).to.equal(1)
  })

  it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
    this.timeout(20000)

    await viewVideo(server.url, videoId, 204, '0.0.0.1,127.0.0.1')
    await viewVideo(server.url, videoId, 204, '0.0.0.2,127.0.0.1')

    // Wait the repeatable job
    await wait(8000)

    const { body } = await getVideo(server.url, videoId)
    expect(body.views).to.equal(3)
  })

  it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
    this.timeout(20000)

    await viewVideo(server.url, videoId, 204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1')
    await viewVideo(server.url, videoId, 204, '0.0.0.5,0.0.0.3,127.0.0.1')

    // Wait the repeatable job
    await wait(8000)

    const { body } = await getVideo(server.url, videoId)
    expect(body.views).to.equal(4)
  })

  it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
    this.timeout(20000)

    await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.6,127.0.0.1')
    await viewVideo(server.url, videoId, 204, '0.0.0.8,0.0.0.7,127.0.0.1')

    // Wait the repeatable job
    await wait(8000)

    const { body } = await getVideo(server.url, videoId)
    expect(body.views).to.equal(6)
  })

  it('Should rate limit logins', async function () {
    const user = { username: 'root', password: 'fail' }

    for (let i = 0; i < 19; i++) {
      await userLogin(server, user, 400)
    }

    await userLogin(server, user, 429)
  })

  it('Should rate limit signup', async function () {
    for (let i = 0; i < 10; i++) {
      try {
        await registerUser(server.url, 'test' + i, 'password')
      } catch {
        // empty
      }
    }

    await registerUser(server.url, 'test42', 'password', 429)
  })

  it('Should not rate limit failed signup', async function () {
    this.timeout(30000)

    await wait(7000)

    for (let i = 0; i < 3; i++) {
      await registerUser(server.url, 'test' + i, 'password', 409)
    }

    await registerUser(server.url, 'test43', 'password', 204)

  })

  it('Should rate limit API calls', async function () {
    this.timeout(30000)

    await wait(7000)

    for (let i = 0; i < 100; i++) {
      try {
        await getVideo(server.url, videoId)
      } catch {
        // don't care if it fails
      }
    }

    await getVideo(server.url, videoId, 429)
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})