Skip to content

Commit 3589ff0

Browse files
committed
bug(users): Fix email duplication
- normalizes email upon signup [Delivers #167975800]
1 parent 7c63683 commit 3589ff0

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/server/middlewares/validateBody.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const Joi = require('joi');
22

33
module.exports = (req, res, next, schema) => {
4+
if (req.body.email) {
5+
const email = req.body.email;
6+
req.body.email = email.toLowerCase();
7+
}
48
Joi.validate(req.body, schema, error => {
59
if (error) {
610
return res.status(400).send({ message: error.details[0].message });

src/tests/users.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ const newUser = {
1919
username: 'Oliver Brice',
2020
};
2121

22+
const newUserCaps = {
23+
...testUser,
24+
email: 'oliver.BRICE@andela.com',
25+
username: 'Oliver Brice',
26+
};
27+
28+
2229
jest.mock('nodemailer', () => ({
2330
createTransport: () => ({
2431
sendMail: (options, call) => {
@@ -56,6 +63,15 @@ describe('User tests', () => {
5663
});
5764
});
5865

66+
it('should normalize email', done => {
67+
sendRequest('post', '/api/users', newUser, () => {
68+
sendRequest('post', '/api/users', newUserCaps, (err, res) => {
69+
expect(res.text).toMatch('email must be unique');
70+
done();
71+
});
72+
});
73+
});
74+
5975
it('should login an authorised user', done => {
6076
sendRequest(
6177
'post',
@@ -78,7 +94,7 @@ describe('User tests', () => {
7894
done();
7995
}
8096
);
81-
});
97+
});
8298

8399
it('should not login an user with missing email', done => {
84100
sendRequest('post', '/api/users/login', {}, (err, res) => {

0 commit comments

Comments
 (0)