Skip to main content

Overview

Testing an L402 API has two layers:
  1. Unit / integration tests — verify your middleware logic without real Lightning payments
  2. End-to-end tests — verify the full flow with a real wallet and real sats

Unit tests — mock the provider

Pass a mock LightningProvider to bypass Lightning entirely:
import { l402, LightningProvider, Invoice } from "l402-kit";
import request from "supertest";
import express from "express";

const mockProvider: LightningProvider = {
  async createInvoice(amountSats: number): Promise<Invoice> {
    const hash = "abc123def456abc123def456abc123def456abc123def456abc123def456abc1";
    const macaroon = Buffer.from(
      JSON.stringify({ hash, exp: Date.now() + 3_600_000 })
    ).toString("base64");
    return { paymentRequest: "lnbc1...", paymentHash: hash, macaroon, amountSats };
  },
  async checkPayment(paymentHash: string): Promise<boolean> {
    return true; // always paid in tests
  },
};

const app = express();
app.get("/premium", l402({ priceSats: 10, lightning: mockProvider }), (_req, res) => {
  res.json({ data: "ok" });
});

// Test 1 — no auth → 402
const res402 = await request(app).get("/premium");
assert(res402.status === 402);
assert(res402.body.invoice === "lnbc1...");

// Test 2 — valid token → 200
// SHA256("correct-preimage") must equal the paymentHash above, or use a real hash pair
const macaroon = res402.body.macaroon;
const preimage = "correct-preimage-hex"; // must satisfy SHA256(preimage) == paymentHash
const res200 = await request(app)
  .get("/premium")
  .set("Authorization", `L402 ${macaroon}:${preimage}`);
assert(res200.status === 200);

Generating a valid preimage for tests

import { createHash, randomBytes } from "crypto";

// Generate a real hash/preimage pair
const preimage = randomBytes(32).toString("hex");
const paymentHash = createHash("sha256").update(Buffer.from(preimage, "hex")).digest("hex");

// Use these in your mock provider
const mockProvider: LightningProvider = {
  async createInvoice(amountSats: number): Promise<Invoice> {
    const macaroon = Buffer.from(
      JSON.stringify({ hash: paymentHash, exp: Date.now() + 3_600_000 })
    ).toString("base64");
    return { paymentRequest: "lnbc1...", paymentHash, macaroon, amountSats };
  },
  async checkPayment(): Promise<boolean> { return true; },
};

// In your test:
// Authorization: L402 <macaroon>:<preimage>
// This satisfies SHA256(preimage) == paymentHash ✓

Python — pytest with mock provider

import pytest
import hashlib
import secrets
import base64
import json
from fastapi.testclient import TestClient
from l402kit.types import LightningProvider, Invoice
from your_app import app

class MockProvider(LightningProvider):
    def __init__(self):
        self.preimage = secrets.token_hex(32)
        self.payment_hash = hashlib.sha256(bytes.fromhex(self.preimage)).hexdigest()

    async def create_invoice(self, amount_sats: int) -> Invoice:
        exp = int((__import__("time").time() + 3600) * 1000)
        macaroon = base64.b64encode(
            json.dumps({"hash": self.payment_hash, "exp": exp}).encode()
        ).decode()
        return Invoice(
            payment_request="lnbc1...",
            payment_hash=self.payment_hash,
            macaroon=macaroon,
            amount_sats=amount_sats,
        )

    async def check_payment(self, payment_hash: str) -> bool:
        return True

def test_402_then_200(monkeypatch):
    provider = MockProvider()
    monkeypatch.setattr("your_app.lightning", provider)
    client = TestClient(app)

    # Step 1: no auth → 402
    res = client.get("/premium")
    assert res.status_code == 402
    macaroon = res.json()["macaroon"]

    # Step 2: valid L402 token → 200
    auth = f"L402 {macaroon}:{provider.preimage}"
    res = client.get("/premium", headers={"Authorization": auth})
    assert res.status_code == 200

Testing replay protection

Verify that a preimage cannot be reused:
const res1 = await request(app)
  .get("/premium")
  .set("Authorization", `L402 ${macaroon}:${preimage}`);
assert(res1.status === 200); // first use — ok

const res2 = await request(app)
  .get("/premium")
  .set("Authorization", `L402 ${macaroon}:${preimage}`);
assert(res2.status === 401); // replay — rejected
assert(res2.body.error === "Token already used");

CI pipeline

Use the mock provider in CI — no Lightning node or API key needed:
# .github/workflows/test.yml
- name: Run tests
  run: npm test
  env:
    NODE_ENV: test
    # No BLINK_API_KEY needed — mock provider is used in test env
Guard your provider selection by environment:
const lightning = process.env.NODE_ENV === "test"
  ? mockProvider
  : ManagedProvider.fromAddress(process.env.LIGHTNING_ADDRESS!);

End-to-end test with real sats

For a full payment flow test (staging/pre-launch):
  1. Set priceSats: 1 — costs ~$0.0008 per test run
  2. Use OpenNode sandbox (testMode: true) to pay without real money:
    const lightning = new OpenNodeProvider(process.env.OPENNODE_KEY!, true); // testMode
    
  3. Or use your Blink wallet — 1 sat payments are practically free

Automated E2E with a test wallet

import { L402Client, BlinkWallet } from "l402-kit";

// Use a dedicated test wallet with a small budget
const wallet = new BlinkWallet(
  process.env.TEST_BLINK_API_KEY!,
  process.env.TEST_BLINK_WALLET_ID!,
);
const client = new L402Client({ wallet, budgetSats: 100 });

const res = await client.fetch("http://localhost:3000/premium");
assert(res.ok);
const data = await res.json();
assert(data.data !== undefined);

Checklist before production

1

Unit tests pass with mock provider

402 → pay → 200 flow verified. Replay protection verified (second use returns 401).
2

Token expiry tested

Set exp: Date.now() - 1 in your mock macaroon — verify the middleware returns 401.
3

Full E2E with real payment at priceSats: 1

Real wallet, real payment, real 200 OK. Use Wallet of Satoshi or Blink on your phone.
4

Replay protection correct for your deployment

Single process: default in-memory adapter is fine. Multi-process (Kubernetes, PM2 cluster): use Supabase or Redis adapter. See Production Guide.