From ebeec2ccba1ab18c6b62a3ac0aeb6758df4604ff Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 10 May 2024 11:26:15 -0300 Subject: [PATCH] test: MuteListPolicy with 100.00% code coverage --- src/policies/MuteListPolicy.test.ts | 72 +++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/policies/MuteListPolicy.test.ts diff --git a/src/policies/MuteListPolicy.test.ts b/src/policies/MuteListPolicy.test.ts new file mode 100644 index 0000000..69561f8 --- /dev/null +++ b/src/policies/MuteListPolicy.test.ts @@ -0,0 +1,72 @@ +import { MockRelay } from '@nostrify/nostrify/test'; + +import { assertEquals } from '@/deps-test.ts'; +import { UserStore } from '@/storages/UserStore.ts'; +import { MuteListPolicy } from '@/policies/MuteListPolicy.ts'; + +import userBlack from '~/fixtures/events/kind-0-black.json' with { type: 'json' }; +import userMe from '~/fixtures/events/event-0-makes-repost-with-quote-repost.json' with { type: 'json' }; +import blockEvent from '~/fixtures/events/kind-10000-black-blocks-user-me.json' with { type: 'json' }; +import event1authorUserMe from '~/fixtures/events/event-1-quote-repost-will-be-reposted.json' with { type: 'json' }; +import event1 from '~/fixtures/events/event-1.json' with { type: 'json' }; + +Deno.test('block event: muted user cannot post', async () => { + const userBlackCopy = structuredClone(userBlack); + const userMeCopy = structuredClone(userMe); + const blockEventCopy = structuredClone(blockEvent); + const event1authorUserMeCopy = structuredClone(event1authorUserMe); + + const db = new MockRelay(); + + const store = new UserStore(userBlackCopy.pubkey, db); + const policy = new MuteListPolicy(userBlack.pubkey, db); + + await store.event(blockEventCopy); + await store.event(userBlackCopy); + await store.event(userMeCopy); + + const ok = await policy.call(event1authorUserMeCopy); + + assertEquals(ok, ['OK', event1authorUserMeCopy.id, false, 'You are banned in this server.']); +}); + +Deno.test('allow event: user is NOT muted because there is no muted event', async () => { + const userBlackCopy = structuredClone(userBlack); + const userMeCopy = structuredClone(userMe); + const event1authorUserMeCopy = structuredClone(event1authorUserMe); + + const db = new MockRelay(); + + const store = new UserStore(userBlackCopy.pubkey, db); + const policy = new MuteListPolicy(userBlack.pubkey, db); + + await store.event(userBlackCopy); + await store.event(userMeCopy); + + const ok = await policy.call(event1authorUserMeCopy); + + assertEquals(ok, ['OK', event1authorUserMeCopy.id, true, '']); +}); + +Deno.test('allow event: user is NOT muted because he is not in mute event', async () => { + const userBlackCopy = structuredClone(userBlack); + const userMeCopy = structuredClone(userMe); + const event1authorUserMeCopy = structuredClone(event1authorUserMe); + const blockEventCopy = structuredClone(blockEvent); + const event1copy = structuredClone(event1); + + const db = new MockRelay(); + + const store = new UserStore(userBlackCopy.pubkey, db); + const policy = new MuteListPolicy(userBlack.pubkey, db); + + await store.event(userBlackCopy); + await store.event(blockEventCopy); + await store.event(userMeCopy); + await store.event(event1copy); + await store.event(event1authorUserMeCopy); + + const ok = await policy.call(event1copy); + + assertEquals(ok, ['OK', event1.id, true, '']); +});