Skip to content

Commit

Permalink
nitpix(userProfile): fix bugs on user-login and user-update endpoints.
Browse files Browse the repository at this point in the history
- for login, error messages on email and password should be the same.
- remove user’s hashed password from user-update response
- response body on update should be named ‘user’

[Fixes #160876621]
  • Loading branch information
emekafredy committed Sep 30, 2018
1 parent 8759dab commit b262c04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
39 changes: 21 additions & 18 deletions server/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class UsersController {
* @param {object} req body of the user's request
* @param {object} res body of the response message
* @param {function} next next function to be called
* @returns {object} The body of the resposne message
* @returns {object} The body of the response message
*/
static userLogin(req, res, next) {
const { error, isValid } = UserValidation.validateLoginInput(req.body);
Expand All @@ -108,7 +108,7 @@ class UsersController {
return res.status(404).json({
status: 'error',
error: {
email: 'User not found'
message: 'Email or Password is incorrect'
}
});
}
Expand Down Expand Up @@ -140,7 +140,7 @@ class UsersController {
return res.status(400).json({
status: 'error',
error: {
password: 'Incorrect Password'
message: 'Email or Password is incorrect'
}
});
});
Expand All @@ -153,7 +153,7 @@ class UsersController {
* @param {object} req body of the user's request
* @param {object} res body of the response message
* @param {function} next next function to be called
* @returns {object} The body of the resposne message
* @returns {object} The body of the response message
*/
static getProfileByUsername(req, res, next) {
const { username } = req.params;
Expand Down Expand Up @@ -185,7 +185,7 @@ class UsersController {
* @param {object} req body of the user's request
* @param {object} res body of the response message
* @param {function} next next function to be called
* @returns {object} The body of the resposne message
* @returns {object} The body of the response message
*/
static updateUserProfile(req, res, next) {
const { userId } = req.params;
Expand Down Expand Up @@ -227,8 +227,8 @@ class UsersController {
}

User.findById(userId)
.then((user) => {
if (!user) {
.then((userFound) => {
if (!userFound) {
return res.status(404).json({
status: 'error',
error: {
Expand All @@ -246,21 +246,24 @@ class UsersController {
}
});
}
return user.update({
firstName: trimInput(firstName) || user.firstName,
lastName: trimInput(lastName) || user.lastName,
username: trimInput(username) || user.username,
bio: trimInput(bio) || user.bio,
role: role || user.role,
twitter: twitter || user.twitter,
linkedin: linkedin || user.linkedin,
facebook: facebook || user.facebook,
image: image || user.image,
return userFound.update({
firstName: trimInput(firstName) || userFound.firstName,
lastName: trimInput(lastName) || userFound.lastName,
username: trimInput(username) || userFound.username,
bio: trimInput(bio) || userFound.bio,
role: role || userFound.role,
twitter: twitter || userFound.twitter,
linkedin: linkedin || userFound.linkedin,
facebook: facebook || userFound.facebook,
image: image || userFound.image,
}).then((updatedUser) => {
const { dataValues } = updatedUser;
const { hash, id, ...rest } = dataValues;
return res.status(200).json({
status: 'success',
dataValues
user: {
...rest
}
});
}).catch(next);
}).catch(next);
Expand Down
4 changes: 2 additions & 2 deletions server/tests/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('Users Controllers', () => {
.send(wrongEmail)
.end((error, res) => {
expect(res).to.have.status(404);
res.body.error.email.should.equal('User not found');
res.body.error.message.should.equal('Email or Password is incorrect');
done();
});
});
Expand All @@ -135,7 +135,7 @@ describe('Users Controllers', () => {
.send(incorrectDetails)
.end((error, res) => {
expect(res).to.have.status(400);
res.body.error.password.should.equal('Incorrect Password');
res.body.error.message.should.equal('Email or Password is incorrect');
done();
});
});
Expand Down
20 changes: 10 additions & 10 deletions server/tests/controllers/users_profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ describe('Users Controllers', () => {
bio: 'A very good and prolific Author'
})
.end((error, res) => {
const { dataValues } = res.body;
const { user } = res.body;
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
dataValues.should.have.property('lastName');
dataValues.should.have.property('image');
user.should.have.property('lastName');
user.should.have.property('image');
done();
});
});
Expand All @@ -83,12 +83,12 @@ describe('Users Controllers', () => {
role: 'author'
})
.end((error, res) => {
const { dataValues } = res.body;
const { user } = res.body;
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
dataValues.should.have.property('lastName');
dataValues.should.have.property('image');
dataValues.role.should.equal('author');
user.should.have.property('lastName');
user.should.have.property('image');
user.role.should.equal('author');
done();
});
});
Expand Down Expand Up @@ -167,11 +167,11 @@ describe('Users Controllers', () => {
.set('Authorization', token)
.send(updateContent)
.end((error, res) => {
const { dataValues } = res.body;
const { user } = res.body;
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
dataValues.should.have.property('lastName');
dataValues.should.have.property('image');
user.should.have.property('lastName');
user.should.have.property('image');
done();
});
});
Expand Down

0 comments on commit b262c04

Please sign in to comment.