From f80d5c3764a06ff1a4cffc8835d3afa2dc6d12a8 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Mon, 19 Apr 2021 11:21:38 -0700 Subject: [PATCH] refactor: rateLimiter.canTry logic to check >= 1 --- src/node/routes/login.ts | 5 ++++- test/e2e/login.test.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/node/routes/login.ts b/src/node/routes/login.ts index e0b5ddda9..017b88307 100644 --- a/src/node/routes/login.ts +++ b/src/node/routes/login.ts @@ -18,7 +18,10 @@ export class RateLimiter { private readonly hourLimiter = new Limiter(12, "hour") public canTry(): boolean { - return this.minuteLimiter.getTokensRemaining() > 0 || this.hourLimiter.getTokensRemaining() > 0 + // Note: we must check using >= 1 because technically when there are no tokens left + // you get back a number like 0.00013333333333333334 + // which would cause fail if the logic were > 0 + return this.minuteLimiter.getTokensRemaining() >= 1 || this.hourLimiter.getTokensRemaining() >= 1 } public removeToken(): boolean { diff --git a/test/e2e/login.test.ts b/test/e2e/login.test.ts index daefd2e3a..4277e2cd3 100644 --- a/test/e2e/login.test.ts +++ b/test/e2e/login.test.ts @@ -54,7 +54,7 @@ test.describe("login", () => { // The current RateLimiter allows 2 logins per minute plus // 12 logins per hour for a total of 14 // See: src/node/routes/login.ts - for (let i = 1; i <= 13; i++) { + for (let i = 1; i <= 14; i++) { await page.click(".submit") await page.waitForLoadState("networkidle") // We double-check that the correct error message shows