Add regexPolicy, update other tests to use assertEquals
This commit is contained in:
parent
d38f5e06cf
commit
2aa05dc194
@ -9,6 +9,7 @@ import {
|
||||
pubkeyBanPolicy,
|
||||
rateLimitPolicy,
|
||||
readStdin,
|
||||
regexPolicy,
|
||||
writeStdout,
|
||||
} from './mod.ts';
|
||||
|
||||
@ -20,6 +21,7 @@ for await (const msg of readStdin()) {
|
||||
[rateLimitPolicy, { whitelist: ['127.0.0.1'] }],
|
||||
[pubkeyBanPolicy, ['e810fafa1e89cdf80cced8e013938e87e21b699b24c8570537be92aec4b12c18']],
|
||||
[keywordPolicy, ['https://t.me/']],
|
||||
[regexPolicy, /(🟠|🔥|😳)ChtaGPT/i],
|
||||
]);
|
||||
|
||||
writeStdout(result);
|
||||
|
1
mod.ts
1
mod.ts
@ -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 rateLimitPolicy } from './src/policies/rate-limit-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 { readStdin, writeStdout } from './src/io.ts';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildEvent, buildInputMessage } from '../test.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 msg1 = buildInputMessage({ event: buildEvent({ tags }) });
|
||||
|
||||
assert((await hellthreadPolicy(msg0, { limit: 1 })).action === 'accept');
|
||||
assert((await hellthreadPolicy(msg1, { limit: 1 })).action === 'reject');
|
||||
assertEquals((await hellthreadPolicy(msg0, { limit: 1 })).action, 'accept');
|
||||
assertEquals((await hellthreadPolicy(msg1, { limit: 1 })).action, 'reject');
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildEvent, buildInputMessage } from '../test.ts';
|
||||
|
||||
import keywordPolicy from './keyword-policy.ts';
|
||||
@ -9,7 +9,7 @@ Deno.test('blocks banned pubkeys', async () => {
|
||||
const msg0 = buildInputMessage();
|
||||
const msg1 = buildInputMessage({ event: buildEvent({ content: '🔥🔥🔥 https://t.me/spam 我想死' }) });
|
||||
|
||||
assert((await keywordPolicy(msg0, words)).action === 'accept');
|
||||
assert((await keywordPolicy(msg1, words)).action === 'reject');
|
||||
assert((await keywordPolicy(msg1, [])).action === 'accept');
|
||||
assertEquals((await keywordPolicy(msg0, words)).action, 'accept');
|
||||
assertEquals((await keywordPolicy(msg1, words)).action, 'reject');
|
||||
assertEquals((await keywordPolicy(msg1, [])).action, 'accept');
|
||||
});
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildInputMessage } from '../test.ts';
|
||||
|
||||
import noopPolicy from './noop-policy.ts';
|
||||
|
||||
Deno.test('allows events', async () => {
|
||||
const msg = buildInputMessage();
|
||||
assert((await noopPolicy(msg)).action === 'accept');
|
||||
assertEquals((await noopPolicy(msg)).action, 'accept');
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildEvent, buildInputMessage } from '../test.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 msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
|
||||
|
||||
assert((await pubkeyBanPolicy(msgA, [])).action === 'accept');
|
||||
assert((await pubkeyBanPolicy(msgA, ['A'])).action === 'reject');
|
||||
assert((await pubkeyBanPolicy(msgC, ['B', 'A'])).action === 'accept');
|
||||
assert((await pubkeyBanPolicy(msgB, ['B', 'A'])).action === 'reject');
|
||||
assertEquals((await pubkeyBanPolicy(msgA, [])).action, 'accept');
|
||||
assertEquals((await pubkeyBanPolicy(msgA, ['A'])).action, 'reject');
|
||||
assertEquals((await pubkeyBanPolicy(msgC, ['B', 'A'])).action, 'accept');
|
||||
assertEquals((await pubkeyBanPolicy(msgB, ['B', 'A'])).action, 'reject');
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildInputMessage } from '../test.ts';
|
||||
|
||||
import readOnlyPolicy from './read-only-policy.ts';
|
||||
@ -6,5 +6,5 @@ import readOnlyPolicy from './read-only-policy.ts';
|
||||
Deno.test('always rejects', async () => {
|
||||
const msg = buildInputMessage();
|
||||
const result = await readOnlyPolicy(msg);
|
||||
assert(result.action === 'reject');
|
||||
assertEquals(result.action, 'reject');
|
||||
});
|
||||
|
15
src/policies/regex-policy.test.ts
Normal file
15
src/policies/regex-policy.test.ts
Normal 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');
|
||||
});
|
22
src/policies/regex-policy.ts
Normal file
22
src/policies/regex-policy.ts
Normal 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;
|
@ -1,4 +1,4 @@
|
||||
import { assert } from '../deps.ts';
|
||||
import { assertEquals } from '../deps.ts';
|
||||
import { buildEvent, buildInputMessage } from '../test.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 msgC = buildInputMessage({ event: buildEvent({ pubkey: 'C' }) });
|
||||
|
||||
assert((await whitelistPolicy(msgA, [])).action === 'reject');
|
||||
assert((await whitelistPolicy(msgA, ['A'])).action === 'accept');
|
||||
assert((await whitelistPolicy(msgC, ['B', 'A'])).action === 'reject');
|
||||
assert((await whitelistPolicy(msgB, ['B', 'A'])).action === 'accept');
|
||||
assertEquals((await whitelistPolicy(msgA, [])).action, 'reject');
|
||||
assertEquals((await whitelistPolicy(msgA, ['A'])).action, 'accept');
|
||||
assertEquals((await whitelistPolicy(msgC, ['B', 'A'])).action, 'reject');
|
||||
assertEquals((await whitelistPolicy(msgB, ['B', 'A'])).action, 'accept');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user