-
Notifications
You must be signed in to change notification settings - Fork 149
Walidacja formularza rejestracji: testy email i hasła #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
92fbc66
2275078
6a4c728
e8fb4ca
406f2da
4f71606
9c3b513
b8fbac7
827ef41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,39 +2,60 @@ | |
|
|
||
| /** | ||
| * @param {string} email | ||
| * | ||
| * @param {string} password | ||
| * | ||
| * @returns {object} | ||
| */ | ||
| function validateRegisterForm(email, password) { | ||
| // eslint-disable-next-line max-len | ||
| const validPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,16}$/; | ||
| // Password regex: krótkie zmienne | ||
| const n = '(?=.*\\d)'; | ||
| const l = '(?=.*[a-z])'; | ||
| const u = '(?=.*[A-Z])'; | ||
| const s = '(?=.*[^a-zA-Z0-9])'; | ||
|
||
| const ns = '(?!.*\\s)'; | ||
| const len = '.{8,16}'; | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| const validEmailMask = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\./i); | ||
| const validPassword = new RegExp('^' + n + l + u + s + ns + len + '$'); | ||
|
||
|
|
||
| if (!email.match(validEmailMask) && password.match(validPassword)) { | ||
| return { | ||
| code: 422, message: 'Email is invalid.', | ||
| }; | ||
| // Email regex: krótkie zmienne | ||
| const loc = '([\\w-]+(?:\\.[\\w-]+)*)'; | ||
| const dom = '([\\w-]+(?:\\.[\\w-]+)+)'; | ||
| const at = '@'; | ||
| const validEmailMask = new RegExp('^' + loc + at + dom + '$', 'i'); | ||
|
||
|
|
||
| const isEmailValid = validEmailMask.test(email); | ||
| const isPasswordValid = validPassword.test(password); | ||
|
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code currently runs the email and password tests by using the parameter names |
||
|
|
||
| // Dodatkowe reguły dla emaila | ||
| let emailExtraValid = true; | ||
| if (isEmailValid) { | ||
| const parts = email.split('@'); | ||
| const local = parts[0]; | ||
| const domain = parts[1]; | ||
|
|
||
| const localInvalid = local.startsWith('.'); | ||
| const domainInvalid = domain.startsWith('.'); | ||
| const dotsInvalid = email.includes('..'); | ||
|
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additional checks for leading dots and double dots ( |
||
|
|
||
| if (localInvalid || domainInvalid || dotsInvalid) { | ||
| emailExtraValid = false; | ||
| } | ||
| } | ||
|
|
||
| if (email.match(validEmailMask) && !password.match(validPassword)) { | ||
| return { | ||
| code: 422, message: 'Password is invalid.', | ||
| }; | ||
| // Walidacja wyników | ||
| if (!isEmailValid || !emailExtraValid) { | ||
| if (isPasswordValid) { | ||
| return { code: 422, message: 'Email is invalid.' }; | ||
| } else { | ||
| return { code: 500, message: 'Password and email are invalid.' }; | ||
|
||
| } | ||
| } | ||
|
|
||
| if (!email.match(validEmailMask) && !password.match(validPassword)) { | ||
| return { | ||
| code: 500, message: 'Password and email are invalid.', | ||
| }; | ||
| if (!isPasswordValid) { | ||
| return { code: 422, message: 'Password is invalid.' }; | ||
| } | ||
|
|
||
| return { | ||
| code: 200, message: 'Email and password are valid.', | ||
| }; | ||
| return { code: 200, message: 'Email and password are valid.' }; | ||
| } | ||
|
|
||
| module.exports = validateRegisterForm; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,23 +8,70 @@ describe(`Function 'validateRegisterForm':`, () => { | |
| }); | ||
|
|
||
| it(`should return object`, () => { | ||
| expect(typeof validateRegisterForm('test@mail.com', 'P@ssword1!')) | ||
| .toBe('object'); | ||
| const result = validateRegisterForm('test@mail.com', 'P@ssword1!'); | ||
| expect(typeof result).toBe('object'); | ||
| }); | ||
|
|
||
| it(`should return success message for the valid input`, () => { | ||
| const isValid = validateRegisterForm('test@mail.com', 'P@ssword1!'); | ||
| it(`should return 200 for valid email and password`, () => { | ||
| const valid = validateRegisterForm('test@mail.com', 'P@ssword1!'); | ||
| expect(valid.code).toBe(200); | ||
| expect(valid.message).toBe('Email and password are valid.'); | ||
| }); | ||
|
|
||
| it(`should return 422 for valid email and password missing digit`, () => { | ||
| const invalidPass = validateRegisterForm('test@mail.com', 'P@ssword'); | ||
| expect(invalidPass.code).toBe(422); | ||
| expect(invalidPass.message).toBe('Password is invalid.'); | ||
| }); | ||
|
|
||
| expect(isValid.code).toBe(200); | ||
| expect(isValid.message).toBe('Email and password are valid.'); | ||
| it(`should return 422 for invalid email and valid password`, () => { | ||
| const invalidEmail = validateRegisterForm('test@com', 'P@ssword1!'); | ||
| expect(invalidEmail.code).toBe(422); | ||
| expect(invalidEmail.message).toBe('Email is invalid.'); | ||
| }); | ||
|
|
||
| it(`should return error for valid email and password without number`, () => { | ||
| const invalidPassword = validateRegisterForm('test@mail.com', 'P@ssword'); | ||
| it(`should return 500 for invalid email and password`, () => { | ||
| const bothInvalid = validateRegisterForm('test@com', 'ssword1'); | ||
| expect(bothInvalid.code).toBe(500); | ||
| expect(bothInvalid.message) | ||
| .toBe('Password and email are invalid.'); | ||
| }); | ||
|
|
||
| expect(invalidPassword.code).toBe(422); | ||
| expect(invalidPassword.message).toBe('Password is invalid.'); | ||
| it(`should return 422 if password too short`, () => { | ||
| const tooShort = validateRegisterForm('valid@mail.com', 'Aa1!'); | ||
| expect(tooShort.code).toBe(422); | ||
| expect(tooShort.message).toBe('Password is invalid.'); | ||
| }); | ||
|
|
||
| // write more tests here | ||
| it(`should return 422 if password too long`, () => { | ||
| const tooLong = validateRegisterForm( | ||
| 'valid@mail.com', 'A1!aaaaaaaaaaaaaaa' | ||
| ); | ||
| expect(tooLong.code).toBe(422); | ||
| expect(tooLong.message).toBe('Password is invalid.'); | ||
| }); | ||
|
|
||
| it(`should return 422 if email missing '@' symbol`, () => { | ||
| const noAt = validateRegisterForm('invalidmail.com', 'P@ssword1!'); | ||
| expect(noAt.code).toBe(422); | ||
| expect(noAt.message).toBe('Email is invalid.'); | ||
| }); | ||
|
|
||
| it(`should return 422 if email starts with dot`, () => { | ||
| const startDot = validateRegisterForm('.test@mail.com', 'P@ssword1!'); | ||
| expect(startDot.code).toBe(422); | ||
| expect(startDot.message).toBe('Email is invalid.'); | ||
| }); | ||
|
|
||
| it(`should return 422 if email has double dots`, () => { | ||
| const doubleDot = validateRegisterForm('test..mail@mail.com', 'P@ssword1!'); | ||
| expect(doubleDot.code).toBe(422); | ||
| expect(doubleDot.message).toBe('Email is invalid.'); | ||
| }); | ||
|
|
||
| it(`should return 422 if top-level domain starts with dot`, () => { | ||
| const tldDot = validateRegisterForm('test@mail..com', 'P@ssword1!'); | ||
| expect(tldDot.code).toBe(422); | ||
|
||
| expect(tldDot.message).toBe('Email is invalid.'); | ||
| }); | ||
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lowercase check
(?=.*[a-z])and uppercase check(?=.*[A-Z])only match Latin letters. The task requires that allowed letters include both English and Cyrillic (А–Я, а–я). Update these parts to include the Cyrillic ranges (for examplea-zA-Z\u0400-\u04FFor use Unicode property escapes) so uppercase detection and lowercase detection work for Cyrillic characters as well.