From e1ca640d9b551de3db0599d8947ab6fbde384d97 Mon Sep 17 00:00:00 2001 From: Jaman dedy Date: Tue, 16 Apr 2019 10:35:37 +0200 Subject: [PATCH] Bug(Migration): fix errors in migration - when running migration, database will be created without clearing existing data [Delivers #165374381] --- controllers/followers.js | 4 ++-- helpers/validateUser.js | 1 + middlewares/followers.js | 18 +++++++++--------- migrations/20190326112352-user.js | 1 + routes/api/users.js | 13 +++++++------ test/8-followers.js | 24 ++++++++++++++++++------ testingdata/user.json | 2 +- 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/controllers/followers.js b/controllers/followers.js index 47307a5..a27a7a9 100644 --- a/controllers/followers.js +++ b/controllers/followers.js @@ -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.' })); } /** @@ -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; diff --git a/helpers/validateUser.js b/helpers/validateUser.js index e87466b..ddd48bb 100644 --- a/helpers/validateUser.js +++ b/helpers/validateUser.js @@ -54,6 +54,7 @@ class Validate { }); } + /** * Validate user's rating * @param {number} rating - User ratings diff --git a/middlewares/followers.js b/middlewares/followers.js index 8f7180c..9320d39 100644 --- a/middlewares/followers.js +++ b/middlewares/followers.js @@ -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; @@ -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(); }; diff --git a/migrations/20190326112352-user.js b/migrations/20190326112352-user.js index 50dc8cc..96e04fe 100644 --- a/migrations/20190326112352-user.js +++ b/migrations/20190326112352-user.js @@ -49,4 +49,5 @@ module.exports = { } }), down: queryInterface => queryInterface.dropTable('users') + }; diff --git a/routes/api/users.js b/routes/api/users.js index dc181dd..963208d 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -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(); @@ -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; diff --git a/test/8-followers.js b/test/8-followers.js index 5b8cf88..0e4e3ce 100644 --- a/test/8-followers.js +++ b/test/8-followers.js @@ -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(); @@ -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; @@ -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(); }); }); @@ -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(); }); }); @@ -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(); }); }); diff --git a/testingdata/user.json b/testingdata/user.json index e394b54..304b24b 100644 --- a/testingdata/user.json +++ b/testingdata/user.json @@ -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"