From d9c921e101f92cbe90c46f9ff25674225690c0a5 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 2 Jan 2024 12:56:08 +0100 Subject: [PATCH] Add tests --- packages/backend-core/src/security/auth.ts | 2 +- .../src/security/tests/auth.spec.ts | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/backend-core/src/security/tests/auth.spec.ts 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", + }) + }) + }) +})