Skip to content

Commit

Permalink
Merge pull request #100 from andela/chore/update-follow-controller
Browse files Browse the repository at this point in the history
chore: Refactor user follow controllers
  • Loading branch information
Rotimi Babalola committed Apr 17, 2019
2 parents caf5678 + 13dbc5c commit a5512c9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 75 deletions.
48 changes: 28 additions & 20 deletions server/controllers/follow.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import model from '../models';
import serverError from '../helpers/server-error';
import profileHelper from '../helpers/profiler';

const { User, Follower } = model;

Expand All @@ -21,14 +23,16 @@ const followController = {
const findUnFollowee = await User.findOne({ where: { id: followeeId } });
if (!findUnFollowee) {
return res.status(404).json({
status: 404,
message: 'User not found',
errors: {
body: ['User not found'],
},
});
}
if (findUnFollowee.id === followerId) {
res.status(403).json({
status: 403,
message: 'You cant follow yourself',
errors: {
body: ["You can't follow yourself"],
},
});
}
// Get follow record
Expand All @@ -44,20 +48,20 @@ const followController = {
followee_id: followeeId,
follower_id: followerId,
});

const userDetails = profileHelper.profiler(findUnFollowee);
return res.status(201).json({
status: 201,
message: `Successfully followed user`,
message: 'You have successfully followed this user',
user: userDetails,
});
}
return res.status(403).json({
status: 403,
message: 'You are already following',
errors: {
body: ['You are already following this user'],
},
});
} catch (error) {
return res.status(500).json({
status: 500,
message: 'Server error',
errors: serverError(),
});
}
},
Expand All @@ -72,15 +76,17 @@ const followController = {
// Check if user exist
if (!findFollowee) {
return res.status(404).json({
status: 404,
message: 'User not found',
errors: {
body: ['User not found'],
},
});
}
// Confirm if the user to be unfollow is the same as logged in user
if (findFollowee.id === followerId) {
return res.status(403).json({
status: 403,
message: 'You cant unfollow yourself',
errors: {
body: ["You can't unfollow yourself"],
},
});
}
// Get follow record.
Expand All @@ -94,7 +100,9 @@ const followController = {
// Confirm if user is already unfollowing
if (!followRecord) {
return res.status(404).json({
message: 'You are already unfollowing user',
errors: {
body: ['You are not currently following this user'],
},
});
}
// Unfollow a user
Expand All @@ -104,14 +112,14 @@ const followController = {
followee_id: unFolloweeId,
},
});
const userDetails = profileHelper.profiler(findFollowee);
return res.status(200).json({
status: 200,
message: 'Successfully unfollowed user',
message: 'You have successfully unfollowed this user',
user: userDetails,
});
} catch (error) {
return res.status(500).json({
status: 500,
message: 'Server error',
errors: serverError(),
});
}
},
Expand Down
5 changes: 3 additions & 2 deletions server/controllers/get-followers.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getFollowers = async (req, res) => {
});

return res.status(200).json({
message: 'Successfully retrieved all your followers',
message: 'You have successfully retrieved all your followers',
followers: users,
});
} catch (err) {
Expand Down Expand Up @@ -83,7 +83,8 @@ const getFollowing = async (req, res) => {
});
}
return res.status(200).json({
message: 'Successfully retrieved all users you are following',
message:
'You have successfully retrieved all the users you are following',
following: users,
});
} catch (errors) {
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/like.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const toggleLike = async (req, res) => {
if (userLike) {
result = await likeHelper.removeLike(user, article);
return res.status(200).json({
message: 'Successfully removed like',
message: 'You have successfully disliked this article',
data: result,
});
}
result = await likeHelper.addLike(user, article);
return res.status(201).json({
message: 'Successfuly added like',
message: 'You have successfuly liked this article',
data: result,
});
} catch (error) {
Expand Down
68 changes: 19 additions & 49 deletions tests/follow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app from '../server/app';
chai.use(chaiHttp);
let token1;
let token2;
let token3;
describe('TEST USER FOLLOW', () => {
it('Create a follower', done => {
chai
Expand Down Expand Up @@ -41,28 +40,31 @@ describe('TEST USER FOLLOW', () => {
});
});

it('It should return a 201 if a user is been followed successfully', done => {
it('should respond 201 when a user follows another user', done => {
chai
.request(app)
.post('/api/v1/follow/6517a6ea-662b-4eef-ab9f-20f89bd7099c')
.set('authorization', token1)
.end((err, res) => {
expect(res.status).to.equal(201);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('Successfully followed user');
expect(res.body.message).to.equal(
'You have successfully followed this user'
);
done();
});
});

it('It should return a 403 if you are already following a user', done => {
it('It should return 403 if a user tries to follow another user he is already following', done => {
chai
.request(app)
.post('/api/v1/follow/6517a6ea-662b-4eef-ab9f-20f89bd7099c')
.set('authorization', token1)
.end((err, res) => {
expect(res.status).to.equal(403);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('You are already following');
expect(res.body.errors.body[0]).to.equal(
'You are already following this user'
);
done();
});
});
Expand All @@ -74,79 +76,47 @@ describe('TEST USER FOLLOW', () => {
.set('authorization', token1)
.end((err, res) => {
expect(res.status).to.equal(500);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('Server error');
expect(res.body.errors.body[0]).to.equal(
'Server error, Please try again later'
);
done();
});
});
it('It should return a 403 if user try to follow him/herself', done => {
it('It should return a 403 if a user tries to follow him/herself', done => {
chai
.request(app)
.post('/api/v1/follow/6517a6ea-662b-4eef-ab9f-20f89bd7099c')
.set('authorization', token2)
.end((err, res) => {
expect(res.status).to.equal(403);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('You cant follow yourself');
expect(res.body.errors.body[0]).to.equal("You can't follow yourself");
done();
});
});
it('It should return a 406 if user try to follow him/herself', done => {
it('It should return a 404 if a user is not found', done => {
chai
.request(app)
.post('/api/v1/follow/6517a6ea-662b-4eef-ab9f-20f89bd7099b')
.set('authorization', token2)
.end((err, res) => {
expect(res.status).to.equal(404);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('User not found');
expect(res.body.errors.body[0]).to.equal('User not found');
done();
});
});
});
describe('TEST USER UNFOLLOW', () => {
it('Create a follower', done => {
chai
.request(app)
.post('/api/v1/auth/signup')
.send({
firstname: 'Dan',
lastname: 'chuks',
email: 'chuks@gmail.com',
password: 'chuks121',
confirmPassword: 'chuks121',
})
.end((err, res) => {
const { token } = res.body.user;
expect(res.status).to.equal(200);
token3 = token;
done();
});
});
it('lgoin a user', done => {
chai
.request(app)
.post('/api/v1/auth/login')
.send({
email: 'ameachichuks@gmail.com',
password: '12345678',
})
.end((err, res) => {
const { token } = res.body.user;
expect(res.status).to.equal(200);
token3 = token;
done();
});
});
it('It should return a 200 status code if user unfollow successfully', done => {
it('should respond 200 when a user unfollows another user', done => {
chai
.request(app)
.delete('/api/v1/follow/6517a6ea-662b-4eef-ab9f-20f89bd7099c')
.set('authorization', token1)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('Successfully unfollowed user');
expect(res.body.message).to.equal(
'You have successfully unfollowed this user'
);
done();
});
});
Expand Down
8 changes: 6 additions & 2 deletions tests/likes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('TEST LIKE', () => {
.end((err, res) => {
expect(res.status).to.equal(201);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('Successfuly added like');
expect(res.body.message).to.equal(
'You have successfuly liked this article'
);
done();
});
});
Expand All @@ -42,7 +44,9 @@ describe('TEST LIKE', () => {
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('Successfully removed like');
expect(res.body.message).to.equal(
'You have successfully disliked this article'
);
done();
});
});
Expand Down

0 comments on commit a5512c9

Please sign in to comment.