From eed09c8fd85727b4d2106198e53add8d770a338d Mon Sep 17 00:00:00 2001 From: BlowaterNostr Date: Wed, 12 Jun 2024 16:26:05 +0800 Subject: [PATCH 1/4] better tests --- deploy/example.ts | 4 +- main.tsx | 2 +- makefile | 4 +- tests/access_policy.ts | 0 test.ts => tests/test.ts | 105 +++++++++++++++++++-------------------- 5 files changed, 56 insertions(+), 59 deletions(-) create mode 100644 tests/access_policy.ts rename test.ts => tests/test.ts (85%) diff --git a/deploy/example.ts b/deploy/example.ts index 526d2ac..0c53da7 100644 --- a/deploy/example.ts +++ b/deploy/example.ts @@ -5,9 +5,7 @@ const relay = await run({ default_policy: { allowed_kinds: "all", }, - default_information: { - auth_required: false, - }, + auth_required: true, }); if (relay instanceof Error) { console.error(relay); diff --git a/main.tsx b/main.tsx index 39723da..72eb12d 100644 --- a/main.tsx +++ b/main.tsx @@ -79,7 +79,7 @@ export type Relay = { [Symbol.asyncDispose]: () => Promise; }; -const ENV_relayed_pubkey = "relayed_pubkey"; +export const ENV_relayed_pubkey = "relayed_pubkey"; export async function run(args: { port?: number; diff --git a/makefile b/makefile index 5c1275d..d0cad37 100644 --- a/makefile +++ b/makefile @@ -15,9 +15,9 @@ test: fmt --trace-leaks --unstable-kv \ --allow-read=queries,test.sqlite,relayed.db,relayed.db-journal \ --allow-net --allow-write --allow-ffi \ - --allow-env=DENO_DEPLOYMENT_ID,DENO_DIR,HOME \ + --allow-env=DENO_DEPLOYMENT_ID,DENO_DIR,HOME,relayed_pubkey \ --coverage \ - test.ts + tests/*.ts cov: deno coverage coverage --html diff --git a/tests/access_policy.ts b/tests/access_policy.ts new file mode 100644 index 0000000..e69de29 diff --git a/test.ts b/tests/test.ts similarity index 85% rename from test.ts rename to tests/test.ts index 46e1a03..4343e78 100644 --- a/test.ts +++ b/tests/test.ts @@ -1,22 +1,22 @@ // deno-lint-ignore-file no-empty -import { Relay, run, software, supported_nips } from "./main.tsx"; +import { ENV_relayed_pubkey, Relay, run, software, supported_nips } from "../main.tsx"; import { assertEquals } from "https://deno.land/std@0.202.0/assert/assert_equals.ts"; import { assertIsError, assertNotInstanceOf } from "https://deno.land/std@0.202.0/assert/mod.ts"; import { fail } from "https://deno.land/std@0.202.0/assert/fail.ts"; -import * as client_test from "./nostr.ts/relay-single-test.ts"; -import { ChannelCreation, ChannelEdition } from "./events.ts"; -import { Kind_V2 } from "./events.ts"; +import * as client_test from "../nostr.ts/relay-single-test.ts"; +import { ChannelCreation, ChannelEdition } from "../events.ts"; +import { Kind_V2 } from "../events.ts"; import { InMemoryAccountContext, NostrKind, RelayResponse_Event, sign_event_v2, Signer, -} from "./nostr.ts/nostr.ts"; -import { prepareNormalNostrEvent } from "./nostr.ts/event.ts"; -import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "./nostr.ts/relay-single.ts"; -import { PrivateKey } from "./nostr.ts/key.ts"; +} from "../nostr.ts/nostr.ts"; +import { prepareNormalNostrEvent } from "../nostr.ts/event.ts"; +import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "../nostr.ts/relay-single.ts"; +import { PrivateKey, PublicKey } from "../nostr.ts/key.ts"; import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts"; const test_kv = async () => { @@ -84,41 +84,6 @@ Deno.test({ const event_got_2 = await client.getEvent(event_sent.id); assertEquals(event_got_2, event_sent); } - // { - // await relay.set_policy({ - // kind: NostrKind.CONTACTS, - // read: true, - // write: true, - // }); - // const event_1 = await randomEvent(ctx, NostrKind.CONTACTS, "1"); - // const event_2 = await randomEvent(ctx, NostrKind.CONTACTS, "2"); - // const event_3 = await randomEvent(ctx, NostrKind.CONTACTS, "3"); - - // const err_1 = await client.sendEvent(event_1); - // if (err_1 instanceof Error) fail(err_1.message); - - // const err_2 = await client.sendEvent(event_2); - // if (err_2 instanceof Error) fail(err_2.message); - - // const err_3 = await client.sendEvent(event_3); - // if (err_3 instanceof Error) fail(err_3.message); - - // const stream = await client.newSub("get kind 3", { - // kinds: [NostrKind.CONTACTS], - // }) as SubscriptionStream; - - // const events: NostrEvent[] = []; - - // for await (const msg of stream.chan) { - // if (msg.type == "EVENT") { - // events.push(msg.event); - // } else if (msg.type == "EOSE") { - // await stream.chan.close(); - // } - // } - - // assertEquals(events.length, 3); - // } { const ctx1 = InMemoryAccountContext.Generate(); const event_1 = await randomEvent(ctx1, NostrKind.TEXT_NOTE, "test:main 1"); @@ -281,6 +246,50 @@ Deno.test({ name: "NIP-11: Relay Information Document", // ignore: true, fn: async (t) => { + await t.step("get relay information", async (t) => { + await t.step("admin from the argument", async () => { + await using relay = await run({ + default_policy: { + allowed_kinds: "none", + }, + default_information: { + name: "Nostr Relay", + }, + auth_required: false, + admin: test_ctx.publicKey, + kv: await test_kv(), + }) as Relay; + const information = await relay.get_relay_information(); + assertEquals(information, { + name: "Nostr Relay", + pubkey: test_ctx.publicKey, + software, + supported_nips, + }); + }); + await t.step("admin from the env var", async () => { + const key = PrivateKey.Generate(); + Deno.env.set(ENV_relayed_pubkey, key.toPublicKey().hex); + await using relay = await run({ + default_policy: { + allowed_kinds: "none", + }, + default_information: { + name: "Nostr Relay", + }, + auth_required: false, + kv: await test_kv(), + }) as Relay; + const information = await relay.get_relay_information(); + assertEquals(information, { + name: "Nostr Relay", + pubkey: key.toPublicKey(), + software, + supported_nips, + }); + }); + }); + await using relay = await run({ default_policy: { allowed_kinds: "none", @@ -294,16 +303,6 @@ Deno.test({ // system_key: PrivateKey.Generate(), }) as Relay; - await t.step("get relay information", async () => { - const information = await relay.get_relay_information(); - assertEquals(information, { - name: "Nostr Relay", - pubkey: test_ctx.publicKey, - software, - supported_nips, - }); - }); - await t.step("set relay information", async () => { await relay.set_relay_information({ name: "Nostr Relay2", From 5ae6efc0046c306ad04d2d90b1689d26a8eb0750 Mon Sep 17 00:00:00 2001 From: BlowaterNostr Date: Wed, 12 Jun 2024 21:55:54 +0800 Subject: [PATCH 2/4] reorg --- main.tsx => main.ts | 43 +++++++++++++++++++++++-------------------- resolvers/event.ts | 6 ++++-- resolvers/nip11.ts | 2 +- resolvers/policy.ts | 2 +- routes/landing.tsx | 2 +- tests/test.ts | 2 +- ws.ts | 2 +- 7 files changed, 32 insertions(+), 27 deletions(-) rename main.tsx => main.ts (95%) diff --git a/main.tsx b/main.ts similarity index 95% rename from main.tsx rename to main.ts index 72eb12d..2dd188b 100644 --- a/main.tsx +++ b/main.ts @@ -107,7 +107,7 @@ export async function run(args: { const write_replaceable_event = write_replaceable_event_sqlite(db); const write_regular_event = write_regular_event_sqlite(db); const get_event_count = get_event_count_sqlite(db); - const get_events_by_filter = get_events_by_filter_sqlite(db); + const get_events_by_filter = get_events_by_filter_sqlite(db, args._debug || false); // Administrator Keys if (args.admin == undefined) { @@ -169,23 +169,7 @@ export async function run(args: { }, root_handler({ ...args, - is_member: ((admin: PublicKey) => async (pubkey: string) => { - const key = PublicKey.FromString(pubkey); - if (key instanceof Error) { - return key; - } - // admin is always a member - if (key.hex == admin.hex) { - return true; - } - const policy = await policyStore.resolvePolicyByKind(NostrKind.TEXT_NOTE); - if (policy instanceof Error) { - return policy; - } - const policyAllow = policy.allow.has(pubkey); - const policyBlock = policy.block.has(pubkey); - return policyAllow && !policyBlock; - })(args.admin), + is_member: is_member({admin: args.admin, policyStore}), // deletion delete_event: delete_event_sqlite(db), delete_events_from_pubkey: async () => { @@ -483,7 +467,26 @@ async function verifyToken(event: NostrEvent, relayInformationStore: RelayInform } } -// export const kv = await Deno.openKv("./test-kv"); +const is_member = (args: { + admin: PublicKey, + policyStore: PolicyStore, +}): func_IsMember => async (pubkey: string) => { + const {admin, policyStore} = args; + const key = PublicKey.FromString(pubkey); + if (key instanceof Error) { + return key; + } + if (key.hex == admin.hex) { + return true; + } + const policy = await policyStore.resolvePolicyByKind(NostrKind.TEXT_NOTE) + if(policy instanceof Error) { + return policy + } + const policyAllow = policy.allow.has(pubkey); + const policyBlock = policy.block.has(pubkey); + return policyAllow && !policyBlock; +} const graphiql = `