Add test for rateLimitPolicy

This commit is contained in:
Alex Gleason 2023-03-26 16:23:03 -05:00
parent a809da4249
commit c1852ddd56
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

View File

@ -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,
});