-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add registration validation errors
- Loading branch information
Mireille Niwemuhuza
authored and
Mireille Niwemuhuza
committed
Jun 11, 2019
1 parent
d20a2c8
commit 78deff8
Showing
17 changed files
with
556 additions
and
28 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
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Router } from 'express'; | ||
import authController from '../controllers/auth'; | ||
import signupValidation from '../../middleware/validateBody'; | ||
import emailValidation from '../../middleware/userExists'; | ||
import usernameValidation from '../../middleware/usernameExists'; | ||
|
||
const authRouter = Router(); | ||
const { signup } = authController; | ||
|
||
authRouter.post('/signup', signup); | ||
authRouter.post('/signup', signupValidation, usernameValidation.usernameExist, emailValidation.userExist, signup); | ||
|
||
export default authRouter; |
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
File renamed without changes.
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,49 @@ | ||
import Joi from '@hapi/joi'; | ||
|
||
export default { | ||
signup: (userData) => { | ||
const schema = { | ||
firstName: Joi.string() | ||
.trim() | ||
.required() | ||
.regex(/^[A-Za-z_-]+$/) | ||
.min(3) | ||
.label('First name is required, it must have at least 3 letters and must contain only letters'), | ||
lastName: Joi.string() | ||
.trim() | ||
.required() | ||
.regex(/^[A-Za-z_.-]+$/) | ||
.min(3) | ||
.label('Last name is required, it must have at least 3 letters and must contain only letters'), | ||
username: Joi.string() | ||
.trim() | ||
.lowercase() | ||
.required() | ||
.regex(/^[a-zA-Z0-9_.-]+$/) | ||
.min(3) | ||
.label('Username is required, it must have at least 3 letters and must contain only letters, numbers, underscores(_), hyphens (-) and points (.)'), | ||
email: Joi.string() | ||
.trim() | ||
.lowercase() | ||
.email() | ||
.required() | ||
.label('Email is required and should look like this : example@email.com!'), | ||
password: Joi.string() | ||
.trim() | ||
.regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/) | ||
.required() | ||
.label('Password is required and must be at least 8 letters containing' | ||
+ ' at least a number a Lowercase letter and an Uppercase letter'), | ||
confirmPassword: Joi.any() | ||
.required() | ||
.valid(Joi.ref('password')) | ||
.label('Password and Confirm Password do not match'), | ||
bio: Joi.string(), | ||
image: Joi.string(), | ||
dateOfBirth: Joi.string(), | ||
gender: Joi.string() | ||
}; | ||
|
||
return Joi.validate(userData, schema, { abortEarly: false }); | ||
}, | ||
}; |
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,23 @@ | ||
import models from '../sequelize/models'; | ||
|
||
const checkEmail = { | ||
async userExist(req, res, next) { | ||
const { email } = req.body; | ||
await models.User.findAll({ | ||
where: { | ||
}, | ||
}).then((data) => { | ||
if (data.length > 0) { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: 'This email is already in use', | ||
}); | ||
} | ||
}); | ||
|
||
next(); | ||
}, | ||
}; | ||
|
||
export default checkEmail; |
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,23 @@ | ||
import models from '../sequelize/models'; | ||
|
||
const usernameAvailability = { | ||
async usernameExist(req, res, next) { | ||
const { username } = req.body; | ||
await models.User.findAll({ | ||
where: { | ||
username | ||
}, | ||
}).then((data) => { | ||
if (data.length > 0) { | ||
return res.status(400).json({ | ||
status: 400, | ||
message: 'This username is not available, Please choose another one!', | ||
}); | ||
} | ||
}); | ||
|
||
next(); | ||
}, | ||
}; | ||
|
||
export default usernameAvailability; |
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,24 @@ | ||
import validate from '../helpers/signupSchema'; | ||
|
||
|
||
const validateBody = (req, res, next) => { | ||
const data = req.body; | ||
const err = validate.signup(data); | ||
|
||
if (err.error === null) { | ||
req.body = data; | ||
next(); | ||
} else { | ||
const allErrors = []; | ||
|
||
err.error.details.forEach((errors) => { | ||
allErrors.push(errors.context.label); | ||
}); | ||
|
||
return res.status(400).send({ | ||
message: allErrors, | ||
}); | ||
} | ||
}; | ||
|
||
export default validateBody; |
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,48 @@ | ||
|
||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstName: { | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
}, | ||
username: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
bio: { | ||
type: Sequelize.STRING | ||
}, | ||
image: { | ||
type: Sequelize.STRING | ||
}, | ||
dateOfBirth: { | ||
type: Sequelize.DATE | ||
}, | ||
gender: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Users') | ||
}; |
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,16 @@ | ||
|
||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const User = sequelize.define('User', { | ||
firstName: DataTypes.STRING, | ||
lastName: DataTypes.STRING, | ||
username: DataTypes.STRING, | ||
email: DataTypes.STRING, | ||
password: DataTypes.STRING, | ||
bio: DataTypes.STRING, | ||
image: DataTypes.STRING, | ||
dateOfBirth: DataTypes.DATE, | ||
gender: DataTypes.STRING | ||
}, {}); | ||
return User; | ||
}; |
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,23 @@ | ||
|
||
|
||
module.exports = { | ||
up: queryInterface => | ||
/* | ||
Add altering commands here. | ||
Return a promise to correctly handle asynchronicity. | ||
*/ | ||
|
||
queryInterface.bulkInsert('Users', [{ | ||
firstName: 'Mireille', | ||
lastName: 'Niwemuhuza', | ||
username: 'mifeille', | ||
email: 'nimilleer@gmail.com', | ||
password: 'Mireille1!', | ||
bio: '', | ||
image: '', | ||
dateOfBirth: '12/12/2000', | ||
gender: '', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}], {}), | ||
}; |
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,67 @@ | ||
import chaiHttp from 'chai-http'; | ||
import chai from 'chai'; | ||
import dotenv from 'dotenv'; | ||
import server from '../src/index'; | ||
|
||
|
||
dotenv.config(); | ||
|
||
const { expect } = chai; | ||
chai.use(chaiHttp); | ||
|
||
|
||
describe('User Registration', () => { | ||
it('should not let a user signup without valid credentials ', (done) => { | ||
chai.request(server) | ||
.post('/api/auth/signup') | ||
.send({ | ||
firstName: 'Emy', | ||
lastName: 'Rukundo', | ||
username: 'emy2', | ||
email: 'rukundogmail.com', | ||
password: 'Rukundo1!', | ||
confirmPassword: 'Rukundo1!' | ||
}) | ||
.end((err, res) => { | ||
expect(res.status).to.equal(400); | ||
expect(res.body).to.be.an('object'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not let a user signup with an already existing email ', (done) => { | ||
chai.request(server) | ||
.post('/api/auth/signup') | ||
.send({ | ||
firstName: 'Emy', | ||
lastName: 'Rukundo', | ||
username: 'mifeillee', | ||
email: 'nimilleer@gmail.com', | ||
password: 'Rukundo1!', | ||
confirmPassword: 'Rukundo1!' | ||
}) | ||
.end((err, res) => { | ||
expect(res.status).to.equal(400); | ||
expect(res.body).to.be.an('object'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not let a user signup with an already existing username ', (done) => { | ||
chai.request(server) | ||
.post('/api/auth/signup') | ||
.send({ | ||
firstName: 'Emy', | ||
lastName: 'Rukundo', | ||
username: 'mifeille', | ||
email: 'nimiller@gmail.com', | ||
password: 'Rukundo1!', | ||
confirmPassword: 'Rukundo1!' | ||
}) | ||
.end((err, res) => { | ||
expect(res.status).to.equal(400); | ||
expect(res.body).to.be.an('object'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.