Skip to content

Commit

Permalink
Fix Username Migration script to run on users with no first/last name (
Browse files Browse the repository at this point in the history
  • Loading branch information
vinit717 authored Sep 8, 2024
1 parent 82a78d9 commit 0edca3d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
16 changes: 11 additions & 5 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,10 @@ const updateUsersWithNewUsernames = async () => {
const snapshot = await userModel.get();

const nonMemberUsers = snapshot.docs.filter((doc) => {
const roles = doc.data().roles;
return !(roles?.member === true || roles?.super_user === true);
const userData = doc.data();
const roles = userData.roles;

return !(roles?.member === true || roles?.super_user === true || userData.incompleteUserDetails === true);
});

const summary = {
Expand All @@ -985,10 +987,14 @@ const updateUsersWithNewUsernames = async () => {
const userData = userDoc.data();
const id = userDoc.id;

const firstName = userData.first_name.split(" ")[0].toLowerCase();
const lastName = userData.last_name.toLowerCase();
const fullName = `${firstName}-${lastName}`;
const firstName = userData.first_name?.split(" ")[0]?.toLowerCase();
const lastName = userData.last_name?.toLowerCase();

if (!firstName || !lastName) {
return;
}

const fullName = `${firstName}-${lastName}`;
if (!nameToUsersMap.has(fullName)) {
nameToUsersMap.set(fullName, []);
}
Expand Down
2 changes: 1 addition & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ 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);
module.exports = router;
router.post("/migration/update-usernames", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);
router.post("/batch-username-update", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.
module.exports = router;
26 changes: 3 additions & 23 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2428,7 +2428,7 @@ describe("Users", function () {
it("should run the migration and update usernames successfully", async function () {
const res = await chai
.request(app)
.post("/users/migration/update-usernames")
.post("/users/batch-username-update")
.set("cookie", `${cookieName}=${superUserAuthToken}`)
.send();

Expand All @@ -2438,7 +2438,7 @@ describe("Users", function () {
it("should not update usernames for super_user or member", async function () {
const res = await chai
.request(app)
.post("/users/migration/update-usernames")
.post("/users/batch-username-update")
.set("cookie", `${cookieName}=${superUserAuthToken}`)
.send();

Expand All @@ -2450,31 +2450,11 @@ describe("Users", function () {
it("should return 401 for unauthorized user attempting migration", async function () {
const res = await chai
.request(app)
.post("/users/migration/update-usernames")
.post("/users/batch-username-update")
.set("cookie", `${cookieName}=${jwt}`)
.send();

expect(res).to.have.status(401);
});

it("should update username for users without member and super user true", async function () {
const userWithoutRoles = userData[2];
const userId = await addUser(userWithoutRoles);

const res = await chai
.request(app)
.post("/users/migration/update-usernames")
.set("cookie", `${cookieName}=${superUserAuthToken}`)
.send();

expect(res).to.have.status(200);
expect(res.body.totalUpdatedUsernames).to.be.greaterThan(0);

const updatedUser = await firestore.collection("users").doc(userId).get();

expect(updatedUser.data().username).to.include(
`${userWithoutRoles.first_name.toLowerCase()}-${userWithoutRoles.last_name.toLowerCase()}-1`
);
});
});
});

0 comments on commit 0edca3d

Please sign in to comment.