import { describe, expect, it } from "vitest"; import { loginInputSchema, passwordSchema, passwordResetConfirmSchema, passwordResetRequestSchema, registerInputSchema, updateProfileInputSchema, } from "./auth.dto.js"; describe("password policy", () => { it("accepts a valid password", () => { expect(passwordSchema.safeParse("hunter222222").success).toBe(true); expect(passwordSchema.safeParse("a1".padEnd(10, "x")).success).toBe(true); }); it("rejects too-short passwords", () => { const r = passwordSchema.safeParse("a12345678"); // 9 chars expect(r.success).toBe(false); }); it("rejects password without a letter", () => { expect(passwordSchema.safeParse("1234567890").success).toBe(false); }); it("rejects password without a number", () => { expect(passwordSchema.safeParse("abcdefghijkl").success).toBe(false); }); it("rejects empty and >72 char passwords", () => { expect(passwordSchema.safeParse("").success).toBe(false); expect(passwordSchema.safeParse("a1" + "x".repeat(71)).success).toBe(false); }); it("uppercase letters count as letters", () => { expect(passwordSchema.safeParse("Password123").success).toBe(true); }); }); describe("registerInputSchema", () => { it("accepts a valid register payload", () => { const r = registerInputSchema.safeParse({ name: "Ada Lovelace", email: "Ada@Example.com", password: "Password123", }); expect(r.success).toBe(true); if (r.success) expect(r.data.email).toBe("ada@example.com"); // lowercased }); it("rejects invalid email", () => { expect( registerInputSchema.safeParse({ name: "A", email: "not-an-email", password: "Password123" }) .success, ).toBe(false); }); it("rejects weak password", () => { expect( registerInputSchema.safeParse({ name: "A", email: "a@b.co", password: "short" }).success, ).toBe(false); }); it("rejects unknown fields (strict)", () => { expect( registerInputSchema.safeParse({ name: "A", email: "a@b.co", password: "Password123", hacker: true, }).success, ).toBe(false); }); it("rejects empty name", () => { expect( registerInputSchema.safeParse({ name: "", email: "a@b.co", password: "Password123" }).success, ).toBe(false); }); }); describe("loginInputSchema", () => { it("accepts email+password", () => { expect(loginInputSchema.safeParse({ email: "a@b.co", password: "x" }).success).toBe(true); }); it("rejects missing password", () => { expect(loginInputSchema.safeParse({ email: "a@b.co" }).success).toBe(false); }); }); describe("password reset", () => { it("request: email only", () => { expect(passwordResetRequestSchema.safeParse({ email: "a@b.co" }).success).toBe(true); expect(passwordResetRequestSchema.safeParse({ email: "nope" }).success).toBe(false); }); it("confirm: token + strong password", () => { expect( passwordResetConfirmSchema.safeParse({ token: "tok", password: "Password123" }).success, ).toBe(true); expect(passwordResetConfirmSchema.safeParse({ token: "tok", password: "weak" }).success).toBe( false, ); expect(passwordResetConfirmSchema.safeParse({ password: "Password123" }).success).toBe(false); }); }); describe("updateProfileInputSchema", () => { it("accepts a partial update", () => { expect(updateProfileInputSchema.safeParse({ name: "Grace" }).success).toBe(true); }); it("rejects empty object", () => { expect(updateProfileInputSchema.safeParse({}).success).toBe(false); }); it("validates theme enum", () => { expect(updateProfileInputSchema.safeParse({ theme: "neon" }).success).toBe(false); expect(updateProfileInputSchema.safeParse({ theme: "dark" }).success).toBe(true); }); });