From 58e17c5e50c9ffa4ce6e788b0b305b2f2e4b84b7 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 15 Apr 2021 15:12:07 -0700 Subject: [PATCH] feat(testing): add tests for RateLimiter --- test/unit/routes/login.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/unit/routes/login.test.ts diff --git a/test/unit/routes/login.test.ts b/test/unit/routes/login.test.ts new file mode 100644 index 00000000..1ef5b659 --- /dev/null +++ b/test/unit/routes/login.test.ts @@ -0,0 +1,23 @@ +import { RateLimiter } from "../../../src/node/routes/login" + +describe("login", () => { + describe("RateLimiter", () => { + it("should allow one try ", () => { + const limiter = new RateLimiter() + expect(limiter.try()).toBe(true) + }) + + it("should not allow more than 14 tries in less than an hour", () => { + const limiter = new RateLimiter() + + // The limiter allows 2 tries per minute plus 12 per hour + // so if we run it 15 times, 14 should return true and the last + // should return false + for (let i = 1; i <= 14; i++) { + expect(limiter.try()).toBe(true) + } + + expect(limiter.try()).toBe(false) + }) + }) +})