diff --git a/src/checkPassword.js b/src/checkPassword.js index 1a6fc05..d167503 100644 --- a/src/checkPassword.js +++ b/src/checkPassword.js @@ -6,12 +6,21 @@ * @returns {boolean} */ function checkPassword(password) { - // eslint-disable-next-line - const validPasswordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/; - const cyrillicValidation = /^((?![А-Яа-я]).)*$/; + if (typeof password !== 'string') return false; - // eslint-disable-next-line - if (password.match(validPasswordRegex) && password.match(cyrillicValidation)) { + // regex sprawdzający: + // - co najmniej 1 wielką literę + // - co najmniej 1 cyfrę + // - co najmniej 1 znak specjalny + // - brak spacji + // - długość 8-16 + const validPasswordRegex = + /^(?=.*\d)(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/; + + // regex sprawdzający tylko znaki ASCII od ! do ~ (bez spacji) + const asciiValidation = /^[\x21-\x7E]*$/; + + if (validPasswordRegex.test(password) && asciiValidation.test(password)) { return true; } @@ -19,3 +28,5 @@ function checkPassword(password) { } module.exports = checkPassword; + + diff --git a/src/checkPassword.test.js b/src/checkPassword.test.js index 1e77e16..8d4e112 100644 --- a/src/checkPassword.test.js +++ b/src/checkPassword.test.js @@ -1,19 +1,75 @@ 'use strict'; -describe(`Function 'checkPassword':`, () => { - const checkPassword = require('./checkPassword'); +const checkPassword = require('./checkPassword'); +describe(`Function 'checkPassword':`, () => { it(`should be declared`, () => { expect(checkPassword).toBeInstanceOf(Function); }); - it(`should return boolean`, () => { + it(`should return a boolean`, () => { + const result = checkPassword('Password1!'); + expect(typeof result).toBe('boolean'); + }); + // README examples + it(`should return true for checkPassword('Password1!')`, () => { + expect(checkPassword('Password1!')).toBe(true); }); - it(`should return 'true' for the valid password with 8 characters`, () => { + it(`should return false for checkPassword('qwerty')`, () => { + expect(checkPassword('qwerty')).toBe(false); + }); + it(`should return false for checkPassword('Str@ng')`, () => { + expect(checkPassword('Str@ng')).toBe(false); }); - // write more tests here + // Length boundaries + it(`should return false for 7-character password`, () => { + expect(checkPassword('Abcde1!')).toBe(false); + }); + + it(`should return true for 8-character valid password`, () => { + expect(checkPassword('Abcdef1!')).toBe(true); + }); + + it(`should return true for 16-character valid password`, () => { + expect(checkPassword('Abcdefgh1234!XYZ')).toBe(true); + }); + + it(`should return false for 17-character password`, () => { + // 15 liter + 1 cyfra + 1 znak specjalny = 17 znaków + expect(checkPassword('Abcdefghijklmno1!')).toBe(false); + }); + + // Character class coverage + it(`should return false for password without uppercase`, () => { + expect(checkPassword('abcdef1!')).toBe(false); + }); + + it(`should return false for password without digit`, () => { + expect(checkPassword('Abcdefgh!')).toBe(false); + }); + + it(`should return false for password without special char`, () => { + expect(checkPassword('Abcdefg1')).toBe(false); + }); + + it(`should return true for uppercase-only valid password`, () => { + expect(checkPassword('ABCDEF12!')).toBe(true); + }); + + it(`should return false for password with Cyrillic letters`, () => { + expect(checkPassword('Пароль1!')).toBe(false); + }); + + it(`should return false for password with accented Latin letters`, () => { + expect(checkPassword('Pässword1!')).toBe(false); + }); + + // Whitespace check + it(`should return false for password containing whitespace`, () => { + expect(checkPassword('Abc def1!')).toBe(false); + }); });