Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate update user problems logic #46

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { userDBInteractions } from "../database/interactions/user";
import { IUserModel } from "../database/models/user";
import { bcryptPassword } from "../config/bcrypt";
import { statusCodes } from "../config/statusCodes";
import { codeforces } from "../util/codeforces";
import { auth } from "../util/auth";

const authController = {
Expand Down Expand Up @@ -35,8 +34,6 @@ const authController = {
message: "Invalid email or password"
});
} else {
if (user.platformData.codeforces.username)
await codeforces.updateUserProblems(user);
const token = jwt.sign(
{
id: user._id,
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,32 @@ const userController = {
}
},

updateUserProblems: async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(statusCodes.MISSING_PARAMS).json(
errors.formatWith(errorMessage).array()[0]
);
} else {
try {
const { userId } = req.params;
const user: IUserModel = await userDBInteractions.find(userId);
if (!user)
res.status(statusCodes.NOT_FOUND).json({
status: statusCodes.NOT_FOUND,
message: "User not found"
});
else {
if (user.platformData.codeforces.username)
await codeforces.updateUserProblems(user);
res.status(statusCodes.SUCCESS).json(user);
}
} catch (error) {
res.status(statusCodes.SERVER_ERROR).json(error);
}
}
},

delete: async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
Expand Down
6 changes: 6 additions & 0 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ userRouter.put(

userRouter.patch("/resetLastSubmissions", userController.resetLastSubmissions);

userRouter.patch(
"/:userId/updateUserProblems",
userValidator("PATCH /users/:userId/updateUserProblems"),
userController.updateUserProblems
);

userRouter.patch(
"/:userId/problems",
userValidator("PATCH /users/:userId/problems"),
Expand Down
3 changes: 3 additions & 0 deletions src/validators/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export function userValidator(method: string): ValidationChain[] {
).custom(validPassword)
];
}
case "PATCH /users/:userId/updateUserProblems": {
return [param("userId", "Invalid ':userId'").isMongoId()];
}
case "PATCH /users/:userId/problems": {
return [
param("userId", "Invalid ':userId'").isMongoId(),
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/controllers/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,6 @@ describe("Auth controller tests", () => {
stubs.jwt.sign.returns(testToken);
await authController.login(req, mockRes);
sinon.assert.calledOnce(stubs.userDB.findByEmail);
sinon.assert.calledOnce(
stubs.userUtil.codeforces.updateUserProblems
);
sinon.assert.calledOnce(stubs.userValidator.validationResult);
sinon.assert.calledWith(mockRes.status, statusCodes.SUCCESS);
const tmp = testUser.toJSON();
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/controllers/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,70 @@ describe("Users controller tests", () => {
});
});

describe("UpdateUserProblems", () => {
let req;
beforeEach(() => {
req = mockReq({
params: {
userId: testUser._id
}
});
});
it("status 200: updates user problems", async () => {
stubs.userValidator.validationResult.returns(
emptyValidationError()
);
stubs.userDB.find.returns(testUser);
stubs.userUtil.codeforces.updateUserProblems.returns();
await userController.updateUserProblems(req, mockRes);
sinon.assert.calledOnce(stubs.userDB.find);
sinon.assert.calledOnce(
stubs.userUtil.codeforces.updateUserProblems
);
sinon.assert.calledOnce(stubs.userValidator.validationResult);
sinon.assert.calledWith(mockRes.status, statusCodes.SUCCESS);
sinon.assert.calledWith(mockRes.json, testUser);
});

it("status 404: returns an appropriate response if user with given id doesn't exist", async () => {
stubs.userValidator.validationResult.returns(
emptyValidationError()
);
await userController.updateUserProblems(req, mockRes);
sinon.assert.calledOnce(stubs.userDB.find);
sinon.assert.calledWith(mockRes.status, statusCodes.NOT_FOUND);
sinon.assert.calledWith(mockRes.json, {
status: statusCodes.NOT_FOUND,
message: "User not found"
});
});

it("status 422: returns an appropriate response with validation error", async () => {
const errorMsg = {
status: statusCodes.MISSING_PARAMS,
message: "params[userId]: Invalid or missing ':userId'"
};
req.params.userId = "not ObjectId";
stubs.userValidator.validationResult.returns(
validationErrorWithMessage(errorMsg)
);
await userController.updateUserProblems(req, mockRes);
sinon.assert.calledOnce(stubs.userValidator.validationResult);
sinon.assert.calledWith(mockRes.status, statusCodes.MISSING_PARAMS);
sinon.assert.calledWith(mockRes.json, errorMsg);
});

it("status 500: fails to update user problems", async () => {
stubs.userValidator.validationResult.returns(
emptyValidationError()
);
stubs.userDB.find.returns(testUser);
stubs.userUtil.codeforces.updateUserProblems.throws();
await userController.updateUserProblems(req, mockRes);
sinon.assert.calledWith(mockRes.status, statusCodes.SERVER_ERROR);
});
});

describe("Show", () => {
let req;
beforeEach(() => {
Expand Down