Files
EpicNext-Cms/src/lib/validators/user.test.ts
T
openhands 17847545dd
Deploy / release (push) Skipped
Deploy / deploy (push) Successful in 1m52s
Improvements: remove dead config, fix ESM, add URL validation, unify types, add missing logging
- Remove .prettierrc (dead config, Biome replaces Prettier)
- Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat
- Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit
- Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts
- Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars
- Replace barrel export src/types/index.ts with direct @/types/common imports
- Make trustHost conditional (development only) in auth.ts
- Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations
- Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
2026-07-26 20:28:11 +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: "secret123",
});
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: "secret123",
}).success,
).toBe(false);
});
it("rejects long username (> 20 chars)", () => {
expect(
createUserSchema.safeParse({
username: "a".repeat(21),
mail: "[email protected]",
password: "secret123",
}).success,
).toBe(false);
});
it("rejects invalid username characters", () => {
expect(
createUserSchema.safeParse({
username: "user@name",
mail: "[email protected]",
password: "secret123",
}).success,
).toBe(false);
});
it("rejects invalid email", () => {
expect(
createUserSchema.safeParse({
username: "TestUser",
mail: "not-an-email",
password: "secret123",
}).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);
});
});