X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fserver%2Freverse-proxy.ts;h=d0d79c4f616683a361c4684e1448223594dc61b8;hb=8b6f0fd53d12faf54a58602a8bcfab05e8b5947b;hp=908b4a68cbd3a9cf0d71115ef72f74f22d97da80;hpb=3cd0734fd9b0ff21aaef02317a874e8f1c06e027;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/server/reverse-proxy.ts b/server/tests/api/server/reverse-proxy.ts index 908b4a68c..d0d79c4f6 100644 --- a/server/tests/api/server/reverse-proxy.ts +++ b/server/tests/api/server/reverse-proxy.ts @@ -1,18 +1,11 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' import * as chai from 'chai' -import { About } from '../../../../shared/models/server/about.model' -import { CustomConfig } from '../../../../shared/models/server/custom-config.model' -import { deleteCustomConfig, getAbout, getVideo, killallServers, login, reRunServer, uploadVideo, userLogin, viewVideo } from '../../utils' -const expect = chai.expect +import { cleanupTests, getVideo, registerUser, uploadVideo, userLogin, viewVideo, wait } from '../../../../shared/extra-utils' +import { flushAndRunServer, setAccessTokensToServers } from '../../../../shared/extra-utils/index' -import { - getConfig, - flushTests, - runServer, - registerUser, getCustomConfig, setAccessTokensToServers, updateCustomConfig -} from '../../utils/index' +const expect = chai.expect describe('Test application behind a reverse proxy', function () { let server = null @@ -21,8 +14,26 @@ describe('Test application behind a reverse proxy', function () { before(async function () { this.timeout(30000) - await flushTests() - server = await runServer(1) + 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, {}) @@ -30,33 +41,53 @@ describe('Test application behind a reverse proxy', function () { }) 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) }) @@ -64,14 +95,55 @@ describe('Test application behind a reverse proxy', function () { it('Should rate limit logins', async function () { const user = { username: 'root', password: 'fail' } - for (let i = 0; i < 14; i++) { + 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 () { - killallServers([ server ]) + await cleanupTests([ server ]) }) })