Skip to content

Commit

Permalink
Merge a681a67 into bb2ef45
Browse files Browse the repository at this point in the history
  • Loading branch information
abejide001 committed Jan 22, 2019
2 parents bb2ef45 + a681a67 commit 6c29712
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/middlewares/UsersValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ class UserValidator {
req.body.email = email.replace(/\s{1,}/g, '').trim().toLowerCase();
return next();
}

/**
* @description - Checks the request parameters for user login
* @param {Object} req - request
* @param {object} res - response
* @param {Object} next - Call back function
* @return {object} - status code and error message or next()
* @static
* @memberof UserValidator
*/
static UserSignInValidator(req, res, next) {
req.check('email', 'Email is required').trim().notEmpty();
req.check('email', 'Email is not valid').trim().isEmail();
req.check('password', 'Password is required').trim().notEmpty();
req.check('password', 'Minimum password length is 5 characters')
.isLength({ min: minPassLen });
const errors = req.validationErrors();
const validationErrors = [];
if (errors) {
errors.map(err => validationErrors.push(err.msg));
response = new Response(
'Bad Request',
400,
'Invalid credentials',
validationErrors
);
return res.status(response.code).json(response);
}
next();
}
}

export default UserValidator;
56 changes: 56 additions & 0 deletions test/controllers/userControllerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,62 @@ describe('Users Endpoint API Test', () => {
done(err);
});
});
it('it should not signin a user with an empty email', (done) => {
chai.request(app)
.post('/api/v1/auth/signin')
.send({
email: '',
password: 'abejidefemi1'
})
.end((err, res) => {
expect(res.body.status).eql('Bad Request');
expect(res.body.code).eql(400);
expect(res.body.messages).eql('Invalid Credentials');
done(err);
});
});
it('it should not signin a user with an empty password', (done) => {
chai.request(app)
.post('/api/v1/auth/signin')
.send({
email: 'abejdiefemi@gmail.com',
password: ''
})
.end((err, res) => {
expect(res.body.status).eql('Bad Request');
expect(res.body.code).eql(400);
expect(res.body.messages).eql('Invalid Credentials');
done(err);
});
});
it('it should not signin a user if password is less than 5', (done) => {
chai.request(app)
.post('/api/v1/auth/signin')
.send({
email: 'abejdiefemi@gmail.com',
password: 'aaa'
})
.end((err, res) => {
expect(res.body.status).eql('Bad Request');
expect(res.body.code).eql(400);
expect(res.body.messages).eql('Invalid Credentials');
done(err);
});
});
it('it should not signin a user with an invalid email', (done) => {
chai.request(app)
.post('/api/v1/auth/signin')
.send({
email: 'abejide',
password: 'abejidefemi1'
})
.end((err, res) => {
expect(res.body.status).eql('Bad Request');
expect(res.body.code).eql(400);
expect(res.body.messages).eql('Invalid Credentials');
done(err);
});
});
it('it should not sign in a user with an email that does not exist',
(done) => {
chai.request(app)
Expand Down

0 comments on commit 6c29712

Please sign in to comment.