Skip to content

Commit 5a4630a

Browse files
committed
fix: coderabbit suggested changes
1 parent 34f8031 commit 5a4630a

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

controllers/users.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,16 @@ const getUsers = async (req, res) => {
137137
}
138138

139139
if (profileStatus) {
140+
const normalizedProfileStatus = String(profileStatus).trim().toUpperCase();
140141
try {
141-
const users = await userQuery.fetchUserForKeyValue("profileStatus", profileStatus);
142+
const users = await userQuery.fetchUserForKeyValue("profileStatus", normalizedProfileStatus);
142143
return res.json({
143-
message: `Users with profile status ${profileStatus} returned successfully!`,
144+
message: `Users with profile status ${normalizedProfileStatus} returned successfully!`,
144145
count: users.length,
145146
users: users,
146147
});
147148
} catch (error) {
148-
logger.error(`Error while fetching users with profile status ${profileStatus}: ${error}`);
149+
logger.error(`Error while fetching users with profile status ${normalizedProfileStatus}: ${error}`);
149150
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
150151
}
151152
}

middlewares/validators/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ async function getUsers(req, res, next) {
197197
query: joi.string().optional(),
198198
q: joi.string().optional(),
199199
profile: joi.string().valid("true").optional(),
200-
profileStatus: joi.string().optional().messages({
200+
profileStatus: joi.string().trim().uppercase().optional().messages({
201201
"string.empty": "profileStatus value must not be empty",
202202
}),
203203
filterBy: joi.string().optional(),

test/integration/users.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,24 @@ describe("Users", function () {
10151015
return done();
10161016
});
10171017
});
1018+
1019+
it("Should accept lowercase profileStatus", function (done) {
1020+
chai
1021+
.request(app)
1022+
.get("/users?profileStatus=blocked")
1023+
.end((err, res) => {
1024+
if (err) {
1025+
return done(err);
1026+
}
1027+
expect(res).to.have.status(200);
1028+
expect(res.body).to.be.a("object");
1029+
expect(res.body.users).to.be.a("array");
1030+
res.body.users.forEach((user) => {
1031+
expect(user.profileStatus).to.equal("BLOCKED");
1032+
});
1033+
return done();
1034+
});
1035+
});
10181036
});
10191037

10201038
describe("GET /users/self", function () {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,24 @@ describe("Middleware | Validators | User", function () {
502502
expect(next.calledOnce).to.be.equal(true);
503503
});
504504

505+
it("Stops the propagation when profileStatus is empty", async function () {
506+
const req = {
507+
query: {
508+
profileStatus: "",
509+
},
510+
};
511+
const res = {
512+
boom: {
513+
badRequest: () => {},
514+
},
515+
};
516+
const nextSpy = sinon.spy();
517+
await getUsers(req, res, nextSpy).catch((err) => {
518+
expect(err).to.be.an.instanceOf(Error);
519+
});
520+
expect(nextSpy.calledOnce).to.be.equal(false);
521+
});
522+
505523
it("Stops the request for passing on to next", async function () {
506524
const req = {
507525
query: {

0 commit comments

Comments
 (0)