mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Fix cashu tests, sorta
This commit is contained in:
parent
e1bf86eb21
commit
33786d2e5d
2 changed files with 33 additions and 76 deletions
|
|
@ -1,53 +1,39 @@
|
||||||
import { Env as HonoEnv, Hono } from '@hono/hono';
|
import { DittoConf } from '@ditto/conf';
|
||||||
import { NostrSigner, NSecSigner, NStore } from '@nostrify/nostrify';
|
import { DittoApp } from '@ditto/router';
|
||||||
|
import { NSecSigner } from '@nostrify/nostrify';
|
||||||
import { genEvent } from '@nostrify/nostrify/test';
|
import { genEvent } from '@nostrify/nostrify/test';
|
||||||
import { bytesToString, stringToBytes } from '@scure/base';
|
import { bytesToString, stringToBytes } from '@scure/base';
|
||||||
import { stub } from '@std/testing/mock';
|
import { stub } from '@std/testing/mock';
|
||||||
import { assertEquals, assertExists, assertObjectMatch } from '@std/assert';
|
import { assertEquals, assertExists, assertObjectMatch } from '@std/assert';
|
||||||
import { generateSecretKey, getPublicKey } from 'nostr-tools';
|
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
||||||
|
|
||||||
import { createTestDB } from '@/test.ts';
|
import { createTestDB } from '@/test.ts';
|
||||||
|
|
||||||
import cashuApp from '@/controllers/api/cashu.ts';
|
import cashuApp from '@/controllers/api/cashu.ts';
|
||||||
import { walletSchema } from '@/schema.ts';
|
import { walletSchema } from '@/schema.ts';
|
||||||
|
|
||||||
interface AppEnv extends HonoEnv {
|
|
||||||
Variables: {
|
|
||||||
/** Signer to get the logged-in user's pubkey, relays, and to sign events. */
|
|
||||||
signer: NostrSigner;
|
|
||||||
/** Storage for the user, might filter out unwanted content. */
|
|
||||||
store: NStore;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Deno.test('PUT /wallet must be successful', {
|
Deno.test('PUT /wallet must be successful', {
|
||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
}, async () => {
|
}, async () => {
|
||||||
using _mock = mockFetch();
|
using _mock = mockFetch();
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
const store = db.store;
|
const relay = db.store;
|
||||||
|
|
||||||
const sk = generateSecretKey();
|
const sk = generateSecretKey();
|
||||||
const signer = new NSecSigner(sk);
|
const signer = new NSecSigner(sk);
|
||||||
const nostrPrivateKey = bytesToString('hex', sk);
|
const nostrPrivateKey = bytesToString('hex', sk);
|
||||||
|
|
||||||
const app = new Hono<AppEnv>().use(
|
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
|
||||||
async (c, next) => {
|
|
||||||
c.set('signer', signer);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
async (c, next) => {
|
|
||||||
c.set('store', store);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
app.route('/', cashuApp);
|
app.route('/', cashuApp);
|
||||||
|
|
||||||
const response = await app.request('/wallet', {
|
const response = await app.request('/wallet', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: [['content-type', 'application/json']],
|
headers: {
|
||||||
|
'authorization': `Bearer ${nip19.nsecEncode(sk)}`,
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
mints: [
|
mints: [
|
||||||
'https://houston.mint.com',
|
'https://houston.mint.com',
|
||||||
|
|
@ -61,7 +47,7 @@ Deno.test('PUT /wallet must be successful', {
|
||||||
|
|
||||||
const pubkey = await signer.getPublicKey();
|
const pubkey = await signer.getPublicKey();
|
||||||
|
|
||||||
const [wallet] = await store.query([{ authors: [pubkey], kinds: [17375] }]);
|
const [wallet] = await relay.query([{ authors: [pubkey], kinds: [17375] }]);
|
||||||
|
|
||||||
assertExists(wallet);
|
assertExists(wallet);
|
||||||
assertEquals(wallet.kind, 17375);
|
assertEquals(wallet.kind, 17375);
|
||||||
|
|
@ -88,7 +74,7 @@ Deno.test('PUT /wallet must be successful', {
|
||||||
]);
|
]);
|
||||||
assertEquals(data.balance, 0);
|
assertEquals(data.balance, 0);
|
||||||
|
|
||||||
const [nutzap_info] = await store.query([{ authors: [pubkey], kinds: [10019] }]);
|
const [nutzap_info] = await relay.query([{ authors: [pubkey], kinds: [10019] }]);
|
||||||
|
|
||||||
assertExists(nutzap_info);
|
assertExists(nutzap_info);
|
||||||
assertEquals(nutzap_info.kind, 10019);
|
assertEquals(nutzap_info.kind, 10019);
|
||||||
|
|
@ -105,27 +91,19 @@ Deno.test('PUT /wallet must be successful', {
|
||||||
Deno.test('PUT /wallet must NOT be successful: wrong request body/schema', async () => {
|
Deno.test('PUT /wallet must NOT be successful: wrong request body/schema', async () => {
|
||||||
using _mock = mockFetch();
|
using _mock = mockFetch();
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
const store = db.store;
|
const relay = db.store;
|
||||||
|
|
||||||
const sk = generateSecretKey();
|
const sk = generateSecretKey();
|
||||||
const signer = new NSecSigner(sk);
|
|
||||||
|
|
||||||
const app = new Hono<AppEnv>().use(
|
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
|
||||||
async (c, next) => {
|
|
||||||
c.set('signer', signer);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
async (c, next) => {
|
|
||||||
c.set('store', store);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
app.route('/', cashuApp);
|
app.route('/', cashuApp);
|
||||||
|
|
||||||
const response = await app.request('/wallet', {
|
const response = await app.request('/wallet', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: [['content-type', 'application/json']],
|
headers: {
|
||||||
|
'authorization': `Bearer ${nip19.nsecEncode(sk)}`,
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
mints: [], // no mints should throw an error
|
mints: [], // no mints should throw an error
|
||||||
}),
|
}),
|
||||||
|
|
@ -143,21 +121,10 @@ Deno.test('PUT /wallet must NOT be successful: wallet already exists', {
|
||||||
}, async () => {
|
}, async () => {
|
||||||
using _mock = mockFetch();
|
using _mock = mockFetch();
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
const store = db.store;
|
const relay = db.store;
|
||||||
|
|
||||||
const sk = generateSecretKey();
|
const sk = generateSecretKey();
|
||||||
const signer = new NSecSigner(sk);
|
|
||||||
|
|
||||||
const app = new Hono<AppEnv>().use(
|
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
|
||||||
async (c, next) => {
|
|
||||||
c.set('signer', signer);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
async (c, next) => {
|
|
||||||
c.set('store', store);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
app.route('/', cashuApp);
|
app.route('/', cashuApp);
|
||||||
|
|
||||||
|
|
@ -165,7 +132,10 @@ Deno.test('PUT /wallet must NOT be successful: wallet already exists', {
|
||||||
|
|
||||||
const response = await app.request('/wallet', {
|
const response = await app.request('/wallet', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: [['content-type', 'application/json']],
|
headers: {
|
||||||
|
'authorization': `Bearer ${nip19.nsecEncode(sk)}`,
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
mints: ['https://mint.heart.com'],
|
mints: ['https://mint.heart.com'],
|
||||||
}),
|
}),
|
||||||
|
|
@ -183,7 +153,7 @@ Deno.test('GET /wallet must be successful', {
|
||||||
}, async () => {
|
}, async () => {
|
||||||
using _mock = mockFetch();
|
using _mock = mockFetch();
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
const store = db.store;
|
const relay = db.store;
|
||||||
|
|
||||||
const sk = generateSecretKey();
|
const sk = generateSecretKey();
|
||||||
const signer = new NSecSigner(sk);
|
const signer = new NSecSigner(sk);
|
||||||
|
|
@ -191,16 +161,7 @@ Deno.test('GET /wallet must be successful', {
|
||||||
const privkey = bytesToString('hex', sk);
|
const privkey = bytesToString('hex', sk);
|
||||||
const p2pk = getPublicKey(stringToBytes('hex', privkey));
|
const p2pk = getPublicKey(stringToBytes('hex', privkey));
|
||||||
|
|
||||||
const app = new Hono<AppEnv>().use(
|
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
|
||||||
async (c, next) => {
|
|
||||||
c.set('signer', signer);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
async (c, next) => {
|
|
||||||
c.set('store', store);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
app.route('/', cashuApp);
|
app.route('/', cashuApp);
|
||||||
|
|
||||||
|
|
@ -282,6 +243,9 @@ Deno.test('GET /wallet must be successful', {
|
||||||
|
|
||||||
const response = await app.request('/wallet', {
|
const response = await app.request('/wallet', {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'authorization': `Bearer ${nip19.nsecEncode(sk)}`,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const body = await response.json();
|
const body = await response.json();
|
||||||
|
|
@ -298,14 +262,9 @@ Deno.test('GET /wallet must be successful', {
|
||||||
Deno.test('GET /mints must be successful', async () => {
|
Deno.test('GET /mints must be successful', async () => {
|
||||||
using _mock = mockFetch();
|
using _mock = mockFetch();
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
const store = db.store;
|
const relay = db.store;
|
||||||
|
|
||||||
const app = new Hono<AppEnv>().use(
|
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
|
||||||
async (c, next) => {
|
|
||||||
c.set('store', store);
|
|
||||||
await next();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
app.route('/', cashuApp);
|
app.route('/', cashuApp);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,6 @@ export function userMiddleware(opts: { privileged: boolean; required?: boolean }
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
const header = c.req.header('authorization');
|
const header = c.req.header('authorization');
|
||||||
|
|
||||||
if (!header && required) {
|
|
||||||
throw new HTTPException(403, { message: 'Authorization required.' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (header) {
|
if (header) {
|
||||||
const user: User = {
|
const user: User = {
|
||||||
signer: await getSigner(header, c.var),
|
signer: await getSigner(header, c.var),
|
||||||
|
|
@ -45,6 +41,8 @@ export function userMiddleware(opts: { privileged: boolean; required?: boolean }
|
||||||
};
|
};
|
||||||
|
|
||||||
c.set('user', user);
|
c.set('user', user);
|
||||||
|
} else if (required) {
|
||||||
|
throw new HTTPException(403, { message: 'Authorization required.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (privileged) {
|
if (privileged) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue