Skip to content

Commit

Permalink
fix: add tests for ratelimit with protection
Browse files Browse the repository at this point in the history
  • Loading branch information
CahidArda committed Oct 21, 2024
1 parent 235762b commit 7aa7aa7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/deny-list/ip-deny-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, test } from "bun:test";
import { checkDenyList } from "./deny-list";
import { disableIpDenyList, updateIpDenyList } from "./ip-deny-list";
import { DenyListExtension, IpDenyListKey, IpDenyListStatusKey } from "../types";
import { Ratelimit } from "..";

describe("should update ip deny list status", async () => {
const redis = Redis.fromEnv();
Expand Down Expand Up @@ -177,4 +178,35 @@ describe("should only allow threshold values from 1 to 8", async () => {
expect(error.name).toEqual("ThresholdError")
}
})
})

test.only("should be able to use ratelimit with deny list", async () => {

/**
* When enableProtection is set to true, there was an error in the
* @upstash/ratelimit 2.0.3 release. Because the @upstash/redis
* uses auto-pipelining by default, the client would send the
* EVALSHA and the protection EVAL at the same time.
*
* When the db loses its scripts after a SCRIPT FLUSH or when
* ratelimit is used in a new DB, the EVALSHA will fail. But
* because since the EVAL and EVALSHA are pipelined, they will
* both fail and EVAL will get the EVALSHA's error.
*/
const redis = Redis.fromEnv({ enableAutoPipelining: true });

// flush the db to make sure
await redis.scriptFlush()
// sleep for two secs
await new Promise(r => setTimeout(r, 2000));

const ratelimit = new Ratelimit({
limiter: Ratelimit.fixedWindow(2, "1s"),
redis,
enableProtection: true
})

const { success, remaining } = await ratelimit.limit("ip-deny-list")
expect(success).toBeTrue()
expect(remaining).toBe(1)
})
})
1 change: 1 addition & 0 deletions src/deny-list/ip-deny-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const updateIpDenyList = async (

// delete the old ip deny list and create new one
transaction.del(ipDenyList)
// @ts-expect-error ...allIps type works but gets type error
transaction.sadd(ipDenyList, ...allIps)

// make all deny list and ip deny list disjoint by removing duplicate
Expand Down
24 changes: 24 additions & 0 deletions src/hash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Redis } from "@upstash/redis";
import { describe, test } from "bun:test";
import { safeEval } from "./hash";
import { SCRIPTS } from "./lua-scripts/hash";

const redis = Redis.fromEnv();

describe("should set hash correctly", () => {
test("should set hash in new db correctly", async () => {
await redis.scriptFlush()

// sleep for two secs
await new Promise(r => setTimeout(r, 2000));

await safeEval(
{
redis
},
SCRIPTS.singleRegion.fixedWindow.limit,
["id"],
[10, 1]
)
})
})

0 comments on commit 7aa7aa7

Please sign in to comment.