Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/api/src/routes/payment-auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { paymentRoutes } from "./paymentRoutes.js";

test("paymentRoutes applies authMiddleware requiring JWT token", async () => {
let middlewareCalled = false;
let statusSet = 0;

const req = { headers: {} };
const res = {
status(code) {
statusSet = code;
return this;
},
json(data) {
return this;
},
};

const next = () => {
middlewareCalled = true;
};

// Find authMiddleware layer in stack
const authLayer = paymentRoutes.stack.find((layer) => layer.name === "authMiddleware" || layer.handle?.name === "authMiddleware");
assert.ok(authLayer, "paymentRoutes must include authMiddleware layer");

await authLayer.handle(req, res, next);

assert.equal(statusSet, 401, "Unauthenticated request to paymentRoutes must return HTTP 401 Unauthorized");
assert.equal(middlewareCalled, false, "next() must not be called without valid authorization header");
});
2 changes: 2 additions & 0 deletions apps/api/src/routes/paymentRoutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Router } from "express";
import { createPayment } from "../controllers/paymentController.js";
import { authMiddleware } from "../middleware/auth.js";

export const paymentRoutes = Router();

paymentRoutes.use(authMiddleware);
paymentRoutes.post("/", createPayment);
Loading