Remove old testApp

This commit is contained in:
Alex Gleason 2025-03-03 14:46:05 -06:00
parent 9be9f7c9d0
commit 0dd085b559
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 30 additions and 54 deletions

View file

@ -1,21 +1,26 @@
import { setUser, testApp } from '@ditto/mastoapi/test'; import { TestApp } from '@ditto/mastoapi/test';
import { assertEquals } from '@std/assert'; import { assertEquals } from '@std/assert';
import { userMiddleware } from './userMiddleware.ts'; import { userMiddleware } from './userMiddleware.ts';
import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts'; import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts';
Deno.test('no user 401', async () => { Deno.test('no user 401', async () => {
const { app } = testApp(); await using app = new TestApp();
const response = await app.use(userMiddleware()).request('/'); const response = await app.use(userMiddleware()).request('/');
assertEquals(response.status, 401); assertEquals(response.status, 401);
}); });
Deno.test('unsupported signer 400', async () => { Deno.test('unsupported signer 400', async () => {
const { app, relay } = testApp(); await using app = new TestApp();
const signer = new ReadOnlySigner('0461fcbecc4c3374439932d6b8f11269ccdb7cc973ad7a50ae362db135a474dd');
const user = {
signer: new ReadOnlySigner('0461fcbecc4c3374439932d6b8f11269ccdb7cc973ad7a50ae362db135a474dd'),
relay: app.var.relay,
};
app.user(user);
const response = await app const response = await app
.use(setUser({ signer, relay }))
.use(userMiddleware({ enc: 'nip44' })) .use(userMiddleware({ enc: 'nip44' }))
.use((c, next) => { .use((c, next) => {
c.var.user.signer.nip44.encrypt; // test that the type is set c.var.user.signer.nip44.encrypt; // test that the type is set
@ -27,10 +32,11 @@ Deno.test('unsupported signer 400', async () => {
}); });
Deno.test('with user 200', async () => { Deno.test('with user 200', async () => {
const { app, user } = testApp(); await using app = new TestApp();
app.user();
const response = await app const response = await app
.use(setUser(user))
.use(userMiddleware()) .use(userMiddleware())
.get('/', (c) => c.text('ok')) .get('/', (c) => c.text('ok'))
.request('/'); .request('/');
@ -39,10 +45,11 @@ Deno.test('with user 200', async () => {
}); });
Deno.test('user and role 403', async () => { Deno.test('user and role 403', async () => {
const { app, user } = testApp(); await using app = new TestApp();
app.user();
const response = await app const response = await app
.use(setUser(user))
.use(userMiddleware({ role: 'admin' })) .use(userMiddleware({ role: 'admin' }))
.request('/'); .request('/');
@ -50,7 +57,10 @@ Deno.test('user and role 403', async () => {
}); });
Deno.test('admin role 200', async () => { Deno.test('admin role 200', async () => {
const { conf, app, user, relay } = testApp(); await using app = new TestApp();
const { conf, relay } = app.var;
const user = app.user();
const event = await conf.signer.signEvent({ const event = await conf.signer.signEvent({
kind: 30382, kind: 30382,
@ -65,7 +75,6 @@ Deno.test('admin role 200', async () => {
await relay.event(event); await relay.event(event);
const response = await app const response = await app
.use(setUser(user))
.use(userMiddleware({ role: 'admin' })) .use(userMiddleware({ role: 'admin' }))
.get('/', (c) => c.text('ok')) .get('/', (c) => c.text('ok'))
.request('/'); .request('/');

View file

@ -1,43 +1 @@
import { DittoConf } from '@ditto/conf';
import { type DittoDB, DummyDB } from '@ditto/db';
import { DittoApp, type DittoMiddleware } from '@ditto/mastoapi/router';
import { type NostrSigner, type NRelay, NSecSigner } from '@nostrify/nostrify';
import { MockRelay } from '@nostrify/nostrify/test';
import { generateSecretKey, nip19 } from 'nostr-tools';
import type { User } from '@ditto/mastoapi/middleware';
export { TestApp } from './test/TestApp.ts'; export { TestApp } from './test/TestApp.ts';
export function testApp(): {
app: DittoApp;
relay: NRelay;
conf: DittoConf;
db: DittoDB;
user: {
signer: NostrSigner;
relay: NRelay;
};
} {
const db = new DummyDB();
const nsec = nip19.nsecEncode(generateSecretKey());
const conf = new DittoConf(new Map([['DITTO_NSEC', nsec]]));
const relay = new MockRelay();
const app = new DittoApp({ conf, relay, db });
const user = {
signer: new NSecSigner(generateSecretKey()),
relay,
};
return { app, relay, conf, db, user };
}
export function setUser<S extends NostrSigner>(user: User<S>): DittoMiddleware<{ user: User<S> }> {
return async (c, next) => {
c.set('user', user);
await next();
};
}

View file

@ -1,5 +1,6 @@
import { DittoConf } from '@ditto/conf'; import { DittoConf } from '@ditto/conf';
import { type DittoDB, DummyDB } from '@ditto/db'; import { type DittoDB, DummyDB } from '@ditto/db';
import { HTTPException } from '@hono/hono/http-exception';
import { type NRelay, NSecSigner } from '@nostrify/nostrify'; import { type NRelay, NSecSigner } from '@nostrify/nostrify';
import { generateSecretKey, nip19 } from 'nostr-tools'; import { generateSecretKey, nip19 } from 'nostr-tools';
@ -43,7 +44,15 @@ export class TestApp extends DittoApp implements AsyncDisposable {
await next(); await next();
}); });
this.onError((err) => { this.onError((err, c) => {
if (err instanceof HTTPException) {
if (err.res) {
return err.res;
} else {
return c.json({ error: err.message }, err.status);
}
}
throw err; throw err;
}); });
} }