feat(backend): pluggable auth (none/mock/oidc seam)
- authPreHandler(mode) returns Fastify preHandler that sets req.user={id}
- none: uses dev@local
- mock: reads x-mock-user header
- oidc: MVP stub returns 501 + throws
- getUser(req) returns user object
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { test, expect } from "vitest";
|
||||
import Fastify from "fastify";
|
||||
import { authPreHandler, getUser } from "../src/auth/auth.js";
|
||||
|
||||
test("mode none assegna dev@local", async () => {
|
||||
const app = Fastify();
|
||||
app.addHook("preHandler", authPreHandler("none"));
|
||||
app.get("/me", async (req) => getUser(req));
|
||||
expect((await app.inject({ method: "GET", url: "/me" })).json()).toEqual({
|
||||
id: "dev@local",
|
||||
});
|
||||
});
|
||||
|
||||
test("mode mock legge l'header", async () => {
|
||||
const app = Fastify();
|
||||
app.addHook("preHandler", authPreHandler("mock"));
|
||||
app.get("/me", async (req) => getUser(req));
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/me",
|
||||
headers: { "x-mock-user": "alice" },
|
||||
});
|
||||
expect(res.json()).toEqual({ id: "alice" });
|
||||
});
|
||||
Reference in New Issue
Block a user