diff --git a/packages/backend-core/src/security/auth.ts b/packages/backend-core/src/security/auth.ts index 60fe72947b..218cc87467 100644 --- a/packages/backend-core/src/security/auth.ts +++ b/packages/backend-core/src/security/auth.ts @@ -3,7 +3,7 @@ const MIN_LENGTH = 8 export function validatePassword( password: string ): { valid: true } | { valid: false; error: string } { - if (password?.length < MIN_LENGTH) { + if (!password || password.length < MIN_LENGTH) { return { valid: false, error: "Password invalid. Minimum eight characters" } } diff --git a/packages/backend-core/src/security/tests/auth.spec.ts b/packages/backend-core/src/security/tests/auth.spec.ts new file mode 100644 index 0000000000..f2d3d08579 --- /dev/null +++ b/packages/backend-core/src/security/tests/auth.spec.ts @@ -0,0 +1,20 @@ +import { validatePassword } from "../auth" + +describe("auth", () => { + describe("validatePassword", () => { + it("a valid password returns successful", () => { + expect(validatePassword("password")).toEqual({ valid: true }) + }) + + it.each([ + ["undefined", undefined], + ["null", null], + ["empty", ""], + ])("%s returns unsuccessful", (_, password) => { + expect(validatePassword(password as string)).toEqual({ + valid: false, + error: "Password invalid. Minimum eight characters", + }) + }) + }) +})