Skip to content

Commit 127bdec

Browse files
committed
Finish route and put in tests
1 parent c756101 commit 127bdec

6 files changed

Lines changed: 102 additions & 7 deletions

File tree

constants/role.constant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const accountRole = {
1111
"routes": [
1212
Constants.Routes.authRoutes.login,
1313
Constants.Routes.authRoutes.logout,
14+
Constants.Routes.authRoutes.changePassword,
1415
Constants.Routes.authRoutes.getSelfRoleBindindings,
1516
Constants.Routes.accountRoutes.getSelf,
1617
Constants.Routes.accountRoutes.getSelfById,

constants/routes.constant.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const authRoutes = {
2121
"getAnyRoleBindings": {
2222
requestType: Constants.REQUEST_TYPES.GET,
2323
uri: "/api/auth/rolebindings/" + Constants.ROLE_CATEGORIES.ALL
24-
}
24+
},
25+
"changePassword": {
26+
requestType: Constants.REQUEST_TYPES.PATCH,
27+
uri: "/api/auth/password/change"
28+
},
2529
};
2630

2731
const accountRoutes = {

middlewares/auth.middleware.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ async function retrieveRoleBindings(req, res, next) {
126126
}
127127

128128
/**
129-
*
130-
* @param {*} req
129+
* Checks that the oldPassword is the current password for the logged in user. If the password is correct,
130+
* then updates the password to the string in newPassword.
131+
* @param {{user: {email: string}, body: {oldPassword: string, newPassword: string}} req
131132
* @param {*} res
132133
* @param {*} next
133134
*/
134135
async function changePassword(req, res, next) {
135-
acc = await Services.Account.getAccountIfValid(req.user.email, req.body.oldPassword);
136+
const acc = await Services.Account.getAccountIfValid(req.user.email, req.body.oldPassword);
136137
// user's old password is correct
137138
if (!!acc) {
138139
req.body.account = await Services.Account.updatePassword(req.user.id, req.body.newPassword);

routes/api/auth.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,31 @@ module.exports = {
9797
Controllers.Auth.sentResetEmail
9898
);
9999

100-
authRouter.coute("/password/change").post(
100+
/**
101+
* @api {post} /auth/password/change change password for logged in user
102+
* @apiName changePassword
103+
* @apiGroup Authentication
104+
* @apiVersion 0.0.8
105+
*
106+
* @apiParam {String} oldPassword The current password of the user
107+
* @apiParam {String} newPassword The new password of the user
108+
*
109+
* @apiParamExample {json} Request-Example:
110+
* {
111+
* "oldPassword": "password12345",
112+
* "newPassword": "password123456"
113+
* }
114+
*
115+
* @apiSuccess {string} message Success message
116+
* @apiSuccess {object} data empty
117+
* @apiSuccessExample {json} Success-Response:
118+
* {"message": "Successfully reset password", "data": {}}
119+
*
120+
* @apiPermission: Must be logged in
121+
*/
122+
authRouter.route("/password/change").patch(
123+
Middleware.Auth.ensureAuthenticated(),
124+
Middleware.Auth.ensureAuthorized(),
101125
Middleware.Validator.Auth.ChangePasswordValidator,
102126
Middleware.parseBody.middleware,
103127

tests/account.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,71 @@ describe("POST reset password", function () {
359359
});
360360
});
361361

362+
describe("PATCH change password for logged in user", function () {
363+
const successChangePassword = {
364+
"oldPassword": Admin1.password,
365+
"newPassword": "password12345"
366+
};
367+
const failChangePassword = {
368+
"oldPassword": "WrongPassword",
369+
"newPassword": "password12345"
370+
};
371+
// fail on authentication
372+
it("should fail to change the user's password because they are not logged in", function (done) {
373+
chai.request(server.app)
374+
.patch("/api/auth/password/change")
375+
.type("application/json")
376+
.send(failChangePassword)
377+
.end(function (err, res) {
378+
res.should.have.status(401);
379+
res.should.be.json;
380+
res.body.should.have.property("message");
381+
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
382+
done();
383+
});
384+
});
385+
// success case
386+
it("should change the logged in user's password to a new password", function (done) {
387+
util.auth.login(agent, Admin1, (error) => {
388+
if (error) {
389+
agent.close();
390+
return done(error);
391+
}
392+
agent
393+
.patch("/api/auth/password/change")
394+
.type("application/json")
395+
.send(successChangePassword)
396+
.end(function (err, res) {
397+
res.should.have.status(200);
398+
res.should.be.json;
399+
res.body.should.have.property("message");
400+
res.body.message.should.equal("Successfully reset password");
401+
done();
402+
});
403+
});
404+
});
405+
// fail case because old password in incorrect
406+
it("should fail to change the logged in user's password to a new password because old password is incorrect", function (done) {
407+
util.auth.login(agent, Admin1, (error) => {
408+
if (error) {
409+
agent.close();
410+
return done(error);
411+
}
412+
agent
413+
.patch("/api/auth/password/change")
414+
.type("application/json")
415+
.send(failChangePassword)
416+
.end(function (err, res) {
417+
res.should.have.status(401);
418+
res.should.be.json;
419+
res.body.should.have.property("message");
420+
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
421+
done();
422+
});
423+
});
424+
});
425+
});
426+
362427
describe("GET retrieve permissions", function () {
363428
it("should SUCCEED and retrieve the rolebindings for the user", function (done) {
364429
util.auth.login(agent, storedAccount1, (error) => {

tests/auth.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ describe("GET roles", function () {
5656
done();
5757
});
5858
});
59-
})
60-
});
59+
});
60+
});

0 commit comments

Comments
 (0)