From c1852ddd56df8d7499fb94987a21d1a6e1f83cc0 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 26 Mar 2023 16:23:03 -0500 Subject: [PATCH] Add test for rateLimitPolicy --- src/policies/rate-limit-policy.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/policies/rate-limit-policy.test.ts diff --git a/src/policies/rate-limit-policy.test.ts b/src/policies/rate-limit-policy.test.ts new file mode 100644 index 0000000..91f33d3 --- /dev/null +++ b/src/policies/rate-limit-policy.test.ts @@ -0,0 +1,25 @@ +import { assertEquals } from '../deps.ts'; +import { buildEvent, buildInputMessage } from '../test.ts'; + +import rateLimitPolicy from './rate-limit-policy.ts'; + +Deno.test({ + name: 'blocks events from IPs that are publishing events too quickly', + fn: async () => { + const opts = { + max: 4, + databaseUrl: `sqlite:///tmp/${crypto.randomUUID()}.sqlite3`, + }; + + const msg = buildInputMessage({ sourceType: 'IP4', sourceInfo: '1.1.1.1', event: buildEvent() }); + + assertEquals((await rateLimitPolicy(msg, opts)).action, 'accept'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'accept'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'accept'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'accept'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'reject'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'reject'); + assertEquals((await rateLimitPolicy(msg, opts)).action, 'reject'); + }, + sanitizeResources: false, +});