Skip to content

Commit 29a0d0b

Browse files
authored
Make firstname and lastname to lowercase (#2168)
* chore: make firstname and lastname to lowercase * chore: remove type check
1 parent ac2eb4b commit 29a0d0b

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

middlewares/validators/user.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ const updateUser = async (req, res, next) => {
6161

6262
try {
6363
await schema.validateAsync(req.body);
64+
65+
if (req.body.first_name) {
66+
req.body.first_name = req.body.first_name.toLowerCase();
67+
}
68+
if (req.body.last_name) {
69+
req.body.last_name = req.body.last_name.toLowerCase();
70+
}
71+
6472
next();
6573
} catch (error) {
6674
logger.error(`Error validating updateUser payload : ${error}`);

test/unit/middlewares/user-validator.test.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ describe("Middleware | Validators | User", function () {
9494
describe("Create user validator for updateUser", function () {
9595
it("lets the request pass to next", async function () {
9696
const req = {
97-
body: userData[1],
97+
body: {
98+
...userData[1],
99+
first_name: "Ankush",
100+
last_name: "Dharkar",
101+
},
98102
};
99103

100104
const res = {};
@@ -310,6 +314,62 @@ describe("Middleware | Validators | User", function () {
310314

311315
expect(nextSpy.calledOnce).to.be.equal(false);
312316
});
317+
318+
it("converts first_name to lowercase before passing to next", async function () {
319+
const req = {
320+
body: {
321+
first_name: "Ankush",
322+
last_name: "Dharkar",
323+
},
324+
};
325+
326+
const res = {};
327+
const next = sinon.spy();
328+
await updateUser(req, res, next);
329+
330+
expect(req.body.first_name).to.be.equal("ankush");
331+
expect(req.body.last_name).to.be.equal("dharkar");
332+
expect(next.calledOnce).to.be.equal(true);
333+
});
334+
335+
it("converts last_name to lowercase before passing to next", async function () {
336+
const req = {
337+
body: {
338+
first_name: "ANKUSH",
339+
last_name: "DHARKAR",
340+
},
341+
};
342+
343+
const res = {};
344+
const next = sinon.spy();
345+
await updateUser(req, res, next);
346+
347+
expect(req.body.first_name).to.be.equal("ankush");
348+
expect(req.body.last_name).to.be.equal("dharkar");
349+
expect(next.calledOnce).to.be.equal(true);
350+
});
351+
352+
it("stops the propagation of the next if first_name and last_name are not strings", async function () {
353+
const req = {
354+
body: {
355+
first_name: 12345,
356+
last_name: true,
357+
},
358+
};
359+
360+
const res = {
361+
boom: {
362+
badRequest: () => {},
363+
},
364+
};
365+
366+
const nextSpy = sinon.spy();
367+
await updateUser(req, res, nextSpy).catch((err) => {
368+
expect(err).to.be.an.instanceOf(Error);
369+
});
370+
371+
expect(nextSpy.calledOnce).to.be.equal(false);
372+
});
313373
});
314374

315375
describe("Test the update Self Validator for disabled_roles property", function () {

0 commit comments

Comments
 (0)