Skip to content

Commit

Permalink
update package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond-Osy committed Apr 16, 2019
1 parent 3ac1768 commit d0971bb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 223 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"db-migrate:undo": "./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:migrate:undo:all",
"db-migrate:undo:test": "NODE_ENV=test ./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:migrate:undo:all",
"db-refresh": "npm run db-migrate:undo && npm run db-migrate && npm run db-seed",
"db-refresht": "npm run db-migrate:undo && npm run db-migrate && npm run db-seed && npm run test",
"db-refresh:test": "NODE_ENV=test npm run db-migrate:undo:test && npm run db-migrate:test && npm run db-seed:test",
"db-seed": "./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:seed:all",
"db-seed:test": "NODE_ENV=test ./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:seed:all",
Expand Down
88 changes: 16 additions & 72 deletions src/controllers/follower.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,28 @@ export const followUser = async (req, res) => {
try {
const { user } = req;
const { followerId } = req.params;
const findUser = await User.findOne({ where: { id: followerId } });
if (!findUser) {
return res.status(404).json(
errorResponseFormat({
status: 'fail',
message: 'User not found!'
})
);
const followee = await User.findOne({ where: { id: followerId } });
if (!followee) {
return res.status(404).json(responseFormat({
status: 'fail',
data: 'This user does not exist'
}));
}
if (findUser.id === user.id) {
if (followerId === user.id) {
return res.status(403).json(
errorResponseFormat({
responseFormat({
status: 'fail',
message: 'Sorry, you can not follow yourself'
data: 'Sorry, you can not follow yourself'
})
);
}
const isFollowed = await Follower.findOrCreate({ where: { userId: user.id, followerId }
});
if (isFollowed[1] === false) {
return res.status(400).json(
errorResponseFormat({
status: 'fail',
message: 'You are already following this user'
const [, created] = await Follower.findOrCreate({ where: { userId: user.id, followerId } });
if (!created) {
await Follower.destroy({ where: { userId: user.id, followerId } });
return res.status(200).json(
responseFormat({
status: 'success',
data: 'You have successfully unfollowed this user'
})
);
}
Expand All @@ -49,60 +47,6 @@ export const followUser = async (req, res) => {
} catch (error) {
return res.status(400).json(
errorResponseFormat({
status: 'fail',
message: 'Something went wrong, please try again later!'
})
);
}
};

/**
* @name unFollowUser
* @description This is the method for indicationg a user to follow in the request body
* @param {object} req The request object
* @param {object} res The response object
* @returns {int} Returns the followed user
*/
export const unfollowUser = async (req, res) => {
try {
const { user } = req;
const { followerId } = req.params;
const findUser = await User.findOne({ where: { id: followerId } });
if (!findUser) {
return res.status(404).json(
errorResponseFormat({
status: 'fail',
message: 'User not found!'
})
);
}
if (findUser.id === user.id) {
return res.status(403).json(
errorResponseFormat({
status: 'fail',
message: 'Sorry, you can not unfollow yourself'
})
);
}
const unfollowResult = await Follower.findOne({ where: { userId: user.id, followerId } });
if (!unfollowResult) {
return res.status(404).json(
errorResponseFormat({
status: 'fail',
message: 'Sorry, You are already unfollowing this user'
})
);
}
await unfollowResult.destroy({ where: { userId: user.id, followerId } });
return res.status(200).json(
responseFormat({
status: 'success',
data: 'You have successfully unfollowed user'
}));
} catch (error) {
return res.status(400).json(
errorResponseFormat({
status: 'error',
message: 'Something went wrong, please try again later!'
})
);
Expand Down
7 changes: 0 additions & 7 deletions src/routers/follower.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import checkBody from '../middlewares/signUpValidator';
import likeArticle from '../controllers/like';
import articleValidation, { verifyArticle, isAuthor } from '../middlewares/articles';
import { checkParam } from '../middlewares/checkParam';
import { followUser, unfollowUser } from '../controllers/follower';
import { followUser } from '../controllers/follower';


const router = Router();
Expand Down Expand Up @@ -73,8 +73,7 @@ router.get('/auth/linkedin', linkedinUser);
router
.post('/signup', checkBody, createUser);
router.post('/login', checkFields, login);
router.post('/followers/:followerId/follow', Auth.verifyToken, followUser);
router.delete('/followers/:followerId/unfollow', Auth.verifyToken, unfollowUser);
router.post('/followers/:followerId/follow', Auth.authenticateUser, followUser);


// route for twitter authentication
Expand Down
82 changes: 82 additions & 0 deletions tests/integration/follower.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import { startServer } from '../../src/server';
import { testUser } from '../mock/followers';
import Auth from '../../src/middlewares/authenticator';


const { expect } = chai;

chai.use(chaiHttp);
describe('User Following API test', () => {
let app = null;
let agent = null;

beforeEach(async () => {
app = await startServer(5000);
agent = chai.request(app);
});

afterEach(async () => {
await app.close();
agent = null;
});

const token = Auth.generateToken(testUser);

it('Should follow a user successfully', (done) => {
agent
.post('/api/v1/followers/979eaa2e-5b8f-4103-8192-4639afae2ba7/follow')
.set('Authorization', token)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.have.property('status').eql('success');
expect(res.body).to.have.property('data').to.be.a('string');
done();
});
});
it('Should unfollow a user successfully', (done) => {
agent
.post('/api/v1/followers/979eaa2e-5b8f-4103-8192-4639afae2ba7/follow')
.set('Authorization', token)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.have.property('status').eql('success');
expect(res.body).to.have.property('data').to.equal('You have successfully unfollowed this user');
done();
});
});
it('Should return status 403 if user tries following self', (done) => {
agent
.post('/api/v1/followers/979eaa2e-5b8f-4103-8192-4639afae2ba9/follow')
.set('Authorization', token)
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body).to.have.property('status').eql('fail');
expect(res.body).to.have.property('data').to.equal('Sorry, you can not follow yourself');
done();
});
});
it('Should return an error if user not found in user table', (done) => {
agent
.post('/api/v1/followers/979eaa2e-5b8f-4103-8192-4639afae2ba0/follow')
.set('Authorization', token)
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.have.property('status').eql('fail');
expect(res.body).to.have.property('data').to.equal('This user does not exist');
done();
});
});
it('Should return 500 for server error', (done) => {
agent
.post('/api/v1/followers/979eaa2e-5b8f-4103-8192-4639afae2ba/follow')
.set('Authorization', token)
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body).to.have.property('status').eql('error');
expect(res.body).to.have.property('message').to.equal('Something went wrong, please try again later!');
done();
});
});
});
6 changes: 6 additions & 0 deletions tests/mock/followers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export const userData = {
email: 'martins@gmail.com',
password: '%RYYT&^UTB*UYT*IUYIU',
};

export const testUser = {
id: '979eaa2e-5b8f-4103-8192-4639afae2ba9',
fullName: 'Chike Ozulumba',
email: 'testuser@email.com',
};
140 changes: 0 additions & 140 deletions tests/unit/follower.test.js

This file was deleted.

0 comments on commit d0971bb

Please sign in to comment.