From c55dc28d598d021a88753dcf3e92143df058d6ad Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Sat, 9 Jul 2022 13:17:45 +0200 Subject: [PATCH] disable info logs in integration tests --- server/.env.test | 4 +++- server/src/app.integration.test.ts | 25 ++++++++++++++++++------- server/src/logger.ts | 4 +++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/server/.env.test b/server/.env.test index ee3f0cb..ba0679f 100644 --- a/server/.env.test +++ b/server/.env.test @@ -1,3 +1,5 @@ DATABASE_URL="file:./test.sqlite" -POST_LIMIT=9999 +POST_LIMIT=50 +POST_LIMIT_WINDOW=1 +LOG_LEVEL=warn \ No newline at end of file diff --git a/server/src/app.integration.test.ts b/server/src/app.integration.test.ts index 4771bf7..c7d462c 100644 --- a/server/src/app.integration.test.ts +++ b/server/src/app.integration.test.ts @@ -1,19 +1,17 @@ import app from "./app"; import request from "supertest"; -import { describe, it, expect } from "vitest"; +import { describe, it, expect, beforeAll } from "vitest"; import prisma from "./client"; +import logger from "./logger"; const testNote = { ciphertext: "sample_ciphertext", hmac: "sample_hmac", }; -describe("GET /api/test", () => { - it('should respond "Hello world!"', async () => { - const res = await request(app).get("/api/test"); - expect(res.statusCode).toBe(200); - expect(res.text).toBe("Hello world!"); - }); +beforeAll(() => { + // logger.level = "error"; + logger.silent(); }); describe("GET /api/note", () => { @@ -92,4 +90,17 @@ describe("POST /api/note", () => { expect(res.body.ciphertext).toEqual(testNote.ciphertext); expect(res.body.hmac).toEqual(testNote.hmac); }); + + it("Applies rate limits to endpoint", async () => { + // make more requests than the post limit set in .env.test + const requests = []; + for (let i = 0; i < 51; i++) { + requests.push(request(app).post("/api/note").send(testNote)); + } + const responses = await Promise.all(requests); + const responseCodes = responses.map((res) => res.statusCode); + + // at least one response should be 429 + expect(responseCodes).toContain(429); + }); }); diff --git a/server/src/logger.ts b/server/src/logger.ts index ded792a..6fa3a75 100644 --- a/server/src/logger.ts +++ b/server/src/logger.ts @@ -8,4 +8,6 @@ import pino, { multistream } from "pino"; // export default pino({}, multistream(streams)); -export default pino({}); +export default pino({ + level: process.env.LOG_LEVEL || "info", +});