Skip to content

Commit f934b6f

Browse files
author
Ujjawal Kumar
authored
Making /users as public and adding id as query param (#1064)
* made users api route public and checking if the id is prensent in query, then it should give the resulting user * fixes #983 fixed the route of /users/userId/:id and wrote tests for them, also removed authentication * made users api route public, accepting id as query param, and written test for it * removed extra line * updated test for users * removing phone and email from user * removed obfuscating * removed obfuscating * added tests for user fields need to be present * refactored user test * refactored code for users test * code refactoring * updated user id in test of /users/?id * pushing user to the firestore db * in users test adding the user to database then fetching the same user and then testing the users/?id route with that users id * removed try catch block from users controller and in users test veryfing the response.id to be same as the testUser.id * adding and fetching user only for a test * removed token and chain code * removed token and chaincode * removed tokens and chaincode from models/users * created a common function to remove fields: phone and email * function name change * refactored users test
1 parent 65de2a4 commit f934b6f

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

controllers/users.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,40 @@ const getUserById = async (req, res) => {
6969
* @param res {Object} - Express response object
7070
*/
7171

72+
const removePersonalDetails = (user) => {
73+
const { phone, email, ...safeUser } = user;
74+
return safeUser;
75+
};
76+
7277
const getUsers = async (req, res) => {
7378
try {
7479
const query = req.query?.query ?? "";
7580
const qualifiers = getQualifiers(query);
7681

82+
// getting user details by id if present.
83+
if (req.query.id) {
84+
const id = req.query.id;
85+
let result;
86+
try {
87+
result = await userQuery.fetchUser({ userId: id });
88+
} catch (error) {
89+
logger.error(`Error while fetching user: ${error}`);
90+
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
91+
}
92+
93+
if (!result.userExists) {
94+
return res.boom.notFound("User doesn't exist");
95+
}
96+
97+
const User = { ...result.user };
98+
const user = removePersonalDetails(User);
99+
100+
return res.json({
101+
message: "User returned successfully!",
102+
user,
103+
});
104+
}
105+
77106
if (qualifiers?.filterBy) {
78107
const allPRs = await getFilteredPRsOrIssues(qualifiers);
79108

middlewares/validators/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ async function getUsers(req, res, next) {
112112
search: joi.string().optional().messages({
113113
"string.empty": "search value must not be empty",
114114
}),
115+
id: joi.string().optional().messages({
116+
"string.empty": "id value must not be empty",
117+
}),
115118
next: joi
116119
.string()
117120
.optional()

routes/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { getUserBadges } = require("../controllers/badges");
1111
router.post("/verify", authenticate, users.verifyUser);
1212
router.get("/userId/:userId", users.getUserById);
1313
router.patch("/self", authenticate, userValidator.updateUser, users.updateSelf);
14-
router.get("/", authenticate, userValidator.getUsers, users.getUsers);
14+
router.get("/", userValidator.getUsers, users.getUsers);
1515
router.get("/self", authenticate, users.getSelfDetails);
1616
router.get("/isUsernameAvailable/:username", authenticate, users.getUsernameAvailabilty);
1717
router.get("/chaincode", authenticate, users.generateChaincode);

test/integration/users.test.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ describe("Users", function () {
124124
chai
125125
.request(app)
126126
.get("/users")
127-
.set("cookie", `${cookieName}=${jwt}`)
128127
.end((err, res) => {
129128
if (err) {
130129
return done(err);
@@ -145,7 +144,6 @@ describe("Users", function () {
145144
chai
146145
.request(app)
147146
.get("/users")
148-
.set("cookie", `${cookieName}=${jwt}`)
149147
.query({
150148
size: 1,
151149
page: 0,
@@ -171,7 +169,6 @@ describe("Users", function () {
171169
chai
172170
.request(app)
173171
.get("/users")
174-
.set("cookie", `${cookieName}=${jwt}`)
175172
.query({
176173
size: -1,
177174
page: -1,
@@ -194,7 +191,6 @@ describe("Users", function () {
194191
chai
195192
.request(app)
196193
.get("/users")
197-
.set("cookie", `${cookieName}=${jwt}`)
198194
.query({
199195
size: 101,
200196
})
@@ -216,7 +212,6 @@ describe("Users", function () {
216212
chai
217213
.request(app)
218214
.get("/users?size=2")
219-
.set("cookie", `${cookieName}=${jwt}`)
220215
.end((err, res) => {
221216
if (err) {
222217
return done(err);
@@ -237,7 +232,6 @@ describe("Users", function () {
237232
chai
238233
.request(app)
239234
.get(`/users?next=${userId}&prev=${userId}&size=2`)
240-
.set("cookie", `${cookieName}=${jwt}`)
241235
.end((err, res) => {
242236
if (err) {
243237
return done(err);
@@ -254,7 +248,6 @@ describe("Users", function () {
254248
chai
255249
.request(app)
256250
.get(`/users?next=${userId}&page=1&size=2`)
257-
.set("cookie", `${cookieName}=${jwt}`)
258251
.end((err, res) => {
259252
if (err) {
260253
return done(err);
@@ -271,7 +264,6 @@ describe("Users", function () {
271264
chai
272265
.request(app)
273266
.get(`/users?page=1&prev=${userId}&size=2`)
274-
.set("cookie", `${cookieName}=${jwt}`)
275267
.end((err, res) => {
276268
if (err) {
277269
return done(err);
@@ -288,7 +280,6 @@ describe("Users", function () {
288280
chai
289281
.request(app)
290282
.get(`/users?search=an&size=2`)
291-
.set("cookie", `${cookieName}=${jwt}`)
292283
.end((err, res) => {
293284
if (err) {
294285
return done(err);
@@ -313,7 +304,6 @@ describe("Users", function () {
313304
chai
314305
.request(app)
315306
.get(`/users?page=1&size=2`)
316-
.set("cookie", `${cookieName}=${jwt}`)
317307
.end((err, res) => {
318308
if (err) {
319309
return done(err);
@@ -333,8 +323,7 @@ describe("Users", function () {
333323
});
334324

335325
it("Should get next and previous page results based upon the links in the response", async function () {
336-
const response = await chai.request(app).get(`/users?size=2`).set("cookie", `${cookieName}=${jwt}`);
337-
326+
const response = await chai.request(app).get(`/users?size=2`);
338327
expect(response).to.have.status(200);
339328
expect(response.body).to.be.a("object");
340329
expect(response.body.message).to.equal("Users returned successfully!");
@@ -343,7 +332,7 @@ describe("Users", function () {
343332
expect(response.body.links).to.have.property("prev");
344333

345334
const nextPageLink = response.body.links.next;
346-
const nextPageResponse = await chai.request(app).get(nextPageLink).set("cookie", `${cookieName}=${jwt}`);
335+
const nextPageResponse = await chai.request(app).get(nextPageLink);
347336

348337
expect(nextPageResponse).to.have.status(200);
349338
expect(nextPageResponse.body).to.be.a("object");
@@ -354,7 +343,7 @@ describe("Users", function () {
354343
expect(nextPageResponse.body.users).to.have.length(2);
355344

356345
const prevPageLink = nextPageResponse.body.links.prev;
357-
const previousPageResponse = await chai.request(app).get(prevPageLink).set("cookie", `${cookieName}=${jwt}`);
346+
const previousPageResponse = await chai.request(app).get(prevPageLink);
358347

359348
expect(previousPageResponse).to.have.status(200);
360349
expect(previousPageResponse.body).to.be.a("object");
@@ -592,6 +581,48 @@ describe("Users", function () {
592581
});
593582
});
594583

584+
describe("GET /users/?id", function () {
585+
afterEach(async function () {
586+
await cleanDb();
587+
});
588+
589+
it("Should return given user by id", async function () {
590+
const { userId } = await addOrUpdate(userData[0]);
591+
const res = await chai.request(app).get(`/users/?id=${userId}`);
592+
expect(res).to.have.status(200);
593+
expect(res.body).to.be.a("object");
594+
expect(res.body.message).to.equal("User returned successfully!");
595+
expect(res.body.user).to.be.a("object");
596+
expect(Object.keys(res.body.user)).to.include.members([
597+
"username",
598+
"first_name",
599+
"last_name",
600+
"yoe",
601+
"linkedin_id",
602+
"github_id",
603+
"isMember",
604+
"roles",
605+
]);
606+
expect(Object.keys(res.body.user)).to.not.include.members(["phone", "email"]);
607+
expect(res.body.user.id).to.equal(userId);
608+
});
609+
610+
it("Should return 404 if user not Found", function (done) {
611+
chai
612+
.request(app)
613+
.get(`/users/?id=anyRandomuserId`)
614+
.end((err, res) => {
615+
if (err) {
616+
return done(err);
617+
}
618+
expect(res).to.have.status(404);
619+
expect(res.body).to.be.a("object");
620+
expect(res.body.message).to.equal("User doesn't exist");
621+
return done();
622+
});
623+
});
624+
});
625+
595626
describe("GET /users?search", function () {
596627
beforeEach(async function () {
597628
await addOrUpdate(userData[0]);

0 commit comments

Comments
 (0)