Skip to content

Commit

Permalink
Merge d0dbb54 into acd8044
Browse files Browse the repository at this point in the history
  • Loading branch information
McHardex authored Apr 5, 2019
2 parents acd8044 + d0dbb54 commit e3a2bc5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
4 changes: 2 additions & 2 deletions server/controllers/rating.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import model from '../models';

const { Rating } = model;
const { databaseError, findArticle } = search;

const rating = {
post: async (req, res) => {
const userId = req.user.userObj.id;
/**
* @description post article Rating
* @param {*} req
Expand All @@ -24,7 +24,7 @@ const rating = {
});
}
const ratingDetails = await Rating.create({
user_id: req.body.user_id,
user_id: userId,
article_id: req.body.article_id,
rating_value: req.body.rating_value,
});
Expand Down
21 changes: 11 additions & 10 deletions server/helpers/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,31 @@ const validations = {
return valid;
},
verifyAuthHeader(req) {
if (!req.headers.authorization) {
return { error: 'error' };
}
const token = req.headers.authorization;
const payload = Authenticate.decode(token);
if (!payload) {
try {
if (!req.headers.authorization) {
return { error: 'error' };
}
const token = req.headers.authorization;
const payload = Authenticate.decode(token);
return payload;
} catch (err) {
return { error: 'Invalid token' };
}
return payload;
},

/**
* @method verifyUser
* @method verifyToken
* @description Verifies the token provided by the user
* @param {*} req
* @param {*} res
* @returns {*} - JSON response object
*/

verifyUser(req, res, next) {
verifyToken(req, res, next) {
const payload = validations.verifyAuthHeader(req);
let error;
let status;
if (!payload || payload === 'error') {
if (!payload || payload.error === 'error') {
status = 401;
error = 'You are not authorized';
}
Expand Down
5 changes: 0 additions & 5 deletions server/joiSchema/ratingSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ const rating = (req, res, next) => {
const data = req.body;

const schema = Joi.object().keys({
user_id: Joi.string()
.guid({
version: ['uuidv4'],
})
.required(),
article_id: Joi.string()
.guid({
version: ['uuidv4'],
Expand Down
8 changes: 7 additions & 1 deletion server/routes/rating.routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import express from 'express';
import controllers from '../controllers';
import ratingValidation from '../joiSchema/ratingSchema';
import validation from '../helpers/validations';

const { ratingController } = controllers;

const router = express.Router();

router.post('/rating', ratingValidation, ratingController.post);
router.post(
'/rating',
validation.verifyToken,
ratingValidation,
ratingController.post
);

export default router;
57 changes: 48 additions & 9 deletions tests/rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@ import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../server/app';

let userToken;
chai.use(chaiHttp);

const rating = {
user_id: '57c515a1-890d-412f-8ca1-0a5395123dca',
article_id: '7139d3af-b8b4-44f6-a49f-9305791700f4',
rating_value: 3,
};

describe('POST RATING', () => {
it('should return 400 with invalid or empty payload(user_id)', done => {
before(async () => {
await chai
.request(app)
.post('/api/v1/auth/signup')
.send({
firstname: 'Adebisi',
lastname: 'Adebisi',
email: 'bukunmi@gmail.com',
password: 'h0ttestt',
confirmPassword: 'h0ttestt',
});

const userDetails = await chai
.request(app)
.post('/api/v1/auth/login')
.send({
email: 'bukunmi@gmail.com',
password: 'h0ttestt',
});
userToken = userDetails.body.user.token;
});
it('should return 401 error when no token is provided', done => {
chai
.request(app)
.post('/api/v1/rating')
Expand All @@ -20,10 +41,26 @@ describe('POST RATING', () => {
rating_value: rating.rating_value,
})
.end((req, res) => {
const { status, errors } = res.body;
expect(status).to.be.equal(400);
expect(res.status).to.be.equal(401);
expect(res).to.be.an('object');
expect(res.body.error).to.equal('You are not authorized');
done();
});
});

it('should return 403 error when invalid token is provided', done => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', 'eeeee')
.send({
article_id: rating.article_id,
rating_value: rating.rating_value,
})
.end((req, res) => {
expect(res.status).to.be.equal(403);
expect(res).to.be.an('object');
expect(errors.body[0]).to.equal('user_id is required');
expect(res.body.error).to.equal('Forbidden');
done();
});
});
Expand All @@ -32,8 +69,8 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send({
user_id: rating.user_id,
rating_value: rating.rating_value,
})
.end((req, res) => {
Expand All @@ -48,8 +85,8 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send({
user_id: rating.user_id,
article_id: rating.article_id,
})
.end((req, res) => {
Expand All @@ -64,8 +101,8 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send({
user_id: rating.user_id,
article_id: '8139d3af-b8b4-44f6-a49f-9305791700f4',
rating_value: rating.rating_value,
})
Expand All @@ -81,8 +118,8 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send({
user_id: rating.user_id,
article_id: rating.article_id,
rating_value: 7,
})
Expand All @@ -100,6 +137,7 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send(rating)
.end((req, res) => {
expect(res).to.have.status(201);
Expand All @@ -120,6 +158,7 @@ describe('POST RATING', () => {
chai
.request(app)
.post('/api/v1/rating')
.set('Authorization', userToken)
.send(rating)
.end((req, res) => {
const { status, errors } = res.body;
Expand Down

0 comments on commit e3a2bc5

Please sign in to comment.