Skip to content

Commit

Permalink
Merge pull request #18 from Billmike/feature/modify-user-details
Browse files Browse the repository at this point in the history
feature/modify-user-details
  • Loading branch information
Billmike committed Apr 20, 2018
2 parents b6e7c60 + f4af3e1 commit 1e77a70
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
37 changes: 37 additions & 0 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,43 @@ class Users {
});
});
}

/**
* Modify user details
*
* @param {object} request - The request object
* @param {object} response - The response object
*
* @returns {object} The user object
*/
static modifyUserDetails(request, response) {
User.findById(request.userDetails.id)
.then((userDetail) => {
if (!userDetail) {
return response.status(400).json({
message: 'User not found.'
});
}
return userDetail.update({
username: request.body.username || userDetail.username,
email: request.body.email || userDetail.email,
phoneNumber: request.body.phoneNumber || userDetail.phoneNumber
}).then((updatedProfile) => {
return response.status(201).json({
message: 'Profile updated successfully',
userDetails: {
username: updatedProfile.username,
email: updatedProfile.email,
phoneNumber: updatedProfile.phoneNumber
}
});
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Users;
5 changes: 5 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ module.exports = (app) => {
sessionControl.isLoggedIn, sessionControl.isUser,
userController.getUserEvents
);
app.put(
'/api/v1/user/edit',
sessionControl.isLoggedIn, sessionControl.isUser,
userController.modifyUserDetails
);
};
27 changes: 27 additions & 0 deletions server/tests/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,33 @@ describe('Integration tests for Authentication', () => {
done();
});
});
it(
'should return an error if the user to be edited is not found',
(done) => {
request.put(`/api/v1/user/edit?token=${nonExistentUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(nonExistentUser)
.end((error, response) => {
expect(response.status).to.equal(400);
done();
});
}
);
it('should modify the details of a signed in user', (done) => {
const testUser = { ...dummyUser };
testUser.phoneNumber = '09089898989';
request.put(`/api/v1/user/edit?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(testUser)
.end((error, response) => {
expect(response.status).to.equal(201);
done();
});
});
it('should update the password of a signed in user', (done) => {
const testUser = {
currentPassword: dummyUser.password,
Expand Down

0 comments on commit 1e77a70

Please sign in to comment.