Skip to content
Snippets Groups Projects
Commit 6ec88638 authored by Gianfranco Harres's avatar Gianfranco Harres
Browse files

Merge branch 'issue/63-Change-password-route' into 'develop'

Issue #63: Add route to change password

Closes #63

See merge request !59
parents c4b19b21 6df71a73
Branches
Tags
2 merge requests!65Stable version 1.2,!59Issue #63: Add route to change password
Pipeline #22709 passed
......@@ -4,12 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 1.1.9 - 03-02-2020
### Added
- Route to change an user's password #63 (Richard Heise)
## Changed
- Delete route now has the token validation
## 1.1.8 - 30-01-2020
### Added
- Route to assign users to forms #60 (Richard Heise)
## Changed
- Route to write a form now has an extra stage in the waterfall
- THis stage assigns the user to a form by ID
- This stage assigns the user to a form by ID
## 1.1.7 - 29-01-2020
### Added
......
{
"name": "form-creator-api",
"version": "1.1.8",
"version": "1.1.9",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
......@@ -166,6 +166,24 @@ describe ("API data controller", () => {
.end(done);
});
it("Should respond 200 when changing an user's password", (done) => {
request(server)
.put("/user/changePassword")
.set("Authorization", "bearer " + testToken)
.send({
hash: "changed_pw_hashing"
})
.expect(200)
.expect((res: any) => {
expect(res.body).to.be.an("object");
expect(res.body.message).to.be.an("string");
expect(res.body.message).to.be.eql("Password changed with sucess.");
})
.end(done);
});
it("Should respond 200 when deleting an user from the database", (done) => {
request(server)
......@@ -197,7 +215,6 @@ describe ("API data controller", () => {
.end(done);
});
it("Should respond 500 when failing to delete an user by incompatible ID", (done) => {
request(server)
......@@ -212,4 +229,23 @@ describe ("API data controller", () => {
})
.end(done);
});
it("Should respond 500 when failing to change a password", (done) => {
request(server)
.put("/user/changePassword")
.set("Authorization", "bearer "+ testToken)
.send({
hash: "changed_pw_hashing"
})
.expect(500)
.expect((res: any) => {
expect(res.body).to.be.an("object");
expect(res.body.message).to.be.an("string");
expect(res.body.message).to.be.eql("Some error has ocurred. Check error property for details.")
expect(res.body.error).to.be.eql("Bad amount of ids returned: found '0' should be 1");
})
.end(done);
});
});
......@@ -172,4 +172,63 @@ export class UserCtrl {
});
});
}
public static changePassword (req: Request, res: Response, next: NextFunction) {
let newUser: User;
waterfall ([
(callback: (err: Error, password?: string) => void) => {
bcrypt.hash(req.body.hash, 10, (err: Error, hashedPw: string) => {
if (err) {
callback(err);
return;
}
callback(null, hashedPw);
});
},
(password: string, callback: (err: Error, user?: User) => void) => {
req.db.user.read(Object(req.userData).id, (err: Error, user?: User) => {
if (err) {
callback(err);
return;
}
try {
newUser = new User(OptHandler.User(user, password));
} catch (err) {
callback(err);
return;
}
callback(null, newUser);
});
},
(user: User, callback: (err: Error) => void) => {
req.db.user.update(user, (err: Error) => {
if (err) {
callback(err);
return;
} else {
res.json({
message: "Password changed with sucess."
});
callback(null);
return;
}
});
}
], (error: Error) => {
if (error) {
res.status(500).json({
message: "Some error has ocurred. Check error property for details.",
error: error.message
});
return;
}
return;
});
}
}
\ No newline at end of file
......@@ -58,6 +58,7 @@ app.post("/answer/:id", AnswerCtrl.write);
app.post("/user/signUp", UserCtrl.signUp);
app.post("/user/signIn", UserCtrl.signIn);
app.delete("/user/deleteData/:id", tokenValidation(), UserCtrl.deleteData);
app.put("/user/changePassword", tokenValidation(), UserCtrl.changePassword);
// Listening
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment