From dcb178cfbbe5a8eeb950e4f5a46865ef2042d13b Mon Sep 17 00:00:00 2001 From: nazeh Date: Thu, 1 Aug 2024 08:43:12 +0300 Subject: [PATCH] test(pubky): add tests for 401 and 403 responses --- pubky/pkg/test/public.js | 76 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/pubky/pkg/test/public.js b/pubky/pkg/test/public.js index 7fb4111..dd116b2 100644 --- a/pubky/pkg/test/public.js +++ b/pubky/pkg/test/public.js @@ -43,6 +43,78 @@ test('public: put/get', async (t) => { // } }) -test.skip("not found") +test("not found", async (t) => { + const client = PubkyClient.testnet(); -test.skip("unauthorized") + + const keypair = Keypair.random(); + + const homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo'); + await client.signup(keypair, homeserver); + + const publicKey = keypair.public_key(); + + let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`; + + let result = await client.get(url).catch(e => e); + + t.ok(result instanceof Error); + t.is( + result.message, + `HTTP status client error (404 Not Found) for url (http://localhost:15411/${publicKey.z32()}/pub/example.com/arbitrary)` + ) +}) + +test("unauthorized", async (t) => { + const client = PubkyClient.testnet(); + + const keypair = Keypair.random() + const publicKey = keypair.public_key() + + const homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo') + await client.signup(keypair, homeserver) + + const session = await client.session(publicKey) + t.ok(session, "signup") + + await client.signout(publicKey) + + const body = Buffer.from(JSON.stringify({ foo: 'bar' })) + + let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`; + + // PUT public data, by authorized client + let result = await client.put(url, body).catch(e => e); + + t.ok(result instanceof Error); + t.is( + result.message, + `HTTP status client error (401 Unauthorized) for url (http://localhost:15411/${publicKey.z32()}/pub/example.com/arbitrary)` + ) +}) + +test("forbidden", async (t) => { + const client = PubkyClient.testnet(); + + const keypair = Keypair.random() + const publicKey = keypair.public_key() + + const homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo') + await client.signup(keypair, homeserver) + + const session = await client.session(publicKey) + t.ok(session, "signup") + + const body = Buffer.from(JSON.stringify({ foo: 'bar' })) + + let url = `pubky://${publicKey.z32()}/priv/example.com/arbitrary`; + + // PUT public data, by authorized client + let result = await client.put(url, body).catch(e => e); + + t.ok(result instanceof Error); + t.is( + result.message, + `HTTP status client error (403 Forbidden) for url (http://localhost:15411/${publicKey.z32()}/priv/example.com/arbitrary)` + ) +})