This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validations): signup-validations
- integrate express-validator - return descriptive error messages on user signup - write tests [Finishes #161291004]
- Loading branch information
1 parent
87c7376
commit be41dbc
Showing
7 changed files
with
521 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
export const userDataWithEmptyName = { | ||
email: 'augustine.ezinwa@andela.com', | ||
password: '@August12', | ||
fullName: '' | ||
}; | ||
export const userDataWithNoLastName = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: ' Augustine ', | ||
}; | ||
export const userDataWithThreeNames = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: ' Augustine Emeka Ezinwa', | ||
}; | ||
export const userDataWithLongName = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: ' Augustinedfekjfkdfjdjf Emrewwekaerfefjduigyguyfgftfyuftc', | ||
}; | ||
export const userDataWithInvalidName = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: ' Augustine* Ezinwa ', | ||
}; | ||
export const userDataWithNumericName = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: ' Augustine9 Ezinwa ', | ||
}; | ||
export const userDataWithInvalidEmail = { | ||
email: 'augustineeainw@@@dfjkfjer.com', | ||
password: 'Inieefjuniorer3e', | ||
fullName: 'Augustine Ezinwa' | ||
}; | ||
export const userDataWithInvalidPassword = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inieefjinio', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithShortPassword = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inief9e', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithWhiteSpacedPassword = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Inief 9e', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithEmptyPassword = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: '', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithEmptyEmail = { | ||
email: '', | ||
password: 'Augustin4334', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithInvalidPasswordAndEmail = { | ||
email: 'augustineezinwa@@@gmail.com', | ||
password: 'emekadonkey', | ||
fullName: 'Abiodun Abudu' | ||
}; | ||
export const userDataWithInvalidFields = { | ||
email: 'augustineezinwa@@@gmail.com', | ||
password: 'emekadonkey', | ||
fullName: 'Abiodun Abudu*9' | ||
}; | ||
export const userDataWithEmptyFields = { | ||
email: '', | ||
password: '', | ||
fullName: '', | ||
}; | ||
export const userDataWithAnExistingEmail = { | ||
email: 'solomon.sulaiman@andela.com', | ||
password: 'Inieefjun00', | ||
fullName: 'Augustine Ezinwa', | ||
}; | ||
export const userDataWithInvalidDataTypes = { | ||
email: true, | ||
password: false, | ||
fullName: true, | ||
bio: true, | ||
avataUrl: true | ||
}; | ||
export const userDataWithVeryLongBio = { | ||
email: 'augustineezinwa@gmail.com', | ||
password: 'Doonkey99', | ||
fullName: 'Abiodun Abudu', | ||
bio: `dkfgnowdnfgojwdgnpsdhfgnowephfnwfroidwhfoewfngodjwfnokn | ||
dsonfgoewhfosnfownfonwodnfonowdfnwnfoefdjfkdfkldjfdjfdjfdjf | ||
dfjdfjkdnfkdljfldjfnkdnfkndlfjndfnodnfldnfdnfdlnfdlkjfdf | ||
dfldnfkdnflndlfndlnfdlnfdlnfldnfldnfldnfldnfldnfldnfld | ||
dfldnfdnflkdnfldnfdlnfldnflndfdlnfdfdnfdnfdnfdlnfern3e394j3r`, | ||
avatarUrl: `fpjfjwerjeojf0e3jro3j2f0owesdfnewf/r/fefdjfdjfeojr3 | ||
dfdjfjdpjfdpjfpdjfpjdpjfpdjfpkdjfnodjfdjfojdojfdojfdjfodjfodj | ||
dfdjfdjfjdpfjkdpjfdpfjpdjfdpfjpdjfdoipjfdopjfdpjfdifjdpfjdjf | ||
dofjdpjfpdjfpdjfdjfpdjfpdjfdpoijfdsjwfpei3er//3erfje3kfkdkf`, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import models from '../models'; | ||
|
||
const { User } = models; | ||
/** | ||
* @class ValidationHelper | ||
* @description Helps perform validations on user request body. | ||
*/ | ||
class UserValidation { | ||
/** | ||
* @description - This method validates the user request body. | ||
* @param {object} req - The request object to be validated. | ||
* @param {object} res - Th response object to be validated. | ||
* @param {object} next - The callback function to the next middleware. | ||
* @returns {object} - The error object with message. | ||
* @memberOf UserValidation | ||
* @static | ||
*/ | ||
static validateUserSignUp(req, res, next) { | ||
let { fullName } = req.body; | ||
const { avatarUrl, bio } = req.body; | ||
req.checkBody('fullName', 'full name is too long').isLength({ max: 40 }); | ||
req.checkBody('email', 'please enter email').exists(); | ||
req.checkBody('email', 'please enter a valid email').isEmail(); | ||
req.checkBody('password', 'please enter password').exists(); | ||
req.checkBody('password', 'password must be at least 8 characters') | ||
.isLength({ min: 8 }); | ||
req.checkBody('password', 'password must contain a letter and number') | ||
.matches(/^((?=.*\d))(?=.*[a-zA-Z])/); | ||
if (avatarUrl) req.checkBody('avatarUrl', 'avatarUrl is invalid').isURL(); | ||
if (bio) req.checkBody('bio', 'bio is too long').isLength({ max: 200 }); | ||
const errors = req.validationErrors(); | ||
const message = []; | ||
fullName = fullName === undefined ? '' : fullName; | ||
const [firstName, ...lastName] = fullName.toString().trim().split(' '); | ||
if (lastName.join(' ').trim().includes(' ')) { | ||
message.push('You entered more than two names'); | ||
} | ||
if (!/^[a-zA-Z-]+$/.test(firstName) | ||
|| !/^[a-zA-Z-]+$/.test(lastName.join(''))) { | ||
message.push('Invalid full name'); | ||
message.push('please enter first name and last name separated by space'); | ||
} | ||
if (!errors && !message.length) { | ||
req.body.fullName = `${firstName} ${lastName.join('')}`; | ||
return next(); | ||
} | ||
if (errors) errors.forEach(err => message.push(err.msg)); | ||
return res.status(422).json({ | ||
errors: { | ||
message | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @description - This method checks if an email is already in the system. | ||
* @param {object} req - The request object bearing the email. | ||
* @param {object} res - The response object sent to the next middleware. | ||
* @param {object} next - The callback function to the next middleware. | ||
* @returns {object} - The error object with message. | ||
* @memberOf UserValidation | ||
* @static | ||
*/ | ||
static checkExistingEmail(req, res, next) { | ||
const { email } = req.body; | ||
return User.findOne({ | ||
where: { | ||
} | ||
}) | ||
.then((foundEmail) => { | ||
if (!foundEmail) return next(); | ||
return res.status(409).json({ | ||
errors: { | ||
message: ['email is already in use'] | ||
} | ||
}); | ||
}) | ||
.catch(err => res.status(500).json({ | ||
errors: { | ||
message: ['error reading user table', `${err}`] | ||
} | ||
})); | ||
} | ||
} | ||
|
||
export default UserValidation; |
Oops, something went wrong.