Skip to content

Commit

Permalink
Make firstname and lastname to lowercase (#2168)
Browse files Browse the repository at this point in the history
* chore: make firstname and lastname to lowercase

* chore: remove type check
  • Loading branch information
vinit717 authored Sep 17, 2024
1 parent ac2eb4b commit 29a0d0b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ const updateUser = async (req, res, next) => {

try {
await schema.validateAsync(req.body);

if (req.body.first_name) {
req.body.first_name = req.body.first_name.toLowerCase();
}
if (req.body.last_name) {
req.body.last_name = req.body.last_name.toLowerCase();
}

next();
} catch (error) {
logger.error(`Error validating updateUser payload : ${error}`);
Expand Down
62 changes: 61 additions & 1 deletion test/unit/middlewares/user-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ describe("Middleware | Validators | User", function () {
describe("Create user validator for updateUser", function () {
it("lets the request pass to next", async function () {
const req = {
body: userData[1],
body: {
...userData[1],
first_name: "Ankush",
last_name: "Dharkar",
},
};

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

expect(nextSpy.calledOnce).to.be.equal(false);
});

it("converts first_name to lowercase before passing to next", async function () {
const req = {
body: {
first_name: "Ankush",
last_name: "Dharkar",
},
};

const res = {};
const next = sinon.spy();
await updateUser(req, res, next);

expect(req.body.first_name).to.be.equal("ankush");
expect(req.body.last_name).to.be.equal("dharkar");
expect(next.calledOnce).to.be.equal(true);
});

it("converts last_name to lowercase before passing to next", async function () {
const req = {
body: {
first_name: "ANKUSH",
last_name: "DHARKAR",
},
};

const res = {};
const next = sinon.spy();
await updateUser(req, res, next);

expect(req.body.first_name).to.be.equal("ankush");
expect(req.body.last_name).to.be.equal("dharkar");
expect(next.calledOnce).to.be.equal(true);
});

it("stops the propagation of the next if first_name and last_name are not strings", async function () {
const req = {
body: {
first_name: 12345,
last_name: true,
},
};

const res = {
boom: {
badRequest: () => {},
},
};

const nextSpy = sinon.spy();
await updateUser(req, res, nextSpy).catch((err) => {
expect(err).to.be.an.instanceOf(Error);
});

expect(nextSpy.calledOnce).to.be.equal(false);
});
});

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

0 comments on commit 29a0d0b

Please sign in to comment.