X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fserver%2Freverse-proxy.ts;h=fc9b88a6a84079bf172c5d7310bfef6696223c19;hb=bf54587a3e2ad9c2c186828f2a5682b91ee2cc00;hp=ee0fffd5ae3f6e7d1373b8c150043408d290bad6;hpb=b718fd22374d64534bcfe69932cf562894abed6a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/server/reverse-proxy.ts b/server/tests/api/server/reverse-proxy.ts index ee0fffd5a..fc9b88a6a 100644 --- a/server/tests/api/server/reverse-proxy.ts +++ b/server/tests/api/server/reverse-proxy.ts @@ -1,108 +1,146 @@ -/* tslint:disable:no-unused-expression */ - -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, - wait -} from '../../../../shared/utils' -const expect = chai.expect - -import { - getConfig, - flushTests, - runServer, - registerUser, getCustomConfig, setAccessTokensToServers, updateCustomConfig -} from '../../../../shared/utils/index' +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import { expect } from 'chai' +import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, wait } from '@shared/server-commands' +import { HttpStatusCode } from '@shared/models' describe('Test application behind a reverse proxy', function () { - let server = null - let videoId + let server: PeerTubeServer + let videoId: string 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 createSingleServer(1, config) await setAccessTokensToServers([ server ]) - const { body } = await uploadVideo(server.url, server.accessToken, {}) - videoId = body.video.uuid + const { uuid } = await server.videos.upload() + videoId = 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) + await server.videos.view({ id: videoId }) + await server.videos.view({ id: videoId }) // Wait the repeatable job await wait(8000) - const { body } = await getVideo(server.url, videoId) - expect(body.views).to.equal(1) + const video = await server.videos.get({ id: videoId }) + expect(video.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') + await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' }) + await server.videos.view({ id: videoId, xForwardedFor: '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) + const video = await server.videos.get({ id: videoId }) + expect(video.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') + await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' }) + await server.videos.view({ id: videoId, xForwardedFor: '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) + const video = await server.videos.get({ id: videoId }) + expect(video.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') + await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' }) + await server.videos.view({ id: videoId, xForwardedFor: '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) + const video = await server.videos.get({ id: videoId }) + expect(video.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 server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + } + + await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) + }) + + it('Should rate limit signup', async function () { + for (let i = 0; i < 10; i++) { + try { + await server.users.register({ username: 'test' + i }) + } catch { + // empty + } + } + + await server.users.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) + }) + + it('Should not rate limit failed signup', async function () { + this.timeout(30000) + + await wait(7000) + + for (let i = 0; i < 3; i++) { + await server.users.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 }) + } + + await server.users.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 }) + + }) + + it('Should rate limit API calls', async function () { + this.timeout(30000) + + await wait(7000) + + for (let i = 0; i < 100; i++) { + try { + await server.videos.get({ id: videoId }) + } catch { + // don't care if it fails + } } - await userLogin(server, user, 429) + await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) }) after(async function () { - killallServers([ server ]) + await cleanupTests([ server ]) }) })