mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
mastoapi: add a test module
This commit is contained in:
parent
d4fc10fe3e
commit
9c97cc387f
3 changed files with 35 additions and 27 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
"name": "@ditto/mastoapi",
|
"name": "@ditto/mastoapi",
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"exports": {
|
"exports": {
|
||||||
"./middleware": "./middleware/mod.ts"
|
"./middleware": "./middleware/mod.ts",
|
||||||
|
"./test": "./test.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,9 @@
|
||||||
import { DittoConf } from '@ditto/conf';
|
import { setUser, testApp } from '@ditto/mastoapi/test';
|
||||||
import { DummyDB } from '@ditto/db';
|
|
||||||
import { DittoApp, type DittoMiddleware } from '@ditto/router';
|
|
||||||
import { type NostrSigner, NSecSigner } from '@nostrify/nostrify';
|
|
||||||
import { MockRelay } from '@nostrify/nostrify/test';
|
|
||||||
import { assertEquals } from '@std/assert';
|
import { assertEquals } from '@std/assert';
|
||||||
import { generateSecretKey, nip19 } from 'nostr-tools';
|
|
||||||
|
|
||||||
import { userMiddleware } from './userMiddleware.ts';
|
import { userMiddleware } from './userMiddleware.ts';
|
||||||
import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts';
|
import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts';
|
||||||
|
|
||||||
import type { User } from './User.ts';
|
|
||||||
|
|
||||||
Deno.test('no user 401', async () => {
|
Deno.test('no user 401', async () => {
|
||||||
const { app } = testApp();
|
const { app } = testApp();
|
||||||
const response = await app.use(userMiddleware()).request('/');
|
const response = await app.use(userMiddleware()).request('/');
|
||||||
|
|
@ -79,21 +72,3 @@ Deno.test('admin role 200', async () => {
|
||||||
|
|
||||||
assertEquals(response.status, 200);
|
assertEquals(response.status, 200);
|
||||||
});
|
});
|
||||||
|
|
||||||
function testApp() {
|
|
||||||
const relay = new MockRelay();
|
|
||||||
const signer = new NSecSigner(generateSecretKey());
|
|
||||||
const conf = new DittoConf(new Map([['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())]]));
|
|
||||||
const db = new DummyDB();
|
|
||||||
const app = new DittoApp({ conf, relay, db });
|
|
||||||
const user = { signer, relay };
|
|
||||||
|
|
||||||
return { app, relay, conf, db, user };
|
|
||||||
}
|
|
||||||
|
|
||||||
function setUser<S extends NostrSigner>(user: User<S>): DittoMiddleware<{ user: User<S> }> {
|
|
||||||
return async (c, next) => {
|
|
||||||
c.set('user', user);
|
|
||||||
await next();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
||||||
32
packages/mastoapi/test.ts
Normal file
32
packages/mastoapi/test.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
import { DittoConf } from '@ditto/conf';
|
||||||
|
import { DummyDB } from '@ditto/db';
|
||||||
|
import { DittoApp, type DittoMiddleware } from '@ditto/router';
|
||||||
|
import { type NostrSigner, NSecSigner } from '@nostrify/nostrify';
|
||||||
|
import { MockRelay } from '@nostrify/nostrify/test';
|
||||||
|
import { generateSecretKey, nip19 } from 'nostr-tools';
|
||||||
|
|
||||||
|
import type { User } from '@ditto/mastoapi/middleware';
|
||||||
|
|
||||||
|
export function testApp() {
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue