Files
EpicNext-Cms/src/lib/validators/user.test.ts
T
openhands 9cf1eb615c
CI / check (push) Successful in 29s
CI / tests-imaging-browsers (push) Successful in 1m46s
CI / tests-integration (push) Successful in 1m47s
CI / tests-unit (push) Successful in 1m49s
CI / tests-quality (push) Successful in 44s
CI / tests-ui (push) Successful in 4m38s
CI / preflight (push) Skipped
CI / deploy (push) Successful in 3m38s
feat(auth): make password length configurable via env (min 5–30, max 30–128)
Add PASSWORD_MIN_LENGTH / PASSWORD_MAX_LENGTH (validated in src/env.ts) and a
shared passwordPolicy helper. The policy drives the zod schemas in register,
password reset and the admin create-user dialog, plus the live requirement
checks and strength meter in the register form.

Locale messages now interpolate the configured minimum via {min} instead of
hard-coding 12.
2026-10-18 19:20:38 +02:00

142 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
banUserSchema,
createUserSchema,
giveBadgeSchema,
updateUserSchema,
} from "./user";
describe("createUserSchema", () => {
it("accepts valid user", () => {
const result = createUserSchema.safeParse({
username: "TestUser",
mail: "[email protected]",
password: "secret123456",
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.rank).toBe(1);
expect(result.data.motto).toBe("");
}
});
it("rejects short username (< 3 chars)", () => {
expect(
createUserSchema.safeParse({
username: "ab",
mail: "[email protected]",
password: "secret123456",
}).success,
).toBe(false);
});
it("rejects long username (> 20 chars)", () => {
expect(
createUserSchema.safeParse({
username: "a".repeat(21),
mail: "[email protected]",
password: "secret123456",
}).success,
).toBe(false);
});
it("rejects invalid username characters", () => {
expect(
createUserSchema.safeParse({
username: "user@name",
mail: "[email protected]",
password: "secret123456",
}).success,
).toBe(false);
});
it("rejects invalid email", () => {
expect(
createUserSchema.safeParse({
username: "TestUser",
mail: "not-an-email",
password: "secret123456",
}).success,
).toBe(false);
});
it("rejects short password (< 6 chars)", () => {
expect(
createUserSchema.safeParse({
username: "TestUser",
mail: "[email protected]",
password: "12345",
}).success,
).toBe(false);
});
});
describe("updateUserSchema", () => {
it("is partial", () => {
expect(updateUserSchema.safeParse({}).success).toBe(true);
});
it("accepts username update", () => {
const result = updateUserSchema.safeParse({
username: "NewName",
});
if (result.success) expect(result.data.username).toBe("NewName");
});
it("rejects rank out of range", () => {
expect(updateUserSchema.safeParse({ rank: 8 }).success).toBe(false);
});
});
describe("banUserSchema", () => {
it("accepts valid ban", () => {
const result = banUserSchema.safeParse({
userId: 1,
reason: "Spamming",
duration: 24,
type: "account",
});
expect(result.success).toBe(true);
if (result.success) expect(result.data.type).toBe("account");
});
it("defaults type to account", () => {
const result = banUserSchema.safeParse({
userId: 1,
reason: "Spamming",
duration: 0,
});
if (result.success) expect(result.data.type).toBe("account");
});
it("rejects empty reason", () => {
expect(
banUserSchema.safeParse({ userId: 1, reason: "", duration: 1 }).success,
).toBe(false);
});
it("rejects negative duration", () => {
expect(
banUserSchema.safeParse({ userId: 1, reason: "Bad", duration: -1 })
.success,
).toBe(false);
});
});
describe("giveBadgeSchema", () => {
it("accepts valid badge", () => {
const result = giveBadgeSchema.safeParse({
userId: 1,
badgeCode: "BADGE01",
});
expect(result.success).toBe(true);
});
it("rejects empty badge code", () => {
expect(
giveBadgeSchema.safeParse({ userId: 1, badgeCode: "" }).success,
).toBe(false);
});
});