diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c56c07408ae84457ad7eb58ff3ab29ab224aa76..eae785546e948a1cebd62aaac0aa3db182e5976f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ 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.12 - 04-02-2020 +### Added +- Route to update an user #65 (Richard Heise) +## Changed +- Opthandler can create user without hash +- UserQueryBuilder Update now needs an id +- UserOptions hash is not obrigatory + ## 1.1.11 - 04-02-2020 ## Changed - Form controller update route to verify if a user own the form #62 (Gianfranco) diff --git a/package.json b/package.json index 1d6b6095c0067c540d5607dc94f9b8704ea33675..49532a74d45e8c46857837ed07b7228198a54b46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "form-creator-api", - "version": "1.1.11", + "version": "1.1.12", "description": "RESTful API used to manage and answer forms.", "main": "index.js", "scripts": { diff --git a/src/api/controllers/user.spec.ts b/src/api/controllers/user.spec.ts index d8a88fe26d99c27635419efcede21b74514e52cb..b9d51024baf9f90bdca5d9514e940a3d663b0a8e 100644 --- a/src/api/controllers/user.spec.ts +++ b/src/api/controllers/user.spec.ts @@ -199,6 +199,60 @@ describe ("API data controller", () => { .end(done); }); + it("Should respond 200 when updating an user info", (done) => { + + request(server) + .put("/user/update") + .send({ + name: "test_name_update" + , email: "test_email@test.com" + }) + .set("Authorization", "bearer " + testToken) + .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.equal("Updated"); + }) + .end(done); + }); + + it("Should respond 500 when failing on updating an user info by email missing", (done) => { + + request(server) + .put("/user/update") + .send({ + name: "test_name_update" + }) + .set("Authorization", "bearer " + testToken) + .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.equal("Invalid User. Check error property for details."); + expect(res.body.error).to.be.equal("The dataType named 'email' was not found"); + }) + .end(done); + }); + + it("Should respond 500 when failing on updating an user info by email missing", (done) => { + + request(server) + .put("/user/update") + .send({ + email: "test_email@test.com" + }) + .set("Authorization", "bearer " + testToken) + .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.equal("Invalid User. Check error property for details."); + expect(res.body.error).to.be.equal("The dataType named 'name' was not found"); + }) + .end(done); + }); + it("Should respond 200 when deleting an user from the database", (done) => { request(server) diff --git a/src/api/controllers/user.ts b/src/api/controllers/user.ts index 0a33425b16f44cf9be2cd65f7f0482c7bbdaa30c..9004cfb24cde6acd3a6187a381ea3066798cf1d0 100644 --- a/src/api/controllers/user.ts +++ b/src/api/controllers/user.ts @@ -207,7 +207,7 @@ export class UserCtrl { }); }, (user: User, callback: (err: Error) => void) => { - req.db.user.update(user, (err: Error) => { + req.db.user.update(user, Object(req.userData).id, (err: Error) => { if (err) { callback(err); return; @@ -253,4 +253,33 @@ export class UserCtrl { return; }); } + + public static update (req: Request, res: Response, next: NextFunction) { + + let newUser: User; + try { + newUser = new User(OptHandler.User(req.body)); + } catch(e) { + res.status(500).json({ + message: "Invalid User. Check error property for details." + , error: e.message + }); + return; + } + waterfall([ + (callback: (err: Error, userResult?: User) => void) => { + req.db.user.update(newUser, Object(req.userData).id, callback); + } + ], (err) => { + if (err) { + res.status(500).json({ + message: "Could not update Form. Some error has ocurred. Check error property for details." + , error: err.message + }); + return; + } + res.json({ message: "Updated" }); + return; + }); + } } \ No newline at end of file diff --git a/src/core/user.ts b/src/core/user.ts index de97d8c184d15207b41e9786e3f4e79985ef82da..e5f39b4f1175c3e519b457dab8b0fc08f166cdce 100644 --- a/src/core/user.ts +++ b/src/core/user.ts @@ -28,7 +28,7 @@ export interface UserOptions { /** User's name. */ name: string; /** Unique User's hash. */ - hash: string; + hash?: string; /** Determine either an user is electable or not */ enabled?: boolean; /** User forms */ diff --git a/src/main.ts b/src/main.ts index 12b35c189fdc30ba5e17372a09ff47f2e6d40787..62dccccf5174d103a889da159e80eafa23854115 100755 --- a/src/main.ts +++ b/src/main.ts @@ -59,6 +59,7 @@ app.post("/user/signIn", UserCtrl.signIn); app.delete("/user/deleteData/:id", tokenValidation(), UserCtrl.deleteData); app.put("/user/changePassword", tokenValidation(), UserCtrl.changePassword); app.get("/user/list/:id", UserCtrl.listForms); +app.put("/user/update", tokenValidation(), UserCtrl.update); // Listening diff --git a/src/utils/dbHandler.spec.ts b/src/utils/dbHandler.spec.ts index 026fb3d417147c6609cca572c2f07a20db8cb8b4..35f07ca94463d16f3cdd643c3f973cccada7ba22 100644 --- a/src/utils/dbHandler.spec.ts +++ b/src/utils/dbHandler.spec.ts @@ -476,7 +476,7 @@ describe("Read and Write on Database", () => { }); it("Should update an user from the database", (done) => { - dbhandler.user.update(dbHandlerScenario.toupdate, (err: Error) => { + dbhandler.user.update(dbHandlerScenario.toupdate, dbHandlerScenario.toupdate.id, (err: Error) => { expect(err).to.be.a("null"); /** Read the user from DB and tests it with the updated user */ dbhandler.user.read(2, (error: Error, userResult: User) => { @@ -488,7 +488,7 @@ describe("Read and Write on Database", () => { }); it("Should update an user's enabled from the database", (done) => { - dbhandler.user.update(dbHandlerScenario.updateEnable, (err: Error) => { + dbhandler.user.update(dbHandlerScenario.updateEnable, dbHandlerScenario.updateEnable.id, (err: Error) => { expect(err).to.be.a("null"); /** Read the user from DB and tests it with the updated user */ dbhandler.user.read(3, (error: Error, userResult: User) => { diff --git a/src/utils/optHandler.ts b/src/utils/optHandler.ts index cd97a10263a3db8c9886ab4907db168094442832..b6639c4a5144b74b70cc8dbe3fe44ea60543ebdd 100644 --- a/src/utils/optHandler.ts +++ b/src/utils/optHandler.ts @@ -256,7 +256,7 @@ export class OptHandler { * @param obj - object to be parsed * @returns - an UserOptions instance */ - public static User(obj: any, hashedPw: string): UserOptions { + public static User(obj: any, hashedPw?: string): UserOptions { if (obj.name === undefined) { throw ErrorHandler.notFound("name"); } @@ -264,6 +264,17 @@ export class OptHandler { throw ErrorHandler.notFound("email"); } + if (! hashedPw) { + const optionNoHash: UserOptions = { + name: obj.name + , email: obj.email + , id: obj.id + , forms: obj.forms + }; + + return optionNoHash; + } + const option: UserOptions = { name: obj.name , email: obj.email diff --git a/src/utils/userQueryBuilder.ts b/src/utils/userQueryBuilder.ts index 1fd8a0973d275d73ddc829ad163e6f4b5e625311..4c7c7370f729c61ba2e3bc75bd2966c3dfc6fb1d 100644 --- a/src/utils/userQueryBuilder.ts +++ b/src/utils/userQueryBuilder.ts @@ -254,7 +254,7 @@ export class UserQueryBuilder extends QueryBuilder { * @param cb - Callback function which contains information about method's execution. * @param cb.err - Error information when the method fails. */ - public update(userUpdate: User, cb: (err: Error) => void) { + public update(userUpdate: User, id: number, cb: (err: Error) => void) { waterfall([ (callback: (err: Error, result?: QueryResult) => void) => { this.begin((error: Error, results?: QueryResult) => { @@ -262,7 +262,7 @@ export class UserQueryBuilder extends QueryBuilder { }); }, (callback: (err: Error, result?: User) => void) => { - this.executeUpdateUser(userUpdate, userUpdate.id, (error: Error) => { + this.executeUpdateUser(userUpdate, id, (error: Error) => { callback(error); }); }, @@ -277,6 +277,7 @@ export class UserQueryBuilder extends QueryBuilder { cb(err); return; }); + return; } cb(null); }); @@ -290,15 +291,14 @@ export class UserQueryBuilder extends QueryBuilder { * @param cb.err - Error information when method fails. */ private executeUpdateUser(value: User, id: number, cb: (err: Error) => void) { - const queryString: string = "UPDATE form_user SET name = $1, email = $2, hash = $3, enabled = $4 WHERE id = $5;"; + const queryString: string = "UPDATE form_user SET name = $1, email = $2, enabled = $3 WHERE id = $4;"; const query: QueryOptions = { query: queryString , parameters: [ value.name , value.email - , value.hash , value.enabled - , value.id + , id ] }; this.executeQuery(query, (err: Error, result?: QueryResult) => { diff --git a/test/scenario.ts b/test/scenario.ts index ef05dce52feeb909bac60a47c36d5c3287583d2e..3da766f9d002c181fd44d811b321f9482b1f77e8 100644 --- a/test/scenario.ts +++ b/test/scenario.ts @@ -2607,7 +2607,7 @@ const userToUpdate: User = new User ({ id: 2 , name: "User updated" , email: "testUpdate@test.com" - , hash: "hashTestUpdate" + , hash: "hashTest2" , enabled: true }); /** User to update another user's enabled (?) */