import { test } from "tap";
import Fastify from "fastify";
import prismPlugin from "@1stdigital/prism-fastify";
test("payment required without header", async (t) => {
const app = Fastify();
await app.register(prismPlugin, {
apiKey: "test-key",
routes: {
"/api/premium": { price: 0.01, description: "Test" },
},
});
app.get("/api/premium", async () => ({ message: "Premium" }));
const response = await app.inject({
method: "GET",
url: "/api/premium",
});
t.equal(response.statusCode, 402);
t.ok(response.json().paymentRequired);
});
test("access granted with valid payment", async (t) => {
const app = Fastify();
await app.register(prismPlugin, {
apiKey: "test-key",
routes: {
"/api/premium": { price: 0.01, description: "Test" },
},
});
app.get("/api/premium", async (request) => ({
message: "Premium",
payer: request.prismPayer,
}));
const payment = JSON.stringify({
scheme: "eip3009",
signature: "0x" + "0".repeat(130),
});
const response = await app.inject({
method: "GET",
url: "/api/premium",
headers: { "X-PAYMENT": payment },
});
t.equal(response.statusCode, 200);
t.equal(response.json().message, "Premium");
});