Skip to content

Commit

Permalink
bug(users): Fix email duplication
Browse files Browse the repository at this point in the history
- normalizes email upon signup

[Delivers #167975800]
  • Loading branch information
leonardnjura committed Aug 21, 2019
1 parent 7c63683 commit 2029fba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/server/middlewares/validateBody.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down
41 changes: 28 additions & 13 deletions src/tests/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@ const testUser = {
location: {
name: 'Valhalla',
centre: 'Nairobi',
country: 'Kenya',
},
country: 'Kenya'
}
};

const newUser = {
...testUser,
email: '[email protected]',
username: 'Oliver Brice',
username: 'Oliver Brice'
};

const newUserCaps = {
...testUser,
email: '[email protected]',
username: 'Oliver Brice'
};

jest.mock('nodemailer', () => ({
createTransport: () => ({
sendMail: (options, call) => {
call();
},
}),
}
})
}));

jest.mock('axios', () => ({
Expand All @@ -33,10 +39,10 @@ jest.mock('axios', () => ({
get: () => ({
data: {
values: [{}],
total: 1,
},
}),
}),
total: 1
}
})
})
}));

describe('User tests', () => {
Expand All @@ -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',
Expand Down Expand Up @@ -139,7 +154,7 @@ describe('User tests', () => {
{
email: '[email protected]',
roleId: 3,
locationId: 'cjee24cz40000guxs6bdner6l',
locationId: 'cjee24cz40000guxs6bdner6l'
},
(err, res) => {
expect(res.body.data.username).toEqual('Oliver Munala');
Expand All @@ -155,7 +170,7 @@ describe('User tests', () => {
{
email: '[email protected]',
roleId: 3,
locationId: 'cjee24cz40000guxs6bdner6l',
locationId: 'cjee24cz40000guxs6bdner6l'
},
(err, res) => {
expect(res.body.message).toMatch(
Expand All @@ -173,7 +188,7 @@ describe('User tests', () => {
{
email: '[email protected]',
roleId: 3,
locationId: 'cjee24cz40000guxs6bdner6l',
locationId: 'cjee24cz40000guxs6bdner6l'
},
(err, res) => {
expect(res.body.message).toMatch(
Expand All @@ -191,7 +206,7 @@ describe('User tests', () => {
{
email: '[email protected]',
roleId: 1,
locationId: 'cjee24n0n0000hfxsefer9tjh',
locationId: 'cjee24n0n0000hfxsefer9tjh'
},
(err, res) => {
expect(res.body.data.username).toEqual('Batian Sss');
Expand Down

0 comments on commit 2029fba

Please sign in to comment.