Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/checkPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = password.match(/[a-z]/ig).length
!== password.match(/\p{Letter}/uig).length;
Comment on lines +11 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Using password.match(/[a-z]/ig).length and password.match(/\p{Letter}/uig).length without checking for null can cause a runtime error if there are no matches. This does not safely handle passwords without Latin letters. Please update this logic to avoid possible exceptions and ensure only allowed characters are accepted, as required by the task.


// eslint-disable-next-line
if (password.match(validPasswordRegex) && password.match(cyrillicValidation)) {
if (password.match(validPasswordRegex) && !doHaveOtherLetters) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is correct, but make sure that the combination of validPasswordRegex and doHaveOtherLetters fully matches the password requirements from your checklist.

return true;
}

Expand Down
95 changes: 94 additions & 1 deletion src/checkPassword.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,111 @@

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Ł]\\'),
generatePassword(10, '1^Baџ]\\'),
generatePassword(10, '1^Baó]\\'),
generatePassword(10, '1^Baä]\\'),
];

value.forEach((password, i) => {
expect(checkPassword(password))
.toBe(false);
});
});
});