Skip to content

Commit 2029fba

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

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

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: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,28 @@ const testUser = {
99
location: {
1010
name: 'Valhalla',
1111
centre: 'Nairobi',
12-
country: 'Kenya',
13-
},
12+
country: 'Kenya'
13+
}
1414
};
1515

1616
const newUser = {
1717
...testUser,
1818
19-
username: 'Oliver Brice',
19+
username: 'Oliver Brice'
20+
};
21+
22+
const newUserCaps = {
23+
...testUser,
24+
25+
username: 'Oliver Brice'
2026
};
2127

2228
jest.mock('nodemailer', () => ({
2329
createTransport: () => ({
2430
sendMail: (options, call) => {
2531
call();
26-
},
27-
}),
32+
}
33+
})
2834
}));
2935

3036
jest.mock('axios', () => ({
@@ -33,10 +39,10 @@ jest.mock('axios', () => ({
3339
get: () => ({
3440
data: {
3541
values: [{}],
36-
total: 1,
37-
},
38-
}),
39-
}),
42+
total: 1
43+
}
44+
})
45+
})
4046
}));
4147

4248
describe('User tests', () => {
@@ -56,6 +62,15 @@ describe('User tests', () => {
5662
});
5763
});
5864

65+
it('should normalize email', done => {
66+
sendRequest('post', '/api/users', newUser, () => {
67+
sendRequest('post', '/api/users', newUserCaps, (err, res) => {
68+
expect(res.text).toMatch('email must be unique');
69+
done();
70+
});
71+
});
72+
});
73+
5974
it('should login an authorised user', done => {
6075
sendRequest(
6176
'post',
@@ -139,7 +154,7 @@ describe('User tests', () => {
139154
{
140155
141156
roleId: 3,
142-
locationId: 'cjee24cz40000guxs6bdner6l',
157+
locationId: 'cjee24cz40000guxs6bdner6l'
143158
},
144159
(err, res) => {
145160
expect(res.body.data.username).toEqual('Oliver Munala');
@@ -155,7 +170,7 @@ describe('User tests', () => {
155170
{
156171
157172
roleId: 3,
158-
locationId: 'cjee24cz40000guxs6bdner6l',
173+
locationId: 'cjee24cz40000guxs6bdner6l'
159174
},
160175
(err, res) => {
161176
expect(res.body.message).toMatch(
@@ -173,7 +188,7 @@ describe('User tests', () => {
173188
{
174189
175190
roleId: 3,
176-
locationId: 'cjee24cz40000guxs6bdner6l',
191+
locationId: 'cjee24cz40000guxs6bdner6l'
177192
},
178193
(err, res) => {
179194
expect(res.body.message).toMatch(
@@ -191,7 +206,7 @@ describe('User tests', () => {
191206
{
192207
193208
roleId: 1,
194-
locationId: 'cjee24n0n0000hfxsefer9tjh',
209+
locationId: 'cjee24n0n0000hfxsefer9tjh'
195210
},
196211
(err, res) => {
197212
expect(res.body.data.username).toEqual('Batian Sss');

0 commit comments

Comments
 (0)