Skip to content

Commit

Permalink
Added New PUT Route for User Intro (#2293)
Browse files Browse the repository at this point in the history
* added one put api for user intro

* route checking fix

* deprecate message comment

* fixed test cases

* added one userauthorization middleware and test cases fixes

* added userAuthorization middleware

* Revert "added one userauthorization middleware and test cases fixes"

This reverts commit 7dcd885.

* Revert "Revert "added one userauthorization middleware and test cases fixes""

This reverts commit 238cd17.

* fixed test case msg

---------

Co-authored-by: Achintya Chatterjee <[email protected]>
Co-authored-by: Rishi <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2024
1 parent f1ea79e commit 0df8007
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
12 changes: 11 additions & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { authorizeAndAuthenticate } = require("../middlewares/authorizeUsersAndSe
const ROLES = require("../constants/roles");
const { Services } = require("../constants/bot");
const authenticateProfile = require("../middlewares/authenticateProfile");
const { devFlagMiddleware } = require("../middlewares/devFlag");
const { userAuthorization } = require("../middlewares/userAuthorization");

router.post("/", authorizeAndAuthenticate([ROLES.SUPERUSER], [Services.CRON_JOB_HANDLER]), users.markUnverified);
router.post("/update-in-discord", authenticate, authorizeRoles([SUPERUSER]), users.setInDiscordScript);
Expand All @@ -35,7 +37,15 @@ router.patch(
);
router.get("/:username", users.getUser);
router.get("/:userId/intro", authenticate, authorizeRoles([SUPERUSER]), users.getUserIntro);
router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro);
router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro); // This route is being deprecated soon, please use alternate available route `/users/:userId/intro`.
router.put(
"/:userId/intro",
devFlagMiddleware,
authenticate,
userValidator.validateJoinData,
userAuthorization,
users.addUserIntro
);
router.get("/:id/skills", users.getUserSkills);
router.get("/:id/badges", getUserBadges);
router.patch(
Expand Down
102 changes: 102 additions & 0 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,108 @@ describe("Users", function () {
});
});

describe("PUT /users/:userId/intro", function () {
let userStatusData;

beforeEach(async function () {
await userStatusModel.updateUserStatus(userId, userStatusDataAfterSignup);
const updateStatus = await userStatusModel.updateUserStatus(userId, userStatusDataAfterFillingJoinSection);
userStatusData = (await firestore.collection("usersStatus").doc(updateStatus.id).get()).data();
});

it("should return 409 if the data already present", function (done) {
addJoinData(joinData(userId)[3]);
chai
.request(app)
.put(`/users/${userId}/intro?dev=true`)
.set("Cookie", `${cookieName}=${jwt}`)
.send(joinData(userId)[3])
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(409);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("User data is already present!");
return done();
});
});

it("Should store the info in db", function (done) {
chai
.request(app)
.put(`/users/${userId}/intro?dev=true`)
.set("Cookie", `${cookieName}=${jwt}`)
.send(joinData()[2])
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(201);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("User join data and newstatus data added and updated successfully");
expect(userStatusData).to.have.own.property("currentStatus");
expect(userStatusData).to.have.own.property("monthlyHours");
expect(userStatusData.currentStatus.state).to.equal("ONBOARDING");
expect(userStatusData.monthlyHours.committed).to.equal(40);
return done();
});
});

it("Should return 401 for Unauthenticated User Request", function (done) {
chai
.request(app)
.put(`/users/${userId}/intro?dev=true`)
.set("Cookie", `${cookieName}=""`)
.send(joinData()[2])
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(401);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("Unauthenticated User");
return done();
});
});

it("Should return 400 for invalid Data", function (done) {
chai
.request(app)
.put(`/users/${userId}/intro?dev=true`)
.set("Cookie", `${cookieName}=${jwt}`)
.send(joinData()[1])
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(400);
expect(res.body).to.be.a("object");
expect(res.body.message).to.be.equal('"firstName" is required');
return done();
});
});

it("Should return 403 for Forbidden access", function (done) {
const userId = "anotherUser123";
addJoinData(joinData(userId)[3]);

chai
.request(app)
.put(`/users/${userId}/intro?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send(joinData(userId)[3])
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(403);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Unauthorized access");
return done();
});
});
});

describe("PATCH /users/rejectDiff", function () {
let profileDiffsId;

Expand Down

0 comments on commit 0df8007

Please sign in to comment.