Files
Nexus/packages/api/src/__tests__/assistant-tools-user-admin-user-create-errors.test.ts
T
Hartmut dfeb4d361e fix(tests): align 20 drifted tests with current source behavior
Tests fell behind source changes: lastTotpAt replay-attack prevention,
activeSession invalidation on password reset, select clauses in
permission updates, UNAUTHORIZED (anti-enumeration) for disabled TOTP,
and password minimum raised from 8 to 12 characters.

Also fix root eslint.config.mjs to ignore packages/ (linted via turbo)
and add --no-warn-ignored to lint-staged to suppress warnings for
ignored files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 15:41:42 +02:00

109 lines
2.7 KiB
TypeScript

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: "SecurePass123!",
}),
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: "SecurePass123!",
}),
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 12 characters.",
}),
);
expect(ctx.db.user.findUnique).not.toHaveBeenCalled();
});
});