Skip to content

Commit

Permalink
Merge 5b7d239 into 70cdda1
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrismarcel committed Mar 19, 2019
2 parents 70cdda1 + 5b7d239 commit e5e7db3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
27 changes: 25 additions & 2 deletions server/middlewares/ValidateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,34 @@ class ValidateUser {
.withMessage('Email is invalid.'),

check('password')
.optional()
.exists()
.withMessage('Password field cannot be blank.')
.isAlphanumeric('en-GB')
.withMessage('Password must be Alphanumeric.')
.isLength({ min: 8 })
.withMessage('Password cannot be less than 8 characters.')
.withMessage('Password cannot be less than 8 characters.'),

check('firstname')
.exists()
.withMessage('First name field must be specified.')
.isLength({ min: 1 })
.withMessage('First name must contain at least 1 character.')
.isAlpha()
.withMessage('First name can only contain alphabetic characters.'),

check('lastname')
.exists()
.withMessage('Last name field must be specified.')
.isLength({ min: 1 })
.withMessage('Last name must contain at least 1 character.')
.isAlpha()
.withMessage('Last name can only contain alphabetic characters.'),

check('username')
.exists()
.withMessage('Username field must be specified.')
.isLength({ min: 3 })
.withMessage('Username must not be less than 3 characters.')
];
}

Expand Down
111 changes: 111 additions & 0 deletions server/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,117 @@ describe('Test signup endpoint and email verification endpoint', () => {
});
});

it('should return 400 if firstname is empty', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
lastname: 'Doe',
username: 'john45',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { firstname } = res.body.errors;
expect(firstname[0]).to.be.equal('First name field must be specified.');
done(err);
});
});

it('should return 400 if firstname contains non-alphabetic characters', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
firstname: 'John124',
lastname: 'Doe',
username: 'john45',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { firstname } = res.body.errors;
expect(firstname[0]).to.be.equal('First name can only contain alphabetic characters.');
done(err);
});
});

it('should return 400 if lastname is empty', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
firstname: 'John',
username: 'john45',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { lastname } = res.body.errors;
expect(lastname[0]).to.be.equal('Last name field must be specified.');
done(err);
});
});

it('should return 400 if lastname contains non-alphabetic characters', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
firstname: 'John',
lastname: 'John123',
username: 'john45',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { lastname } = res.body.errors;
expect(lastname[0]).to.be.equal('Last name can only contain alphabetic characters.');
done(err);
});
});

it('should return 400 if username is empty', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
firstname: 'John',
lastname: 'Doe',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { username } = res.body.errors;
expect(username[0]).to.be.equal('Username field must be specified.');
done(err);
});
});

it('should return 400 if username is less than 3 characters', (done) => {
chai
.request(app)
.post(signupURL)
.send({
email: 'johndoe@gmail.com',
firstname: 'John',
lastname: 'Doe',
username: 'Je',
password: '12345678'
})
.end((err, res) => {
expect(res.status).to.equal(400);
const { username } = res.body.errors;
expect(username[0]).to.be.equal('Username must not be less than 3 characters.');
done(err);
});
});

it('should return 400 if email is invalid', (done) => {
chai
.request(app)
Expand Down

0 comments on commit e5e7db3

Please sign in to comment.