From e16908df5c2f30fd78768c33184c6d6f54337215 Mon Sep 17 00:00:00 2001 From: Maciej Date: Wed, 30 Jul 2025 16:40:47 +0200 Subject: [PATCH 1/2] Solution --- src/checkPassword.js | 5 ++- src/checkPassword.test.js | 87 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/checkPassword.js b/src/checkPassword.js index 1a6fc05..c69eb62 100644 --- a/src/checkPassword.js +++ b/src/checkPassword.js @@ -8,10 +8,11 @@ function checkPassword(password) { // eslint-disable-next-line const validPasswordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/; - const cyrillicValidation = /^((?![А-Яа-я]).)*$/; + const doHaveOtherLetters = /[^a-z0-9~`!@#$%^&*()-_+={}[\]|\\/:;"'<>,.?]/i + .test(password); // eslint-disable-next-line - if (password.match(validPasswordRegex) && password.match(cyrillicValidation)) { + if (password.match(validPasswordRegex) && !doHaveOtherLetters) { return true; } diff --git a/src/checkPassword.test.js b/src/checkPassword.test.js index 1e77e16..6dffb70 100644 --- a/src/checkPassword.test.js +++ b/src/checkPassword.test.js @@ -2,18 +2,103 @@ describe(`Function 'checkPassword':`, () => { const checkPassword = require('./checkPassword'); + const generatePassword = (amountOfCharacters, partWithRequiredCharacters) => { + const different = amountOfCharacters - partWithRequiredCharacters.length; + + if (different <= 0) { + return partWithRequiredCharacters.slice(0, amountOfCharacters); + } + + const randomLetterASCII = (howManyTimes) => { + let generatedLetters = ''; + + for (let i = 1; i <= howManyTimes; i++) { + const codeASCII = Math.floor(Math.random() * 26) + 97; + + generatedLetters += String.fromCharCode(codeASCII); + } + + return generatedLetters; + }; + + const randomPart = randomLetterASCII(amountOfCharacters + - partWithRequiredCharacters.length); + const result = partWithRequiredCharacters + randomPart; + + return result; + }; it(`should be declared`, () => { expect(checkPassword).toBeInstanceOf(Function); }); it(`should return boolean`, () => { + const typeOfValue = typeof checkPassword('B34ji^sska'); + expect(typeOfValue).toBeTruthy(); }); it(`should return 'true' for the valid password with 8 characters`, () => { + const value = generatePassword(8, '1^B'); + expect(checkPassword(value)).toBe(true); }); - // write more tests here + it(`should return 'true' for the valid password with 9 characters`, () => { + const value = generatePassword(9, '1^B'); + + expect(checkPassword(value)).toBe(true); + }); + + it(`should return 'true' for the valid password with 15 characters`, () => { + const value = generatePassword(15, '1^B'); + + expect(checkPassword(value)).toBe(true); + }); + + it(`should return 'true' for the valid password with 16 characters`, () => { + const value = generatePassword(16, '1^B'); + + expect(checkPassword(value)).toBe(true); + }); + + it(`should return 'false' for the valid password with 7 characters`, () => { + const value = generatePassword(7, '1^B'); + + expect(checkPassword(value)).toBe(false); + }); + + it(`should return 'false' for the valid password with 17 characters`, () => { + const value = generatePassword(17, '1^B'); + + expect(checkPassword(value)).toBe(false); + }); + + it(`should return 'false' if ` + + `password doesn't contain any digit`, () => { + const value = generatePassword(10, '^B'); + + expect(checkPassword(value)).toBe(false); + }); + + it(`should return 'false' if ` + + `password doesn't contain any special character`, () => { + const value = generatePassword(10, '1B'); + + expect(checkPassword(value)).toBe(false); + }); + + it(`should return 'false' if ` + + `password doesn't contain any upperCase`, () => { + const value = generatePassword(10, '1^'); + + expect(checkPassword(value)).toBe(false); + }); + + it(`should return 'false' if ` + + `password contains non-Latin alphabet`, () => { + const value = generatePassword(10, '1^BaŁ]\\'); + + expect(checkPassword(value)).toBe(false); + }); }); From b06d33af53e32277135ffd7a553469c6931b202b Mon Sep 17 00:00:00 2001 From: Maciej Date: Thu, 31 Jul 2025 14:44:48 +0200 Subject: [PATCH 2/2] Fixed --- src/checkPassword.js | 4 ++-- src/checkPassword.test.js | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/checkPassword.js b/src/checkPassword.js index c69eb62..fd65419 100644 --- a/src/checkPassword.js +++ b/src/checkPassword.js @@ -8,8 +8,8 @@ function checkPassword(password) { // eslint-disable-next-line const validPasswordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/; - const doHaveOtherLetters = /[^a-z0-9~`!@#$%^&*()-_+={}[\]|\\/:;"'<>,.?]/i - .test(password); + const doHaveOtherLetters = password.match(/[a-z]/ig).length + !== password.match(/\p{Letter}/uig).length; // eslint-disable-next-line if (password.match(validPasswordRegex) && !doHaveOtherLetters) { diff --git a/src/checkPassword.test.js b/src/checkPassword.test.js index 6dffb70..6acb0ee 100644 --- a/src/checkPassword.test.js +++ b/src/checkPassword.test.js @@ -97,8 +97,16 @@ describe(`Function 'checkPassword':`, () => { it(`should return 'false' if ` + `password contains non-Latin alphabet`, () => { - const value = generatePassword(10, '1^BaŁ]\\'); - - expect(checkPassword(value)).toBe(false); + const value = [ + generatePassword(10, '1^BaŁ]\\'), + generatePassword(10, '1^Baџ]\\'), + generatePassword(10, '1^Baó]\\'), + generatePassword(10, '1^Baä]\\'), + ]; + + value.forEach((password, i) => { + expect(checkPassword(password)) + .toBe(false); + }); }); });