From 7928dc2bff2d74a4da27ac3c88e3fa891369a80e Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 16 Apr 2021 14:23:46 -0700 Subject: [PATCH] feat: add test for limiter.canTry() --- test/unit/routes/login.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/routes/login.test.ts b/test/unit/routes/login.test.ts index 1ef5b659..fde02b2c 100644 --- a/test/unit/routes/login.test.ts +++ b/test/unit/routes/login.test.ts @@ -7,6 +7,19 @@ describe("login", () => { expect(limiter.try()).toBe(true) }) + it("should pull tokens from both limiters (minute & hour)", () => { + const limiter = new RateLimiter() + + // Try twice, which pulls two from the minute bucket + limiter.try() + limiter.try() + + // Check that we can still try + // which should be true since there are 12 remaining in the hour bucket + expect(limiter.canTry()).toBe(true) + expect(limiter.try()).toBe(true) + }) + it("should not allow more than 14 tries in less than an hour", () => { const limiter = new RateLimiter()