Add regexPolicy, update other tests to use assertEquals

This commit is contained in:
Alex Gleason 2023-03-26 17:02:59 -05:00
parent d38f5e06cf
commit 2aa05dc194
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
10 changed files with 61 additions and 21 deletions

View File

@ -9,6 +9,7 @@ import {
pubkeyBanPolicy, pubkeyBanPolicy,
rateLimitPolicy, rateLimitPolicy,
readStdin, readStdin,
regexPolicy,
writeStdout, writeStdout,
} from './mod.ts'; } from './mod.ts';
@ -20,6 +21,7 @@ for await (const msg of readStdin()) {
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }], [rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
[pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']], [pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']],
[keywordPolicy, ['https://t.me/']], [keywordPolicy, ['https://t.me/']],
[regexPolicy, /(🟠|🔥|😳)ChtaGPT/i],
]); ]);
writeStdout(result); writeStdout(result);

1
mod.ts
View File

@ -5,6 +5,7 @@ export { default as noopPolicy } from './src/policies/noop-policy.ts';
export { default as pubkeyBanPolicy } from './src/policies/pubkey-ban-policy.ts'; export { default as pubkeyBanPolicy } from './src/policies/pubkey-ban-policy.ts';
export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts'; export { default as rateLimitPolicy } from './src/policies/rate-limit-policy.ts';
export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts'; export { default as readOnlyPolicy } from './src/policies/read-only-policy.ts';
export { default as regexPolicy } from './src/policies/regex-policy.ts';
export { default as whitelistPolicy } from './src/policies/whitelist-policy.ts'; export { default as whitelistPolicy } from './src/policies/whitelist-policy.ts';
export { readStdin, writeStdout } from './src/io.ts'; export { readStdin, writeStdout } from './src/io.ts';

View File

@ -1,4 +1,4 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts'; import { buildEvent, buildInputMessage } from '../test.ts';
import hellthreadPolicy from './hellthread-policy.ts'; import hellthreadPolicy from './hellthread-policy.ts';
@ -9,6 +9,6 @@ Deno.test('blocks events with too many mentioned users', async () => {
const msg0 = buildInputMessage(); const msg0 = buildInputMessage();
const msg1 = buildInputMessage({ event: buildEvent({ tags }) }); const msg1 = buildInputMessage({ event: buildEvent({ tags }) });
assert((await hellthreadPolicy(msg0, { limit: 1 })).action === 'accept'); assertEquals((await hellthreadPolicy(msg0, { limit: 1 })).action, 'accept');
assert((await hellthreadPolicy(msg1, { limit: 1 })).action === 'reject'); assertEquals((await hellthreadPolicy(msg1, { limit: 1 })).action, 'reject');
}); });

View File

@ -1,4 +1,4 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts'; import { buildEvent, buildInputMessage } from '../test.ts';
import keywordPolicy from './keyword-policy.ts'; import keywordPolicy from './keyword-policy.ts';
@ -9,7 +9,7 @@ Deno.test('blocks banned pubkeys', async () => {
const msg0 = buildInputMessage(); const msg0 = buildInputMessage();
const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) }); const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) });
assert((await keywordPolicy(msg0, words)).action === 'accept'); assertEquals((await keywordPolicy(msg0, words)).action, 'accept');
assert((await keywordPolicy(msg1, words)).action === 'reject'); assertEquals((await keywordPolicy(msg1, words)).action, 'reject');
assert((await keywordPolicy(msg1, [])).action === 'accept'); assertEquals((await keywordPolicy(msg1, [])).action, 'accept');
}); });

View File

@ -1,9 +1,9 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildInputMessage } from '../test.ts'; import { buildInputMessage } from '../test.ts';
import noopPolicy from './noop-policy.ts'; import noopPolicy from './noop-policy.ts';
Deno.test('allows events', async () => { Deno.test('allows events', async () => {
const msg = buildInputMessage(); const msg = buildInputMessage();
assert((await noopPolicy(msg)).action === 'accept'); assertEquals((await noopPolicy(msg)).action, 'accept');
}); });

View File

@ -1,4 +1,4 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts'; import { buildEvent, buildInputMessage } from '../test.ts';
import pubkeyBanPolicy from './pubkey-ban-policy.ts'; import pubkeyBanPolicy from './pubkey-ban-policy.ts';
@ -8,8 +8,8 @@ Deno.test('blocks banned pubkeys', async () => {
const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) }); const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) });
const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) }); const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
assert((await pubkeyBanPolicy(msgA, [])).action === 'accept'); assertEquals((await pubkeyBanPolicy(msgA, [])).action, 'accept');
assert((await pubkeyBanPolicy(msgA, ['A'])).action === 'reject'); assertEquals((await pubkeyBanPolicy(msgA, ['A'])).action, 'reject');
assert((await pubkeyBanPolicy(msgC, ['B', 'A'])).action === 'accept'); assertEquals((await pubkeyBanPolicy(msgC, ['B', 'A'])).action, 'accept');
assert((await pubkeyBanPolicy(msgB, ['B', 'A'])).action === 'reject'); assertEquals((await pubkeyBanPolicy(msgB, ['B', 'A'])).action, 'reject');
}); });

View File

@ -1,4 +1,4 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildInputMessage } from '../test.ts'; import { buildInputMessage } from '../test.ts';
import readOnlyPolicy from './read-only-policy.ts'; import readOnlyPolicy from './read-only-policy.ts';
@ -6,5 +6,5 @@ import readOnlyPolicy from './read-only-policy.ts';
Deno.test('always rejects', async () => { Deno.test('always rejects', async () => {
const msg = buildInputMessage(); const msg = buildInputMessage();
const result = await readOnlyPolicy(msg); const result = await readOnlyPolicy(msg);
assert(result.action === 'reject'); assertEquals(result.action, 'reject');
}); });

View File

@ -0,0 +1,15 @@
import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts';
import regexPolicy from './regex-policy.ts';
Deno.test('blocks banned regular expressions', async () => {
const msg = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) });
assertEquals((await regexPolicy(msg)).action, 'accept');
assertEquals((await regexPolicy(msg, /https:\/\/t\.me\/\w+/i)).action, 'reject');
assertEquals((await regexPolicy(msg, /🔥{1,3}/)).action, 'reject');
assertEquals((await regexPolicy(msg, /🔥{4}/)).action, 'accept');
assertEquals((await regexPolicy(msg, /🔥$/)).action, 'accept');
assertEquals((await regexPolicy(msg, /^🔥/)).action, 'reject');
});

View File

@ -0,0 +1,22 @@
import { Policy } from '../types.ts';
/** Reject events whose content matches the regex. */
const regexPolicy: Policy<RegExp> = ({ event: { id, content } }, regex) => {
const isMatch = regex ? regex.test(content) : false;
if (isMatch) {
return {
id,
action: 'reject',
msg: 'Event matches a banned expression.',
};
}
return {
id,
action: 'accept',
msg: '',
};
};
export default regexPolicy;

View File

@ -1,4 +1,4 @@
import { assert } from '../deps.ts'; import { assertEquals } from '../deps.ts';
import { buildEvent, buildInputMessage } from '../test.ts'; import { buildEvent, buildInputMessage } from '../test.ts';
import whitelistPolicy from './whitelist-policy.ts'; import whitelistPolicy from './whitelist-policy.ts';
@ -8,8 +8,8 @@ Deno.test('allows only whitelisted pubkeys', async () => {
const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) }); const msgB = buildInputMessage({ event: buildEvent({ pubkey: 'B' }) });
const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) }); const msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
assert((await whitelistPolicy(msgA, [])).action === 'reject'); assertEquals((await whitelistPolicy(msgA, [])).action, 'reject');
assert((await whitelistPolicy(msgA, ['A'])).action === 'accept'); assertEquals((await whitelistPolicy(msgA, ['A'])).action, 'accept');
assert((await whitelistPolicy(msgC, ['B', 'A'])).action === 'reject'); assertEquals((await whitelistPolicy(msgC, ['B', 'A'])).action, 'reject');
assert((await whitelistPolicy(msgB, ['B', 'A'])).action === 'accept'); assertEquals((await whitelistPolicy(msgB, ['B', 'A'])).action, 'accept');
}); });