From 9ce0616e5be59e340afd3374d4d47ca3c946b8a0 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 21 Aug 2019 12:33:39 +0300 Subject: [PATCH] bug(users): Fix email duplication - normalizes email upon signup [Delivers #167975800] --- src/server/middlewares/validateBody.js | 4 ++++ src/tests/users.spec.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/server/middlewares/validateBody.js b/src/server/middlewares/validateBody.js index c248ffee..64bbc9c5 100644 --- a/src/server/middlewares/validateBody.js +++ b/src/server/middlewares/validateBody.js @@ -1,6 +1,10 @@ const Joi = require('joi'); module.exports = (req, res, next, schema) => { + if (req.body.email) { + const email = req.body.email; + req.body.email = email.toLowerCase(); + } Joi.validate(req.body, schema, error => { if (error) { return res.status(400).send({ message: error.details[0].message }); diff --git a/src/tests/users.spec.js b/src/tests/users.spec.js index 1bf7dc03..2829e823 100644 --- a/src/tests/users.spec.js +++ b/src/tests/users.spec.js @@ -19,6 +19,13 @@ const newUser = { username: 'Oliver Brice', }; +const newUserCaps = { + ...testUser, + email: 'oliver.BRICE@andela.com', + username: 'Oliver Brice', +}; + + jest.mock('nodemailer', () => ({ createTransport: () => ({ sendMail: (options, call) => { @@ -56,6 +63,15 @@ describe('User tests', () => { }); }); + it('should normalize email', done => { + sendRequest('post', '/api/users', newUser, () => { + sendRequest('post', '/api/users', newUserCaps, (err, res) => { + expect(res.text).toMatch('email must be unique'); + done(); + }); + }); + }); + it('should login an authorised user', done => { sendRequest( 'post',