1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, wait } from '@shared/extra-utils'
5 import { HttpStatusCode } from '@shared/models'
7 describe('Test application behind a reverse proxy', function () {
8 let server: PeerTubeServer
11 before(async function () {
33 server = await createSingleServer(1, config)
34 await setAccessTokensToServers([ server ])
36 const { uuid } = await server.videos.upload()
40 it('Should view a video only once with the same IP by default', async function () {
43 await server.videos.view({ id: videoId })
44 await server.videos.view({ id: videoId })
46 // Wait the repeatable job
49 const video = await server.videos.get({ id: videoId })
50 expect(video.views).to.equal(1)
53 it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
56 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' })
57 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' })
59 // Wait the repeatable job
62 const video = await server.videos.get({ id: videoId })
63 expect(video.views).to.equal(3)
66 it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
69 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' })
70 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' })
72 // Wait the repeatable job
75 const video = await server.videos.get({ id: videoId })
76 expect(video.views).to.equal(4)
79 it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
82 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' })
83 await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' })
85 // Wait the repeatable job
88 const video = await server.videos.get({ id: videoId })
89 expect(video.views).to.equal(6)
92 it('Should rate limit logins', async function () {
93 const user = { username: 'root', password: 'fail' }
95 for (let i = 0; i < 19; i++) {
96 await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
99 await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
102 it('Should rate limit signup', async function () {
103 for (let i = 0; i < 10; i++) {
105 await server.users.register({ username: 'test' + i })
111 await server.users.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
114 it('Should not rate limit failed signup', async function () {
119 for (let i = 0; i < 3; i++) {
120 await server.users.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 })
123 await server.users.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 })
127 it('Should rate limit API calls', async function () {
132 for (let i = 0; i < 100; i++) {
134 await server.videos.get({ id: videoId })
136 // don't care if it fails
140 await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
143 after(async function () {
144 await cleanupTests([ server ])