Skip to content
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
5941bc5
Merge branch 'develop' into main-resolve-conflict
shubham-y Feb 26, 2022
5aad16d
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Mar 26, 2022
7358662
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Apr 20, 2022
0c2e8ce
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y May 18, 2022
0ee25fd
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Jul 13, 2022
d54f16a
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Aug 15, 2022
4e5aff3
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Sep 15, 2022
9afcbd0
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Oct 26, 2022
0fbfc9a
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Oct 29, 2022
cc487d9
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Nov 23, 2022
a5ca925
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Nov 30, 2022
f01f146
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Apr 5, 2023
558dd2d
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Apr 15, 2023
adaf3cb
fixingdoc_duplication
iamYashSinha Apr 24, 2023
8d8232c
fixed linting error
iamYashSinha Apr 24, 2023
e88520b
tests added for github_user_id
iamYashSinha Apr 26, 2023
2e0838b
tests added for github_user_id
iamYashSinha Apr 25, 2023
65f4545
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Apr 28, 2023
fcac418
added migration route
iamYashSinha Apr 29, 2023
a45e5c8
updated the add or update function
iamYashSinha Apr 29, 2023
63cf7d0
Delete firestore-private-key.json
iamYashSinha Apr 29, 2023
45b2f3b
changed to post method
iamYashSinha Apr 29, 2023
9ee96fc
resolved errors in 15 testcases
iamYashSinha May 1, 2023
0fb5eca
fixed contribution tests error
iamYashSinha May 1, 2023
0d60558
protection on migration route
iamYashSinha May 1, 2023
7c6d7aa
required changes done
iamYashSinha May 2, 2023
e1e57a9
typecast github_user_id field
iamYashSinha May 2, 2023
a481fb6
added batch api calls and fixed test cases
iamYashSinha May 4, 2023
66b6e07
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y May 4, 2023
b25554d
added dynamic batchCount for migration
iamYashSinha May 5, 2023
75d682f
fixed tests and batchWrites call
iamYashSinha May 6, 2023
aa0f615
deleted migration fixtures
iamYashSinha May 7, 2023
fa249fc
Delete migration.js
iamYashSinha May 7, 2023
241df6a
fixed tests and added migration fixture
iamYashSinha May 7, 2023
88aaabd
Merge branch 'bugfix/issue957' of https://github.com/iamYashSinha/web…
iamYashSinha May 7, 2023
7353c58
fixed tests and added migration fixtures
iamYashSinha May 7, 2023
e56cd15
removed github_user_id field in migration fixture
iamYashSinha May 7, 2023
2bdff72
Merge branch 'Real-Dev-Squad:develop' into bugfix/issue957
iamYashSinha May 8, 2023
d59ae9e
removed unwanted spaces
iamYashSinha May 8, 2023
d1b213c
fixed desc of model test
iamYashSinha May 8, 2023
eccbbd5
replaced res.send and removed validation on github_id
iamYashSinha May 14, 2023
9c0fa42
Merge branch 'develop' into bugfix/issue957
iamYashSinha May 15, 2023
f61ff70
changed the variable name in usersModel
iamYashSinha May 15, 2023
7bcdb26
updated tests and replaced res.send
iamYashSinha May 17, 2023
348ce2b
fixed failing test cases and status code
iamYashSinha May 17, 2023
5247952
removed beforeEach and afterEach block in migration test
iamYashSinha May 17, 2023
468b057
made the GitHub API call authenticated
iamYashSinha May 17, 2023
1f04c76
fixed the variable name and spacing
iamYashSinha May 17, 2023
8e9414e
removed unwanted tests
iamYashSinha May 17, 2023
421c736
added a method to find where github_id is not valid
iamYashSinha May 17, 2023
3d11b0b
added users data in response, fixed test cases
iamYashSinha May 19, 2023
20f0b3e
updated variable name
iamYashSinha May 20, 2023
34f235c
fixed sinon test
iamYashSinha May 20, 2023
ae3bfd8
fixed integration test
iamYashSinha May 20, 2023
9569688
fixed sinon.stub and resolves method
iamYashSinha May 20, 2023
5484294
fixed sinon.stub timeout
iamYashSinha May 20, 2023
d20932d
fixed test cases
iamYashSinha Jun 11, 2023
6d309e3
auth token yet to be generated
iamYashSinha Jun 11, 2023
240ccb9
Merge branch 'develop' into bugfix/issue957
iamYashSinha Jun 11, 2023
d0ed63c
add missing closing bracket
shubham-y Jun 26, 2023
c3bd1db
fix conflict in users integration test file
shubham-y Jul 1, 2023
2dc68a5
Merge branch 'develop' of https://github.com/shubham-y/website-backen…
shubham-y Jul 1, 2023
e9a0819
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Jul 1, 2023
ed2dfc3
add github_user_id in user fixtures
shubham-y Jul 1, 2023
7b57924
update response data for migrate function
shubham-y Jul 3, 2023
97f0959
update tests for migrate route
shubham-y Jul 3, 2023
0dfda95
update tests for migrate route
shubham-y Jul 5, 2023
b2c933e
update response data for migrate function
shubham-y Jul 5, 2023
8158773
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-b…
shubham-y Jul 26, 2023
31ad08a
update controller function
shubham-y Jul 26, 2023
708d57e
refactor tests
shubham-y Jul 26, 2023
7f79193
update integration test
shubham-y Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const githubAuth = (req, res, next) => {
tokens: {
githubAccessToken: accessToken,
},
github_user_id: user.id,
};

const { userId, incompleteUserDetails } = await users.addOrUpdate(userData);
Expand Down
43 changes: 43 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const axios = require("axios");
const firestore = require("../utils/firestore");
const chaincodeQuery = require("../models/chaincodes");
const userQuery = require("../models/users");
const profileDiffsQuery = require("../models/profileDiffs");
Expand Down Expand Up @@ -462,6 +464,46 @@ const filterUsers = async (req, res) => {
}
};

// one time script function to perform the migration - adding github_user_id field to the document
const migrate = async (req, res) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use a better name for this please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the name to addGtihubId

try {
// Fetch user data from GitHub API for each document in the users collection
// divided by 500 because firestore api guarantee that we can process in batch of 500.
const usersSnapshot = await firestore.collection("users").get();
const batchCount = Math.ceil(usersSnapshot.docs.length / 500);
// Create batch write operations for each batch of documents
for (let i = 0; i < batchCount; i++) {
const batchDocs = usersSnapshot.docs.slice(i * 500, (i + 1) * 500);
const batchWrite = firestore.batch();
const batchWrites = [];
for (const userDoc of batchDocs) {
const githubUsername = userDoc.data().github_id;
batchWrite.update(userDoc.ref, { github_user_id: null });
batchWrites.push(
axios
.get(`https://api.github.com/users/${githubUsername}`)
.then((response) => {
const githubUserId = response.data.id;
batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}` });
})
.catch((error) => {
logger.error(error);
})
);
}
await Promise.all(batchWrites);
await batchWrite.commit();
}

return res.send({
statusCode: 201,
message: "All Users github_user_id added successfully",
});
} catch (error) {
return res.status(500).send("Internal server error");
}
};

module.exports = {
verifyUser,
generateChaincode,
Expand All @@ -481,4 +523,5 @@ module.exports = {
addDefaultArchivedRole,
getUserSkills,
filterUsers,
migrate,
};
4 changes: 1 addition & 3 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const addOrUpdate = async (userData, userId = null) => {
}

// userId is null, Add or Update user
const user = await userModel.where("github_id", "==", userData.github_id).limit(1).get();
const user = await userModel.where("github_user_id", "==", userData.github_user_id).limit(1).get();
if (!user.empty) {
await userModel.doc(user.docs[0].id).set(userData, { merge: true });

Expand All @@ -67,7 +67,6 @@ const addOrUpdate = async (userData, userId = null) => {
throw err;
}
};

const addJoinData = async (userData) => {
try {
await joinModel.add(userData);
Expand All @@ -80,7 +79,6 @@ const addJoinData = async (userData) => {
throw err;
}
};

const getJoinData = async (userId) => {
try {
const userData = [];
Expand Down
3 changes: 3 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);

// WARNING!! - One time Script/Route to do migration.
router.post("/migrate", authenticate, authorizeRoles([SUPERUSER]), users.migrate);

module.exports = router;
38 changes: 38 additions & 0 deletions test/fixtures/user/migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const githubUserInfo = require("../auth/githubUserInfo")();

/**
* User info for GitHub auth response
* Multiple responses can be added to the array if required
*
* @return {Object}
*/
module.exports = () => {
return [
{
username: "ankur",
first_name: "Ankur",
last_name: "Narkhede",
yoe: 0,
img: "./img.png",
linkedin_id: "ankurnarkhede",
github_id: githubUserInfo[0].username,
github_display_name: githubUserInfo[0].displayName,
isMember: true,
phone: "1234567890",
email: "[email protected]",
roles: {
member: true,
},
tokens: {
githubAccessToken: "githubAccessToken",
},
status: "active",
profileURL: "https://abcde.com",
picture: {
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
},
incompleteUserDetails: false,
},
];
};
9 changes: 9 additions & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = () => {
yoe: 0,
img: "./img.png",
linkedin_id: "ankurnarkhede",
github_user_id: githubUserInfo[0].id,
github_id: githubUserInfo[0].username,
github_display_name: githubUserInfo[0].displayName,
isMember: true,
Expand All @@ -41,6 +42,7 @@ module.exports = () => {
last_name: "Bhandarkar",
yoe: 0,
img: "./img.png",
github_user_id: "23654780",
github_id: "whydonti",
linkedin_id: "nikhil-bhandarkar",
twitter_id: "whatifi",
Expand All @@ -57,6 +59,7 @@ module.exports = () => {
last_name: "Gajjewar",
yoe: 0,
img: "./img.png",
github_user_id: "23654788",
github_id: "cartmanishere",
linkedin_id: "pranav-gajjewar",
twitter_id: "PGajjewar",
Expand All @@ -77,6 +80,7 @@ module.exports = () => {
yoe: 3,
img: "./img.png",
linkedin_id: "sagarbajpai",
github_user_id: "23654790",
github_id: "sagarbajpai",
github_display_name: "Sagar Bajpai",
phone: "1234567890",
Expand All @@ -101,6 +105,7 @@ module.exports = () => {
yoe: 10,
img: "./img.png",
linkedin_id: "ankushdharkar",
github_user_id: "23654700",
github_id: "ankushdharkar",
github_display_name: "Ankush Dharkar",
phone: "1234567890",
Expand All @@ -125,6 +130,7 @@ module.exports = () => {
yoe: 0,
img: "./img.png",
linkedin_id: "ankitabannore",
github_user_id: "23654725",
github_id: "Ankita2002-Fr",
github_display_name: "Ankita Bannore",
isMember: true,
Expand All @@ -149,6 +155,7 @@ module.exports = () => {
last_name: "Chaudhari",
yoe: 0,
img: "./img.png",
github_user_id: "23654583",
github_id: "mehulkchaudhari",
linkedin_id: "mehulkchaudhari",
twitter_id: "mehulkchaudhari",
Expand All @@ -174,6 +181,7 @@ module.exports = () => {
yoe: 0,
img: "./img.png",
linkedin_id: "ankurnarkhede",
github_user_id: "23654236",
github_id: "ankur1234",
github_display_name: "ankur-xyz",
phone: "1234567890",
Expand All @@ -182,6 +190,7 @@ module.exports = () => {
{
username: "ritvik",
github_id: "RitvikJamwal75",
github_user_id: "23654123",
first_name: "Ritvik",
yoe: 1,
picture: {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/contributions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ describe("Contributions", function () {
yoe: 0,
img: "./img.png",
github_id: "prakashchoudhary07",
github_user_id: "12345678",
username: "prakash",
};
// Adding user
await userModel.addOrUpdate(user);
// Creating second user
user.username = "userWithNoPrs";
user.github_id = "userWithNoPrs";
user.github_user_id = "userWithNoPrs";
await userModel.addOrUpdate(user);
// Creating second user
user.username = "userNoTask";
user.github_id = "userNoTask";
user.github_user_id = "userNoTask";
await userModel.addOrUpdate(user);
// Creating task for user
const task = {
Expand Down
1 change: 1 addition & 0 deletions test/integration/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe("Tasks", function () {
const { userId: assignedUser } = await userModel.addOrUpdate({
github_id: "prakashchoudhary07",
username: "user1",
github_user_id: "12345678",
});
const assignedTask = [
{
Expand Down
60 changes: 57 additions & 3 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const chai = require("chai");
const { expect } = chai;
const chaiHttp = require("chai-http");

const firestore = require("../../utils/firestore");
const app = require("../../server");
const authService = require("../../services/authService");
Expand All @@ -13,7 +12,6 @@ const userData = require("../fixtures/user/user")();
const profileDiffData = require("../fixtures/profileDiffs/profileDiffs")();
const superUser = userData[4];
const searchParamValues = require("../fixtures/user/search")();

const config = require("config");
const joinData = require("../fixtures/user/join");
const {
Expand All @@ -25,7 +23,6 @@ const userStatusModel = require("../../models/userStatus");

const cookieName = config.get("userToken.cookieName");
chai.use(chaiHttp);

describe("Users", function () {
let jwt;
let superUserId;
Expand Down Expand Up @@ -1040,4 +1037,61 @@ describe("Users", function () {
});
});
});

describe("POST /users/migrate", function () {
beforeEach(async function () {
superUserId = await addUser(superUser);
superUserAuthToken = authService.generateAuthToken({ userId: superUserId });
});

afterEach(async function () {
await cleanDb();
});
it("Should return 401 when user is unauthorize", function (done) {
chai
.request(app)
.post("/users/migrate")
.end((err, res) => {
if (err) {
return done();
}
expect(res).to.have.status(401);
expect(res.body.message).to.equal("Unauthenticated User");
return done();
});
});
it("Should update the user", async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the title must be should add github_id to the user

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the title

const response1 = await chai
.request(app)
.post(`/users/migrate`)
.set("Cookie", `${cookieName}=${superUserAuthToken}`);
expect(response1).to.have.status(200);
expect(response1.body).to.eql({
statusCode: 201,
message: `All Users github_user_id added successfully`,
});
const response2 = await chai.request(app).get(`/users`).set("cookie", `${cookieName}=${superUserAuthToken}`);
expect(response2).to.have.status(200);
response2.body.users.forEach((document) => {
expect(document).to.have.property(`github_user_id`);
});
});
it("Should return unauthorized error when not logged in", function (done) {
chai
.request(app)
.post(`/users/migrate`)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(401);
expect(res.body).to.eql({
statusCode: 401,
error: "Unauthorized",
message: "Unauthenticated User",
});
return done();
});
});
});
});
57 changes: 57 additions & 0 deletions test/unit/models/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,62 @@ describe("users", function () {
expect(user.last_name).to.equal(userData.last_name);
expect(userExists).to.equal(true);
});

it("should add the github_user_id to the user collection", async function () {
const userData = userDataArray[0];
userData.github_user_id = "12345678";

const { isNewUser, userId } = await users.addOrUpdate(userData);

const data = (await userModel.doc(userId).get()).data();

expect(data.github_user_id).to.equal(userData.github_user_id);
expect(isNewUser).to.equal(true);
});

it("should update the github_id in the user collection", async function () {
const userData = userDataArray[0];
userData.github_user_id = "Yash Sinha";

// Add the user the first time
const { userId } = await users.addOrUpdate(userData);

// Update the user with same data and new github_user_id
userData.github_id = "Ankush Dharkar";
await users.addOrUpdate(userData, userId);

const data = (await userModel.doc(userId).get()).data();

expect(data.github_id).to.equal(userData.github_id);
});

it("verifies the error text message", async function () {
const userData = { ...userDataArray[0], github_id: 123 };

try {
await users.addOrUpdate(userData);
} catch (error) {
expect(error.message).to.equal("Validation error: github_id must be a string");
}
});

it("github_user_id field should have a maximum length of 8 characters", async function () {
const userData = { ...userDataArray[0], github_user_id: "a".repeat(9) };

try {
await users.addOrUpdate(userData);
} catch (error) {
expect(error.message).to.equal("Validation error: github_id exceeds maximum length of 8 characters");
}
});

it("should be stored correctly in the database", async function () {
const userData = { ...userDataArray[0], github_id: "my_github_id" };

const { userId } = await users.addOrUpdate(userData);
const data = (await userModel.doc(userId).get()).data();

expect(data.github_id).to.equal("my_github_id");
});
});
});