diff --git a/package.json b/package.json index 17c9ec3..7db942b 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "format:check": "prettier --check .", "lint": "npm run typecheck && npm run format:check && eslint .", "lint:fix": "prettier --write . && eslint . --fix", + "test": "tsx --test src/auth.test.ts", "test:client": "tsx test-client.ts", "typecheck": "tsc --noEmit" }, diff --git a/src/auth.test.ts b/src/auth.test.ts new file mode 100644 index 0000000..b8417ea --- /dev/null +++ b/src/auth.test.ts @@ -0,0 +1,41 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { Hono } from "hono"; +import { createProtectedRoute } from "./auth"; +import type { AppContext, Env } from "./env"; + +test("protected routes return an MPP payment challenge", async () => { + const app = new Hono(); + app.use( + "/paid", + createProtectedRoute({ + amount: "0.01", + description: "Test payment", + pattern: "/paid", + }), + ); + app.get("/paid", (c) => c.text("paid")); + + const env: Env = { + JWT_SECRET: "test-jwt-secret-000000000000000000000000000000000000", + MPP_SECRET_KEY: "test-mpp-secret-000000000000000000000000000000000000", + PAYMENT_CURRENCY: "0x20c0000000000000000000000000000000000000", + PAY_TO: "0x000000000000000000000000000000000000dEaD", + PROTECTED_PATTERNS: [ + { + amount: "0.01", + description: "Access to premium content for 1 hour", + pattern: "/premium/*", + }, + ], + TEMPO_TESTNET: true, + }; + + const response = await app.request("http://localhost/paid", undefined, env); + + assert.equal(response.status, 402); + assert.match( + response.headers.get("www-authenticate") ?? "", + /intent="charge"/, + ); +}); diff --git a/src/auth.ts b/src/auth.ts index 9696a25..025af2b 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -82,7 +82,7 @@ export function createProtectedRoute(config: ProtectedRouteConfig) { return async (c: Context, next: Next) => { const mppx = Mppx.create({ methods: [ - tempo({ + tempo.charge({ currency: c.env.PAYMENT_CURRENCY, recipient: c.env.PAY_TO, testnet: c.env.TEMPO_TESTNET,