1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils'
6 import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
7 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
9 const expect = chai.expect
11 describe('Test application behind a reverse proxy', function () {
15 before(async function () {
37 server = await flushAndRunServer(1, config)
38 await setAccessTokensToServers([ server ])
40 const { body } = await uploadVideo(server.url, server.accessToken, {})
41 videoId = body.video.uuid
44 it('Should view a video only once with the same IP by default', async function () {
47 await viewVideo(server.url, videoId)
48 await viewVideo(server.url, videoId)
50 // Wait the repeatable job
53 const { body } = await getVideo(server.url, videoId)
54 expect(body.views).to.equal(1)
57 it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
60 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.1,127.0.0.1')
61 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.2,127.0.0.1')
63 // Wait the repeatable job
66 const { body } = await getVideo(server.url, videoId)
67 expect(body.views).to.equal(3)
70 it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
73 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.4,0.0.0.3,::ffff:127.0.0.1')
74 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.5,0.0.0.3,127.0.0.1')
76 // Wait the repeatable job
79 const { body } = await getVideo(server.url, videoId)
80 expect(body.views).to.equal(4)
83 it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
86 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.8,0.0.0.6,127.0.0.1')
87 await viewVideo(server.url, videoId, HttpStatusCode.NO_CONTENT_204, '0.0.0.8,0.0.0.7,127.0.0.1')
89 // Wait the repeatable job
92 const { body } = await getVideo(server.url, videoId)
93 expect(body.views).to.equal(6)
96 it('Should rate limit logins', async function () {
97 const user = { username: 'root', password: 'fail' }
99 for (let i = 0; i < 19; i++) {
100 await userLogin(server, user, HttpStatusCode.BAD_REQUEST_400)
103 await userLogin(server, user, HttpStatusCode.TOO_MANY_REQUESTS_429)
106 it('Should rate limit signup', async function () {
107 for (let i = 0; i < 10; i++) {
109 await registerUser(server.url, 'test' + i, 'password')
115 await registerUser(server.url, 'test42', 'password', HttpStatusCode.TOO_MANY_REQUESTS_429)
118 it('Should not rate limit failed signup', async function () {
123 for (let i = 0; i < 3; i++) {
124 await registerUser(server.url, 'test' + i, 'password', HttpStatusCode.CONFLICT_409)
127 await registerUser(server.url, 'test43', 'password', HttpStatusCode.NO_CONTENT_204)
131 it('Should rate limit API calls', async function () {
136 for (let i = 0; i < 100; i++) {
138 await getVideo(server.url, videoId)
140 // don't care if it fails
144 await getVideo(server.url, videoId, HttpStatusCode.TOO_MANY_REQUESTS_429)
147 after(async function () {
148 await cleanupTests([ server ])