diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index f58a2e7a..7a253d96 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -5,6 +5,12 @@ 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). +## [Unreleased] + +### Fixed + +- Change the attribute used to check already exisiting emails when updating one's account + ## [2.0.0](https://github.com/hackmcgill/hackerapi/tree/2.0.0) - 2019-12-17 ### Added diff --git a/middlewares/account.middleware.js b/middlewares/account.middleware.js index 38b14170..3c4ef751 100644 --- a/middlewares/account.middleware.js +++ b/middlewares/account.middleware.js @@ -169,7 +169,7 @@ async function updateAccount(req, res, next) { // TODO: When pull request for parse patch refactor #546 hits, req.body.email will not be present. if (req.body.email && account.email != req.body.email) { const existingAccount = await Services.Account.findByEmail( - account.email + req.body.email ); if (existingAccount) { return next({ diff --git a/tests/account.test.js b/tests/account.test.js index 467f4101..ca456296 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -287,13 +287,15 @@ describe("PATCH update account", function() { const updatedInfo = { _id: teamHackerAccount0._id, firstName: "new", - lastName: "name" + lastName: "name", + email: "newUpdatedEmail@mail.com" }; const failUpdatedInfo = { _id: Admin0._id, firstName: "fail", - lastName: "fail" + lastName: "fail", + email: storedAccount1.email }; // fail on authentication @@ -358,6 +360,7 @@ describe("PATCH update account", function() { // Is this correct matching of data? res.body.data.firstName.should.equal(updatedInfo.firstName); res.body.data.lastName.should.equal(updatedInfo.lastName); + res.body.data.email.should.equal(updatedInfo.email); done(); }); }); @@ -387,6 +390,29 @@ describe("PATCH update account", function() { }); }); }); + + // fail due to attempt to update account email to one that already exists in DB + it("should FAIL to update email to one that already exists", function(done) { + util.auth.login(agent, Admin0, (error) => { + if (error) { + agent.close(); + return done(error); + } + agent + .patch(`/api/account/${failUpdatedInfo._id}`) + .type("application/json") + .send(failUpdatedInfo) + .end(function(err, res) { + res.should.have.status(409); + res.should.be.json; + res.body.should.have.property("message"); + res.body.message.should.equal( + Constants.Error.ACCOUNT_EMAIL_409_MESSAGE + ); + done(); + }); + }); + }); }); describe("POST reset password", function() {