fix(api): harden user self-service and resource linking
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { SystemRole } from "@capakraken/shared";
|
||||
|
||||
vi.mock("@capakraken/application", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("@capakraken/application")>();
|
||||
return {
|
||||
...actual,
|
||||
getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
|
||||
getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
|
||||
listAssignmentBookings: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
});
|
||||
|
||||
import { executeTool } from "../router/assistant-tools.js";
|
||||
import { createToolContext } from "./assistant-tools-user-admin-test-helpers.js";
|
||||
|
||||
describe("assistant user admin tools user create errors", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns a stable error when creating a user with a duplicate email", async () => {
|
||||
const ctx = createToolContext(
|
||||
{
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "user_existing",
|
||||
email: "peter.parker@example.com",
|
||||
name: "Peter Parker",
|
||||
}),
|
||||
},
|
||||
},
|
||||
SystemRole.ADMIN,
|
||||
);
|
||||
|
||||
const result = await executeTool(
|
||||
"create_user",
|
||||
JSON.stringify({
|
||||
email: "peter.parker@example.com",
|
||||
name: "Peter Parker",
|
||||
password: "secret123",
|
||||
}),
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
|
||||
error: "User with this email already exists.",
|
||||
}));
|
||||
});
|
||||
|
||||
it("returns a stable error when creating a user without a name", async () => {
|
||||
const ctx = createToolContext(
|
||||
{
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
},
|
||||
SystemRole.ADMIN,
|
||||
);
|
||||
|
||||
const result = await executeTool(
|
||||
"create_user",
|
||||
JSON.stringify({
|
||||
email: "miles.morales@example.com",
|
||||
name: "",
|
||||
password: "secret123",
|
||||
}),
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
|
||||
error: "Name is required.",
|
||||
}));
|
||||
expect(ctx.db.user.findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns a stable error when creating a user with a password that is too short", async () => {
|
||||
const ctx = createToolContext(
|
||||
{
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
},
|
||||
SystemRole.ADMIN,
|
||||
);
|
||||
|
||||
const result = await executeTool(
|
||||
"create_user",
|
||||
JSON.stringify({
|
||||
email: "miles.morales@example.com",
|
||||
name: "Miles Morales",
|
||||
password: "short",
|
||||
}),
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(JSON.parse(result.content)).toEqual(expect.objectContaining({
|
||||
error: "Password must be at least 8 characters.",
|
||||
}));
|
||||
expect(ctx.db.user.findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user