diff --git a/Sprint-3/4-stretch/card-validator.js b/Sprint-3/4-stretch/card-validator.js new file mode 100644 index 000000000..134867007 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.js @@ -0,0 +1,13 @@ +function validateNumber(number) { + const arrNumber = [...number.toString()]; + + return arrNumber.length === 16 && + arrNumber.every((x) => x >= "0" && x <= "9") && + new Set(arrNumber).size > 1 && + arrNumber[arrNumber.length - 1] % 2 === 0 && + arrNumber.reduce((acc, cur) => +acc + +cur, 0) > 16 + ? true + : false; +} + +module.exports = validateNumber; diff --git a/Sprint-3/4-stretch/card-validator.test.js b/Sprint-3/4-stretch/card-validator.test.js new file mode 100644 index 000000000..c79150032 --- /dev/null +++ b/Sprint-3/4-stretch/card-validator.test.js @@ -0,0 +1,43 @@ +const validateNumber = require("./card-validator"); +const isValidNumber = require("./card-validator"); + +test("returns true for a 16-digit number", () => { + expect(validateNumber(1029384756820562)).toEqual(true); +}); +test("should return false if the number is less than 16 digits", () => { + expect(validateNumber(10293847568202)).toEqual(false); +}); +test("should return false if the number is more than 16 digits long", () => { + expect(validateNumber(1029384756820512348)).toEqual(false); +}); + +test("returns true when all characters are digits", () => { + expect(validateNumber(1036294650361848)).toEqual(true); +}); +test("should return false if one or more of the digits aren't numbers", () => { + expect(validateNumber("103629465036184a")).toEqual(false); +}); + +test("returns true when digits are not all identical", () => { + expect(validateNumber(3636363636363636)).toEqual(true); +}); +test("returns true when one digit differs", () => { + expect(validateNumber(3333333333333336)).toEqual(true); +}); +test("returns false when all digits are identical", () => { + expect(validateNumber(2222222222222222)).toEqual(false); +}); + +test("should return true if the final digit is even", () => { + expect(validateNumber(1528056378293456)).toEqual(true); +}); +test("should return false if the final digit isn't even", () => { + expect(validateNumber(1528056378293457)).toEqual(false); +}); + +test("should return true if the sum of all digits is greater than 16", () => { + expect(validateNumber(1903647295628592)).toEqual(true); +}); +test("should return false if the sum of all the digits isn't greater than 16", () => { + expect(validateNumber(1000100000000002)).toEqual(false); +}); diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527db..e43442c34 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,6 +1,16 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true -} +function passwordValidator(password,oldPasswords=[]) { + if ( + /[A-Z]/.test(password) && + /[a-z]/.test(password) && + /[0-9]/.test(password) && + /[!#$%.*&]/.test(password) && + password.length >= 5 && + !oldPasswords.includes(password) + ) + return true; + + return false; +} -module.exports = passwordValidator; \ No newline at end of file +module.exports = passwordValidator; diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6..4e622a711 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -10,17 +10,67 @@ To be valid, a password must: - Have at least one English lowercase letter (a-z) - Have at least one number (0-9) - Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&") -- Must not be any previous password in the passwords array. +- Must not be any previous password in the passwords array. You must breakdown this problem in order to solve it. Find one test case first and get that working */ const isValidPassword = require("./password-validator"); -test("password has at least 5 characters", () => { - // Arrange - const password = "12345"; - // Act + +describe("Valid passwords", () => { + test("should return true for a valid password with all rules met", () => { + const password = "12345Dpw%"; const result = isValidPassword(password); - // Assert expect(result).toEqual(true); -} -); \ No newline at end of file + }); + + test("should return true for another valid password", () => { + const password = "Abc123!"; + const result = isValidPassword(password); + expect(result).toEqual(true); + }); + + test("should return true for a valid password with different special symbol", () => { + const password = "MyPass#1"; + const result = isValidPassword(password); + expect(result).toEqual(true); + }); +}); + +describe("Invalid passwords - each breaks one rule", () => { + test("should reject password with less than 5 characters", () => { + const password = "1aS!"; + const result = isValidPassword(password); + expect(result).toEqual(false); + }); + + test("should reject password without an uppercase letter", () => { + const password = "abcde1!"; + const result = isValidPassword(password); + expect(result).toEqual(false); + }); + + test("should reject password without a lowercase letter", () => { + const password = "ABCDE1!"; + const result = isValidPassword(password); + expect(result).toEqual(false); + }); + + test("should reject password without a number", () => { + const password = "abcdeFg!"; + const result = isValidPassword(password); + expect(result).toEqual(false); + }); + + test("should reject password without a special symbol", () => { + const password = "abcde1FG"; + const result = isValidPassword(password); + expect(result).toEqual(false); + }); + + test("should reject password if it was used before", () => { + const password = "12345Dpw%"; + const oldPasswords = ["12345Dpw%"]; + const result = isValidPassword(password, oldPasswords); + expect(result).toEqual(false); + }); +});