Skip to content

Commit 3d11b0b

Browse files
committed
added users data in response, fixed test cases
1 parent 421c736 commit 3d11b0b

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

controllers/users.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,11 @@ const filterUsers = async (req, res) => {
466466

467467
// one time script function to perform the migration - adding github_user_id field to the document
468468
const migrate = async (req, res) => {
469-
const authToken = `${config.get("githubOauth.clientId")}:${config.get("githubOauth.clientSecret")}`;
469+
const authToken = `${config.get("githubOauth.patToken")}`;
470470
// converting the `authToken` string into Base64 format
471-
const encodedToken = Buffer.from(`${authToken}`, "binary").toString("base64");
471+
const encodedToken = Buffer.from(`${authToken}`).toString("base64");
472+
const usernameNotFound = [];
473+
let countUserNotFound = 0;
472474
try {
473475
// Fetch user data from GitHub API for each document in the users collection
474476
// divided by 500 because firestore api guarantee that we can process in batch of 500.
@@ -481,6 +483,7 @@ const migrate = async (req, res) => {
481483
const batchWrites = [];
482484
for (const userDoc of batchDocs) {
483485
const githubUsername = userDoc.data().github_id;
486+
const userName = userDoc.data().github_display_name;
484487
batchWrite.update(userDoc.ref, { github_user_id: null });
485488
batchWrites.push(
486489
axios
@@ -496,7 +499,12 @@ const migrate = async (req, res) => {
496499
})
497500
.catch((error) => {
498501
if (error.response && error.response.status === 404) {
499-
logger.error(`Error: github_user_id not found for ${githubUsername}`);
502+
countUserNotFound++;
503+
const userNotFound = {
504+
name: `${userName}`,
505+
username: `${githubUsername}`,
506+
};
507+
usernameNotFound.push(userNotFound);
500508
} else {
501509
// Other error occurred
502510
logger.error("An error occurred at axios.get:", error);
@@ -510,11 +518,14 @@ const migrate = async (req, res) => {
510518

511519
return res.status(200).json({
512520
message: "All Users github_user_id added successfully",
521+
data: {
522+
invalidUsers: usernameNotFound,
523+
totalCount: countUserNotFound,
524+
},
513525
});
514526
} catch (error) {
515-
return res.status(500).json({
516-
message: "Internal Server Error",
517-
});
527+
logger.error(`Error while Updating all users: ${error}`);
528+
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
518529
}
519530
};
520531

test/integration/users.test.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const chaiHttp = require("chai-http");
44
const firestore = require("../../utils/firestore");
55
const app = require("../../server");
66
const authService = require("../../services/authService");
7+
const sinon = require("sinon");
78
const addUser = require("../utils/addUser");
89
const profileDiffs = require("../../models/profileDiffs");
910
const cleanDb = require("../utils/cleanDb");
@@ -1053,19 +1054,30 @@ describe("Users", function () {
10531054
});
10541055
});
10551056
it("Should update the user", async function () {
1056-
const response1 = await chai
1057+
const usersMigrateResponse = await chai
10571058
.request(app)
10581059
.post(`/users/migrate`)
10591060
.set("Cookie", `${cookieName}=${superUserAuthToken}`);
1060-
expect(response1).to.have.status(200);
1061-
expect(response1.body).to.eql({
1061+
expect(usersMigrateResponse).to.have.status(200);
1062+
expect(usersMigrateResponse.body).to.eql({
10621063
message: `All Users github_user_id added successfully`,
1064+
data: {
1065+
invalidUsers: [],
1066+
totalCount: 0,
1067+
},
10631068
});
1064-
const response2 = await chai.request(app).get(`/users`).set("cookie", `${cookieName}=${superUserAuthToken}`);
1065-
expect(response2).to.have.status(200);
1066-
response2.body.users.forEach((document) => {
1069+
const stub = sinon.stub(chai.request(app), "get").resolves({
1070+
status: 200,
1071+
body: {
1072+
users: [{ github_user_id: "12345678" }, { github_user_id: "78945612" }],
1073+
},
1074+
});
1075+
const usersReponse = await chai.request(app).get(`/users`).set("cookie", `${cookieName}=${superUserAuthToken}`);
1076+
expect(usersReponse).to.have.status(200);
1077+
usersReponse.body.users.forEach((document) => {
10671078
expect(document).to.have.property(`github_user_id`);
10681079
});
1080+
stub.restore();
10691081
});
10701082
it("Should return unauthorized error when not logged in", function (done) {
10711083
chai

test/unit/models/users.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ describe("users", function () {
116116
expect(data.github_id).to.equal(userData.github_id);
117117
});
118118

119-
it("github_user_id field should have a maximum length of 8 characters", async function () {
120-
const userData = { ...userDataArray[0], github_user_id: "a".repeat(9) };
121-
122-
try {
123-
await users.addOrUpdate(userData);
124-
} catch (error) {
125-
expect(error.message).to.equal("Validation error: github_id exceeds maximum length of 8 characters");
126-
}
127-
});
128-
129119
it("should be stored correctly in the database", async function () {
130120
const userData = { ...userDataArray[0], github_id: "my_github_id" };
131121

0 commit comments

Comments
 (0)