From 2029fba4c2a82c5d86d2d1ea18d46e89721c4c7b 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 | 41 ++++++++++++++++++-------- 2 files changed, 32 insertions(+), 13 deletions(-) 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..2289013b 100644 --- a/src/tests/users.spec.js +++ b/src/tests/users.spec.js @@ -9,22 +9,28 @@ const testUser = { location: { name: 'Valhalla', centre: 'Nairobi', - country: 'Kenya', - }, + country: 'Kenya' + } }; const newUser = { ...testUser, email: 'oliver.brice@andela.com', - username: 'Oliver Brice', + username: 'Oliver Brice' +}; + +const newUserCaps = { + ...testUser, + email: 'oliver.BRICE@andela.com', + username: 'Oliver Brice' }; jest.mock('nodemailer', () => ({ createTransport: () => ({ sendMail: (options, call) => { call(); - }, - }), + } + }) })); jest.mock('axios', () => ({ @@ -33,10 +39,10 @@ jest.mock('axios', () => ({ get: () => ({ data: { values: [{}], - total: 1, - }, - }), - }), + total: 1 + } + }) + }) })); describe('User tests', () => { @@ -56,6 +62,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', @@ -139,7 +154,7 @@ describe('User tests', () => { { email: 'oliver.munala@andela.com', roleId: 3, - locationId: 'cjee24cz40000guxs6bdner6l', + locationId: 'cjee24cz40000guxs6bdner6l' }, (err, res) => { expect(res.body.data.username).toEqual('Oliver Munala'); @@ -155,7 +170,7 @@ describe('User tests', () => { { email: 'joseph.nzau@andela.com', roleId: 3, - locationId: 'cjee24cz40000guxs6bdner6l', + locationId: 'cjee24cz40000guxs6bdner6l' }, (err, res) => { expect(res.body.message).toMatch( @@ -173,7 +188,7 @@ describe('User tests', () => { { email: 'joseph.nzau@gmail.com', roleId: 3, - locationId: 'cjee24cz40000guxs6bdner6l', + locationId: 'cjee24cz40000guxs6bdner6l' }, (err, res) => { expect(res.body.message).toMatch( @@ -191,7 +206,7 @@ describe('User tests', () => { { email: 'batian.sss@andela.com', roleId: 1, - locationId: 'cjee24n0n0000hfxsefer9tjh', + locationId: 'cjee24n0n0000hfxsefer9tjh' }, (err, res) => { expect(res.body.data.username).toEqual('Batian Sss');