Skip to content

Commit

Permalink
Bug(Migration): fix errors in migration
Browse files Browse the repository at this point in the history
- when running migration, database will be created without clearing existing data

[Delivers #165374381]
  • Loading branch information
Jaman-dedy authored and Itsgracian committed Apr 25, 2019
1 parent 406ec0d commit e1ca640
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions controllers/followers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class FollowerController {
}
return res.status(200).json({ status: 200, numberOfFollowers: number.length, followers });
})
.catch(error => res.status(500).json({ error }));
.catch(error => res.status(500).json({ error: 'something wrong try again.' }));
}

/**
Expand All @@ -76,7 +76,7 @@ class FollowerController {
}
return res.status(200).json({ status: 200, numberOfFollowing: number.length, following });
})
.catch(error => res.status(500).json({ error }));
.catch(error => res.status(500).json({ error: 'something wrong try again.' }));
}
}
export default FollowerController;
1 change: 1 addition & 0 deletions helpers/validateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Validate {
});
}


/**
* Validate user's rating
* @param {number} rating - User ratings
Expand Down
18 changes: 9 additions & 9 deletions middlewares/followers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Sequelize from 'sequelize';
import models from '../models/index';
import validate from '../helpers/validateUser';
import httpError from '../helpers/errors/httpError';

const Follower = models.follower;
const User = models.user;
Expand All @@ -20,13 +22,11 @@ export const checkFollowedBy = async (req, res, next) => {
};

export const checkUserId = async (req, res, next) => {
await User.findByPk(req.params.userId)
.then((user) => {
if (!user) {
return res.status(404).json({ status: 404, error: 'sorry user not found.' });
}
req.followedBy = user.username;
next();
})
.catch(error => res.status(500).json({ status: 500, error }));
await validate.isInteger(req.params.userId, 'User');
const user = await User.findByPk(req.params.userId);
if (!user) {
throw new httpError(404, 'sorry user not found');
}
req.followedBy = user.username;
next();
};
1 change: 1 addition & 0 deletions migrations/20190326112352-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ module.exports = {
}
}),
down: queryInterface => queryInterface.dropTable('users')

};
13 changes: 7 additions & 6 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import multer from '../../middlewares/multerConfiguration';
import { passwordValidation } from '../../middlewares/passwordValidate';
import articleStats from '../../controllers/stats/articleStats';
import asyncHandler from '../../helpers/errors/asyncHandler';
import activate from '../../middlewares/userAccount';


const router = express.Router();
Expand All @@ -38,20 +39,20 @@ router.put('/profile', auth, multer, User.updateProfile);
// @method POST
// @desc follow user
// @access private
router.post('/follow/:userId', auth, checkUserId, checkFollowedBy, Follow.follow);
router.post('/follow/:userId', auth, asyncHandler(activate.isUserAccountActivated),
asyncHandler(checkUserId), checkFollowedBy, Follow.follow);
// @method DELETE
// @desc Unfollow user
// @access private
router.delete('/unfollow/:userId', auth, checkUserId, Follow.unfollow);
router.delete('/unfollow/:userId', auth, asyncHandler(activate.isUserAccountActivated),
asyncHandler(checkUserId), Follow.unfollow);
// @method GET
// @desc get followers of users
// @access private
router.get('/followers', auth, Follow.followers);
router.get('/followers', auth, asyncHandler(activate.isUserAccountActivated), Follow.followers);
// @method GET
// @desc get following user
// @access private
router.get('/following', auth, Follow.following);

router.get('/following', auth, asyncHandler(activate.isUserAccountActivated), Follow.following);
router.get('/reading-stats', auth, asyncHandler(articleStats.getUserReadingStats));

export default router;
24 changes: 18 additions & 6 deletions test/8-followers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../index';
import { login1, login4 } from '../testingdata/user.json';
import { testMailer, login4 } from '../testingdata/user.json';

chai.use(chaiHttp);
chai.should();
Expand All @@ -11,7 +11,7 @@ let userId2;
describe('Followers', () => {
before(async () => {
try {
const login = await chai.request(app).post('/api/users/login').set('Content-Type', 'application/json').send(login1);
const login = await chai.request(app).post('/api/users/login').set('Content-Type', 'application/json').send(testMailer);
const login2 = await chai.request(app).post('/api/users/login').set('Content-Type', 'application/json').send(login4);
token = `Bearer ${login.body.token}`;
userId2 = login.body.user.id;
Expand All @@ -23,14 +23,26 @@ describe('Followers', () => {
// check
it('Should return status of 404 (follow user)', (done) => {
chai.request(app)
.post('/api/users/follow/100005667')
.post('/api/users/follow/5000')
.set('Authorization', token)
.set('Content-Type', 'application/json')
.end((error, res) => {
if (error) done(error);
res.should.have.status(404);
res.body.should.have.property('status');
res.body.should.have.property('error');
res.body.should.have.property('errors');
done();
});
});
it('Should return status of 500 (follow user)', (done) => {
chai.request(app)
.post('/api/users/follow/50008745648365847465746547564765746547564')
.set('Authorization', token)
.set('Content-Type', 'application/json')
.end((error, res) => {
if (error) done(error);
res.should.have.status(500);
res.body.should.have.property('errors');
done();
});
});
Expand Down Expand Up @@ -103,7 +115,7 @@ describe('Followers', () => {
if (error) done(error);
res.should.have.status(404);
res.body.should.have.property('status');
res.body.should.have.property('error');
res.body.should.have.property('errors');
done();
});
});
Expand All @@ -117,7 +129,7 @@ describe('Followers', () => {
if (error) done(error);
res.should.have.status(500);
res.body.should.have.property('status');
res.body.should.have.property('error');
res.body.should.have.property('errors');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion testingdata/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"password": "1Kig1L@20"
},
"googleValidToken": {
"access_token": "ya29.Glv2Bp7QVyQ3SM4WAbypJoZiVxEcIAJqTFn1bDfpvI-K8fVa-orlCmMQCOSGSJTeyy54VwKD900_97FbD7vqJNjnamWPFCQcQAv_ZhABP31oknn8j74S6EEfzJ4v"
"access_token": "ya29.Glv2BitbuYw7XcTEcc2lKYfT84eVe0fpa1UOqfbKyk4saT-QM8zIR9aJ0zwDcpweHFLU8n__EXTCBGGyiGKOXJGTBDzIhfOahZ7rpfrP6FCrqwRPC6SQ8j-e2R2t"
},
"googleInvalidToken": {
"access_token": "ya29.Glv1Bvx5U-OTtL7Qxym4ugGyyRsxhEPoKe_Uz5cKKF_mLuGzrxHywvzLx_kgWKVfHZTKL2eg0K5oJ7RVKXwqDSG5-f5PUlo0iFz5jISJrVWsc5Ls7crApI-Uagjy"
Expand Down

0 comments on commit e1ca640

Please sign in to comment.