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
21 changes: 16 additions & 5 deletions src/checkPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
* @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;
}

return false;
}

module.exports = checkPassword;


66 changes: 61 additions & 5 deletions src/checkPassword.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
'use strict';

describe(`Function 'checkPassword':`, () => {
const checkPassword = require('./checkPassword');
const checkPassword = require('./checkPassword');

Choose a reason for hiding this comment

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

The import line is fine — require('./checkPassword') is correct and necessary for these tests. No change required here.


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');
});
Comment on lines +10 to +13

Choose a reason for hiding this comment

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

This test only asserts the return type for 'Password1!'. The task description explicitly lists checkPassword('Password1!') === true as a required example. Add a dedicated test asserting expect(checkPassword('Password1!')).toBe(true) so the example from the specification is covered exactly .


// 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);
});
});