diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fbcc9883cba27a01a0394b5088e5b4aff545d53..c354f63139ca6e4b3c72bd00e7a1392a8869f293 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index c65f3c2699828491f5116943612da5ff2e0892b7..492508044d4aece1b6b977b568c07a5dc980b057 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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": { diff --git a/src/api/controllers/form.spec.ts b/src/api/controllers/form.spec.ts index de86a1a42afad955519a5ea341215d7199bff0e8..a413be6dbf3104ad7417a34b87c6bf673df72b11 100644 --- a/src/api/controllers/form.spec.ts +++ b/src/api/controllers/form.spec.ts @@ -87,7 +87,7 @@ describe("Initial test User", () => { expect(res.body.message).to.be.equal("Authentication successful."); testToken = res.body.token; }) - .end(done); + .end(done); }); }) diff --git a/src/api/controllers/user.spec.ts b/src/api/controllers/user.spec.ts index 593c169b627f30b9d0b25fb2091df7d74ffd463b..3787cd1a042cb26b7684099eeca18059049e8334 100644 --- a/src/api/controllers/user.spec.ts +++ b/src/api/controllers/user.spec.ts @@ -165,6 +165,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) => { @@ -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); + }); }); diff --git a/src/api/controllers/user.ts b/src/api/controllers/user.ts index 772c5d1589f44bd0f7c64442ac149b096fb217fc..8c3585cff8aa701e7bbb96ddca47fb02f8b16b03 100644 --- a/src/api/controllers/user.ts +++ b/src/api/controllers/user.ts @@ -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 diff --git a/src/main.ts b/src/main.ts index 569a1c364e1c829b09403f3e119bd110e08450c7..67b1f27cde6c0b1f9d59ba953462256d5d6963cc 100755 --- a/src/main.ts +++ b/src/main.ts @@ -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