Files
Anirban SinghaandBandhan Majumder 0d164da8dd fix: implement missing password validation (#27634)
* fix: validate password presence and requirements

* chore: add error code for invalid passwords

* chore: avoid trimming password

* chore: add test for password validation

* fix: update password to delete user

* fix: create user password following protocol

* test: simplify deleteMeHandler password validation tests

* fix: address coderabbit

* fix: modify login params for failing tests

* fix: failing e2e tests

---------

Co-authored-by: Bandhan Majumder <133476557+bandhan-majumder@users.noreply.github.com>
2026-06-22 10:37:09 +05:30

63 lines
1.9 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
import { HttpError } from "@calcom/lib/http-error";
import { deleteMeHandler } from "./deleteMe.handler";
import type { DeleteMeOptions } from "./deleteMe.handler";
const makeMockCtx = (role: "USER" | "ADMIN" = "USER") =>
({
user: {
id: 123,
name: "Test User",
email: "test@example.com",
role,
},
} as DeleteMeOptions["ctx"]);
describe("deleteMeHandler", () => {
describe("Password presence validation", () => {
it("should throw error when password is empty", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx(),
input: { password: "" },
})
).rejects.toThrow(new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword }));
});
it("should throw error when password is only whitespace", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx(),
input: { password: " " },
})
).rejects.toThrow(new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword }));
});
});
describe("Password policy validation", () => {
it("should throw error for a weak password (USER role)", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx("USER"),
input: { password: "weak" },
})
).rejects.toThrow(
new HttpError({ statusCode: 400, message: ErrorCode.PasswordPolicyViolation })
);
});
it("should throw error for a weak password (ADMIN role)", async () => {
await expect(
deleteMeHandler({
ctx: makeMockCtx("ADMIN"),
input: { password: "weak" },
})
).rejects.toThrow(
new HttpError({ statusCode: 400, message: ErrorCode.PasswordPolicyViolation })
);
});
});
});