mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Remove old testApp
This commit is contained in:
parent
9be9f7c9d0
commit
0dd085b559
3 changed files with 30 additions and 54 deletions
|
|
@ -1,21 +1,26 @@
|
|||
import { setUser, testApp } from '@ditto/mastoapi/test';
|
||||
import { TestApp } from '@ditto/mastoapi/test';
|
||||
import { assertEquals } from '@std/assert';
|
||||
|
||||
import { userMiddleware } from './userMiddleware.ts';
|
||||
import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts';
|
||||
|
||||
Deno.test('no user 401', async () => {
|
||||
const { app } = testApp();
|
||||
await using app = new TestApp();
|
||||
const response = await app.use(userMiddleware()).request('/');
|
||||
assertEquals(response.status, 401);
|
||||
});
|
||||
|
||||
Deno.test('unsupported signer 400', async () => {
|
||||
const { app, relay } = testApp();
|
||||
const signer = new ReadOnlySigner('0461fcbecc4c3374439932d6b8f11269ccdb7cc973ad7a50ae362db135a474dd');
|
||||
await using app = new TestApp();
|
||||
|
||||
const user = {
|
||||
signer: new ReadOnlySigner('0461fcbecc4c3374439932d6b8f11269ccdb7cc973ad7a50ae362db135a474dd'),
|
||||
relay: app.var.relay,
|
||||
};
|
||||
|
||||
app.user(user);
|
||||
|
||||
const response = await app
|
||||
.use(setUser({ signer, relay }))
|
||||
.use(userMiddleware({ enc: 'nip44' }))
|
||||
.use((c, next) => {
|
||||
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 () => {
|
||||
const { app, user } = testApp();
|
||||
await using app = new TestApp();
|
||||
|
||||
app.user();
|
||||
|
||||
const response = await app
|
||||
.use(setUser(user))
|
||||
.use(userMiddleware())
|
||||
.get('/', (c) => c.text('ok'))
|
||||
.request('/');
|
||||
|
|
@ -39,10 +45,11 @@ Deno.test('with user 200', async () => {
|
|||
});
|
||||
|
||||
Deno.test('user and role 403', async () => {
|
||||
const { app, user } = testApp();
|
||||
await using app = new TestApp();
|
||||
|
||||
app.user();
|
||||
|
||||
const response = await app
|
||||
.use(setUser(user))
|
||||
.use(userMiddleware({ role: 'admin' }))
|
||||
.request('/');
|
||||
|
||||
|
|
@ -50,7 +57,10 @@ Deno.test('user and role 403', 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({
|
||||
kind: 30382,
|
||||
|
|
@ -65,7 +75,6 @@ Deno.test('admin role 200', async () => {
|
|||
await relay.event(event);
|
||||
|
||||
const response = await app
|
||||
.use(setUser(user))
|
||||
.use(userMiddleware({ role: 'admin' }))
|
||||
.get('/', (c) => c.text('ok'))
|
||||
.request('/');
|
||||
|
|
|
|||
|
|
@ -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 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();
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { DittoConf } from '@ditto/conf';
|
||||
import { type DittoDB, DummyDB } from '@ditto/db';
|
||||
import { HTTPException } from '@hono/hono/http-exception';
|
||||
import { type NRelay, NSecSigner } from '@nostrify/nostrify';
|
||||
import { generateSecretKey, nip19 } from 'nostr-tools';
|
||||
|
||||
|
|
@ -43,7 +44,15 @@ export class TestApp extends DittoApp implements AsyncDisposable {
|
|||
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;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue