1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { HttpStatusCode } from '@shared/models'
6 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 describe('Test application behind a reverse proxy', function () {
9 let server: PeerTubeServer
12 before(async function () {
34 server = await createSingleServer(1, config)
35 await setAccessTokensToServers([ server ])
37 const { uuid } = await server.videos.upload()
41 it('Should view a video only once with the same IP by default', async function () {
44 await server.videos.view({ id: videoId })
45 await server.videos.view({ id: videoId })
47 // Wait the repeatable job
50 const video = await server.videos.get({ id: videoId })
51 expect(video.views).to.equal(1)
54 it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
57 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' })
58 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' })
60 // Wait the repeatable job
63 const video = await server.videos.get({ id: videoId })
64 expect(video.views).to.equal(3)
67 it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
70 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' })
71 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' })
73 // Wait the repeatable job
76 const video = await server.videos.get({ id: videoId })
77 expect(video.views).to.equal(4)
80 it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
83 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' })
84 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' })
86 // Wait the repeatable job
89 const video = await server.videos.get({ id: videoId })
90 expect(video.views).to.equal(6)
93 it('Should rate limit logins', async function () {
94 const user = { username: 'root', password: 'fail' }
96 for (let i = 0; i < 19; i++) {
97 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
100 await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
103 it('Should rate limit signup', async function () {
104 for (let i = 0; i < 10; i++) {
106 await server.users.register({ username: 'test' + i })
112 await server.users.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
115 it('Should not rate limit failed signup', async function () {
120 for (let i = 0; i < 3; i++) {
121 await server.users.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 })
124 await server.users.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 })
128 it('Should rate limit API calls', async function () {
133 for (let i = 0; i < 100; i++) {
135 await server.videos.get({ id: videoId })
137 // don't care if it fails
141 await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
144 after(async function () {
145 await cleanupTests([ server ])