Skip to content

Commit

Permalink
feat(js): pass all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Dec 17, 2024
1 parent 992607a commit f42797a
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 150 deletions.
40 changes: 14 additions & 26 deletions pubky/pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`;
// Verify that you are signed in.
const session = await client.session(publicKey)

const body = Buffer.from(JSON.stringify({ foo: 'bar' }))

// PUT public data, by authorized client
await client.put(url, body);
await client.fetch(url, {
method: "PUT",
body: JSON.stringify({foo: "bar"}),
credentials: "include"
});

// GET public data without signup or signin
{
const client = new Client();

let response = await client.get(url);
let response = await client.fetch(url);
}

// Delete public data, by authorized client
await client.delete(url);
await client.fetch(url, { method: "DELETE", credentials: "include "});
```

## API
Expand All @@ -67,6 +69,13 @@ await client.delete(url);
let client = new Client()
```

#### fetch
```js
let response = await client.fetch(url, opts);
```

Just like normal Fetch API, but it can handle `pubky://` urls and `http(s)://` urls with Pkarr domains.

#### signup
```js
await client.signup(keypair, homeserver)
Expand Down Expand Up @@ -127,27 +136,6 @@ let session = await client.session(publicKey)
- publicKey: An instance of [PublicKey](#publickey).
- Returns: A [Session](#session) object if signed in, or undefined if not.

#### put
```js
let response = await client.put(url, body);
```
- url: A string representing the Pubky URL.
- body: A Buffer containing the data to be stored.

### get
```js
let response = await client.get(url)
```
- url: A string representing the Pubky URL.
- Returns: A Uint8Array object containing the requested data, or `undefined` if `NOT_FOUND`.

### delete

```js
let response = await client.delete(url);
```
- url: A string representing the Pubky URL.

### list
```js
let response = await client.list(url, cursor, reverse, limit)
Expand Down
88 changes: 53 additions & 35 deletions pubky/pkg/test/public.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'tape'

import { Client, Keypair, PublicKey } from '../index.cjs'
import { Client, Keypair, PublicKey ,setLogLevel} from '../index.cjs'

const HOMESERVER_PUBLICKEY = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo')

test.skip('public: put/get', async (t) => {
test('public: put/get', async (t) => {
const client = Client.testnet();

const keypair = Keypair.random();
Expand All @@ -15,33 +15,43 @@ test.skip('public: put/get', async (t) => {

let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`;

const body = Buffer.from(JSON.stringify({ foo: 'bar' }))
const json = { foo: 'bar' }

// PUT public data, by authorized client
await client.put(url, body);
await client.fetch(url, {
method:"PUT",
body: JSON.stringify(json),
contentType: "json",
credentials: "include"
});

const otherClient = Client.testnet();

// GET public data without signup or signin
{
let response = await otherClient.get(url);
let response = await otherClient.fetch(url)

t.ok(Buffer.from(response).equals(body))
t.is(response.status, 200);

t.deepEquals(await response.json(), {foo: "bar"})
}

// DELETE public data, by authorized client
await client.delete(url);
await client.fetch(url, {
method:"DELETE",
credentials: "include"
});


// GET public data without signup or signin
{
let response = await otherClient.get(url);
let response = await otherClient.fetch(url);

t.notOk(response)
t.is(response.status, 404)
}
})

test.skip("not found", async (t) => {
test("not found", async (t) => {
const client = Client.testnet();


Expand All @@ -53,12 +63,12 @@ test.skip("not found", async (t) => {

let url = `pubky://${publicKey.z32()}/pub/example.com/arbitrary`;

let result = await client.get(url).catch(e => e);
let result = await client.fetch(url);

t.notOk(result);
t.is(result.status, 404);
})

test.skip("unauthorized", async (t) => {
test("unauthorized", async (t) => {
const client = Client.testnet();

const keypair = Keypair.random()
Expand All @@ -71,21 +81,20 @@ test.skip("unauthorized", async (t) => {

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)`
)
let response = await client.fetch(url, {
method: "PUT",
body: JSON.stringify({ foo: 'bar' }),
contentType: "json",
credentials: "include"
});

t.equals(response.status,401);
})

test.skip("forbidden", async (t) => {
test("forbidden", async (t) => {
const client = Client.testnet();

const keypair = Keypair.random()
Expand All @@ -96,21 +105,22 @@ test.skip("forbidden", async (t) => {
const session = await client.session(publicKey)
t.ok(session, "signup")

const body = Buffer.from(JSON.stringify({ foo: 'bar' }))
const body = (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)`
)
let response = await client.fetch(url, {
method: "PUT",
body: JSON.stringify({ foo: 'bar' }),
credentials: "include"
});

t.is(response.status, 403)
t.is(await response.text(), 'Writing to directories other than \'/pub/\' is forbidden')
})

test.skip("list", async (t) => {
test("list", async (t) => {
const client = Client.testnet();

const keypair = Keypair.random()
Expand All @@ -132,7 +142,11 @@ test.skip("list", async (t) => {
]

for (let url of urls) {
await client.put(url, Buffer.from(""));
await client.fetch(url, {
method: "PUT",
body:Buffer.from(""),
credentials: "include"
});
}

let url = `pubky://${pubky}/pub/example.com/`;
Expand Down Expand Up @@ -241,7 +255,7 @@ test.skip("list", async (t) => {
}
})

test.skip('list shallow', async (t) => {
test('list shallow', async (t) => {
const client = Client.testnet();

const keypair = Keypair.random()
Expand All @@ -264,7 +278,11 @@ test.skip('list shallow', async (t) => {
]

for (let url of urls) {
await client.put(url, Buffer.from(""));
await client.fetch(url, {
method: "PUT",
body: Buffer.from(""),
credentials: "include"
});
}

let url = `pubky://${pubky}/pub/`;
Expand Down
1 change: 0 additions & 1 deletion pubky/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::Client;
mod api;
mod cookies;
mod http;
mod internal;

pub(crate) use cookies::CookieJar;

Expand Down
6 changes: 6 additions & 0 deletions pubky/src/native/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ impl Client {
pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder {
self.request(Method::HEAD, url)
}

// === Private Methods ===

pub(crate) async fn inner_request<T: IntoUrl>(&self, method: Method, url: T) -> RequestBuilder {
self.request(method, url)
}
}

#[cfg(test)]
Expand Down
12 changes: 0 additions & 12 deletions pubky/src/native/internal.rs

This file was deleted.

1 change: 0 additions & 1 deletion pubky/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::Client;

mod api;
mod http;
mod internals;
mod wrappers;

impl Default for Client {
Expand Down
Loading

0 comments on commit f42797a

Please sign in to comment.