Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 = /[^a-z0-9~`!@#$%^&*()-_+={}[\]|\\/:;"'<>,.?]/i
.test(password);

Choose a reason for hiding this comment

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

Potential issue: The regular expression used for doHaveOtherLetters may not correctly exclude all unwanted characters due to the inclusion of special characters in the character class. Double-check if this matches the task requirements for allowed characters. If the checklist specifies an exact set of allowed characters, ensure that the regex is accurate.


// 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
87 changes: 86 additions & 1 deletion src/checkPassword.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});