Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1674a7df4a | |||
| 09187ca277 |
@@ -28,6 +28,7 @@ GroomBook API is a Hono-based REST service (TypeScript/Node.js) powering the pet
|
|||||||
| TC-API-1.1 | Login via OIDC | POST to OIDC provider callback, verify JWT token issued | 200 OK, JWT returned with valid claims |
|
| TC-API-1.1 | Login via OIDC | POST to OIDC provider callback, verify JWT token issued | 200 OK, JWT returned with valid claims |
|
||||||
| TC-API-1.2 | Session persistence | Make authenticated request, verify session token valid | 200 OK, request succeeds |
|
| TC-API-1.2 | Session persistence | Make authenticated request, verify session token valid | 200 OK, request succeeds |
|
||||||
| TC-API-1.3 | Logout | Call logout endpoint, verify token invalidated | 200 OK, subsequent requests return 401 |
|
| TC-API-1.3 | Logout | Call logout endpoint, verify token invalidated | 200 OK, subsequent requests return 401 |
|
||||||
|
| TC-API-1.4 | Auto-provision on first OIDC login | First login as a Better-Auth user with no existing staff record | 200 OK, access granted; groomer staff record auto-created with name/email from user table |
|
||||||
|
|
||||||
### 4.2 Client Management
|
### 4.2 Client Management
|
||||||
|
|
||||||
|
|||||||
@@ -45,40 +45,72 @@ const GROOMER: StaffRow = {
|
|||||||
|
|
||||||
let staffLookupResult: StaffRow | null = null;
|
let staffLookupResult: StaffRow | null = null;
|
||||||
let managerFallbackResult: StaffRow | null = MANAGER;
|
let managerFallbackResult: StaffRow | null = MANAGER;
|
||||||
|
let userLookupResult: { id: string; name: string | null; email: string | null } | null = null;
|
||||||
|
let insertedStaff: StaffRow | null = null;
|
||||||
|
|
||||||
vi.mock("../db", () => {
|
vi.mock("../db", () => {
|
||||||
const staff = new Proxy(
|
const makeTableProxy = (name: string) =>
|
||||||
{ _name: "staff" },
|
new Proxy(
|
||||||
{
|
{ _name: name },
|
||||||
get(target, prop) {
|
{
|
||||||
if (prop === "_name") return "staff";
|
get(target, prop) {
|
||||||
if (prop === "$inferSelect") return {};
|
if (prop === "_name") return name;
|
||||||
return { table: "staff", column: prop };
|
if (prop === "$inferSelect") return {};
|
||||||
|
return { table: name, column: prop };
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const staff = makeTableProxy("staff");
|
||||||
|
const user = makeTableProxy("user");
|
||||||
|
|
||||||
|
const buildQuery = (result: unknown, fallback: unknown) => ({
|
||||||
|
limit: () => ({
|
||||||
|
[Symbol.iterator]: function* () {
|
||||||
|
if (result) yield result;
|
||||||
},
|
},
|
||||||
}
|
0: result,
|
||||||
);
|
length: result ? 1 : 0,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getDb: () => ({
|
getDb: () => ({
|
||||||
select: () => ({
|
select: () => ({
|
||||||
from: () => ({
|
from: (table: unknown) => ({
|
||||||
where: () => ({
|
where: () => buildQuery(
|
||||||
limit: () => {
|
table === staff ? staffLookupResult : userLookupResult,
|
||||||
// dev mode fallback to first manager
|
table === staff ? managerFallbackResult : null
|
||||||
return managerFallbackResult ? [managerFallbackResult] : [];
|
),
|
||||||
},
|
}),
|
||||||
[Symbol.iterator]: function* () {
|
}),
|
||||||
if (staffLookupResult) yield staffLookupResult;
|
insert: (table: unknown) => ({
|
||||||
},
|
values: (vals: Record<string, unknown>) => ({
|
||||||
0: staffLookupResult,
|
returning: () => {
|
||||||
length: staffLookupResult ? 1 : 0,
|
const newStaff: StaffRow = {
|
||||||
}),
|
id: "new-staff-id",
|
||||||
|
oidcSub: null,
|
||||||
|
userId: vals.userId as string,
|
||||||
|
role: vals.role as StaffRow["role"],
|
||||||
|
isSuperUser: false,
|
||||||
|
name: vals.name as string,
|
||||||
|
email: vals.email as string,
|
||||||
|
active: true,
|
||||||
|
icalToken: null,
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
};
|
||||||
|
insertedStaff = newStaff;
|
||||||
|
return [newStaff];
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
staff,
|
staff,
|
||||||
|
user,
|
||||||
eq: vi.fn((_col: unknown, _val: unknown) => ({ col: _col, val: _val })),
|
eq: vi.fn((_col: unknown, _val: unknown) => ({ col: _col, val: _val })),
|
||||||
and: vi.fn((..._clauses: unknown[]) => ({})),
|
and: vi.fn((..._clauses: unknown[]) => ({})),
|
||||||
|
sql: vi.fn((..._args: unknown[]) => ({})),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -87,6 +119,8 @@ vi.mock("../db", () => {
|
|||||||
function resetMocks() {
|
function resetMocks() {
|
||||||
staffLookupResult = null;
|
staffLookupResult = null;
|
||||||
managerFallbackResult = MANAGER;
|
managerFallbackResult = MANAGER;
|
||||||
|
userLookupResult = null;
|
||||||
|
insertedStaff = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build a minimal Hono app with jwtPayload pre-set, then apply a middleware. */
|
/** Build a minimal Hono app with jwtPayload pre-set, then apply a middleware. */
|
||||||
@@ -202,6 +236,50 @@ describe("resolveStaffMiddleware", () => {
|
|||||||
const body = await res.json();
|
const body = await res.json();
|
||||||
expect(body.error).toMatch(/no staff records found/i);
|
expect(body.error).toMatch(/no staff records found/i);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("auto-provision: creates groomer staff record on first login when Better-Auth user exists", async () => {
|
||||||
|
staffLookupResult = null;
|
||||||
|
userLookupResult = { id: "ba-user-new", name: "New User", email: "newuser@example.com" };
|
||||||
|
let capturedStaff: StaffRow | null = null;
|
||||||
|
const app = buildApp(resolveStaffMiddleware, (c) => {
|
||||||
|
capturedStaff = c.get("staff");
|
||||||
|
return c.json({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await app.request("/test");
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(capturedStaff).not.toBeNull();
|
||||||
|
expect(capturedStaff!.role).toBe("groomer");
|
||||||
|
expect(capturedStaff!.userId).toBe("ba-user-new");
|
||||||
|
expect(capturedStaff!.name).toBe("New User");
|
||||||
|
expect(capturedStaff!.email).toBe("newuser@example.com");
|
||||||
|
expect(capturedStaff!.isSuperUser).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("auto-provision: falls back to email prefix when user has no name", async () => {
|
||||||
|
staffLookupResult = null;
|
||||||
|
userLookupResult = { id: "ba-user-noname", name: null, email: "firstlogin@example.com" };
|
||||||
|
let capturedStaff: StaffRow | null = null;
|
||||||
|
const app = buildApp(resolveStaffMiddleware, (c) => {
|
||||||
|
capturedStaff = c.get("staff");
|
||||||
|
return c.json({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await app.request("/test");
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(capturedStaff!.name).toBe("firstlogin");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("auto-provision: returns 403 when no staff record and no Better-Auth user exists", async () => {
|
||||||
|
staffLookupResult = null;
|
||||||
|
userLookupResult = null;
|
||||||
|
const app = buildApp(resolveStaffMiddleware);
|
||||||
|
|
||||||
|
const res = await app.request("/test");
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
const body = await res.json();
|
||||||
|
expect(body.error).toMatch(/no staff record found for authenticated user/i);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ─── requireRole tests ────────────────────────────────────────────────────────
|
// ─── requireRole tests ────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { MiddlewareHandler } from "hono";
|
import type { MiddlewareHandler } from "hono";
|
||||||
import { and, eq, getDb, sql, staff } from "../db/index.js";
|
import { and, eq, getDb, sql, staff, user } from "../db/index.js";
|
||||||
|
|
||||||
export type StaffRole = "groomer" | "receptionist" | "manager";
|
export type StaffRole = "groomer" | "receptionist" | "manager";
|
||||||
export type StaffRow = typeof staff.$inferSelect;
|
export type StaffRow = typeof staff.$inferSelect;
|
||||||
@@ -110,6 +110,30 @@ export const resolveStaffMiddleware: MiddlewareHandler<AppEnv> = async (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Auto-provision: no staff record exists for this user at all, but a valid
|
||||||
|
// Better-Auth user session exists (jwt.sub = user.id from user table).
|
||||||
|
// Create a minimal groomer staff record on first login.
|
||||||
|
const [userRow] = await db
|
||||||
|
.select({ id: user.id, name: user.name, email: user.email })
|
||||||
|
.from(user)
|
||||||
|
.where(eq(user.id, jwt.sub))
|
||||||
|
.limit(1);
|
||||||
|
if (userRow) {
|
||||||
|
const [newStaff] = await db
|
||||||
|
.insert(staff)
|
||||||
|
.values({
|
||||||
|
name: userRow.name ?? jwt.email?.split("@")[0] ?? "Unknown",
|
||||||
|
email: userRow.email ?? jwt.email ?? "",
|
||||||
|
userId: jwt.sub,
|
||||||
|
role: "groomer",
|
||||||
|
isSuperUser: false,
|
||||||
|
active: true,
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
c.set("staff", newStaff);
|
||||||
|
await next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
return c.json(
|
return c.json(
|
||||||
{ error: "Forbidden: no staff record found for authenticated user" },
|
{ error: "Forbidden: no staff record found for authenticated user" },
|
||||||
403
|
403
|
||||||
|
|||||||
Reference in New Issue
Block a user