Skip to content

Commit

Permalink
Merge 5568c83 into c7005e9
Browse files Browse the repository at this point in the history
  • Loading branch information
mnzube committed Jul 23, 2019
2 parents c7005e9 + 5568c83 commit 1094180
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 120 deletions.
172 changes: 77 additions & 95 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
"sequelize-cli": "^5.5.0",
"social-share": "^0.1.0",
"socket.io": "^2.2.0",
"swagger-ui-express": "^4.0.6"
"swagger-ui-express": "^4.0.6",
"chai-http": "^4.3.0",
"chai": "^4.2.0"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.4.4",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"eslint": "^6.0.1",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.17.3",
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/ratingsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ class Rate {
: newRating = await Rating.create({ rate: req.body.rating,
reviewerId: id,
articleId: article.dataValues.id });
return newRating && res.status(204).json({ message: 'You have Rated the article' });
return newRating && res.status(200).json({ message: 'You have Rated the article' });
}
return res.status(400).json({ error: 'Bad Rating range' });
}

static async getAllRatings(req, res) {
const articleId = req.params.id;
const rating = await Rating.findAll({ where: { articleId } });

return rating[0] ? res.status(200).json({ rating }) : res.status(404).json({ message: 'No ratings found' });
const { id: articleId } = req.params;
try {
const { offset, limit } = req.query;
const ratingsCount = await Rating.findAll({ offset, limit }, { where: { id: articleId } });
return ratingsCount.length ? res.status(200).json({ ratingsCount }) : res.status(404).json({ message: 'No Ratings found' });
} catch (error) {
return res.status(404).json({ error: 'No article found' });
}
}
}
export default Rate;
10 changes: 5 additions & 5 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class UserController {
email: user.email,
verified: user.verified };

const token = await generateToken({ payload });
const token = await generateToken(payload);
const mailSend = await MailSender.sendMail(user.email, user.username, token);

if (mailSend[0].statusCode === 202) {
Expand All @@ -61,7 +61,7 @@ export default class UserController {
const payload = { username: user.username,
userId: user.id,
email: user.email };
const token = await generateToken({ payload });
const token = await generateToken(payload);
// @sends a message to an existing email in our database with the below email template
const message = `<div>You are receiving this because you (or someone else) requested the reset of your password.<br>
Please click on the followoing link or paste this link in youre browser to complete this process within one hour: <Br>
Expand Down Expand Up @@ -109,10 +109,10 @@ export default class UserController {
static async verifyUser(req, res) {
try {
const decode = await decodeToken(req.params.userToken);
if (!decode.payload.email || decode.payload.verified) {
return (!decode.payload.email && res.status(404).json({ error: `Email:${decode.payload.email} does not exist in the database` })) || (decode.payload.verified && res.status(409).json({ error: 'Your account is already verified' }));
if (!decode.email || decode.verified) {
return (!decode.email && res.status(409).json({ error: `Email:${decode.email} does not exist in the database` })) || (decode.verified && res.status(409).json({ error: 'Your account is already verified' }));
}
await User.update({ verified: true }, { where: { email: decode.payload.email } });
await User.update({ verified: true }, { where: { email: decode.email } });
return res.status(200).json({ message: 'Your account is now verified you can login with your email', });
} catch (error) {
return res.status(500).json(error.message);
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import passport from 'passport';
import session from 'express-session';
import swaggerUi from 'swagger-ui-express';
import swagger from '../swagger.json';

// eslint-disable-next-line import/no-named-as-default
import route from './routes/index';
import './config/passport';
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Auth {
req.user = user.dataValues;
next();
} catch (error) {
res.status(401).json({ error: 'invalid token' });
res.status(401).json({ error: 'Server Error' });
}
}
}
4 changes: 2 additions & 2 deletions src/test/articlesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Article', () => {
.end((req, res) => {
res.should.have.status(401);
res.body.should.be.an('object');
res.body.should.have.property('error').eql('invalid token');
res.body.should.have.property('error');
done();
});
});
Expand All @@ -154,7 +154,7 @@ describe('Article', () => {
.end((req, res) => {
res.should.have.status(401);
res.body.should.be.an('object');
res.body.should.have.property('error').eql('invalid token');
res.body.should.have.property('error');
done();
});
});
Expand Down
17 changes: 13 additions & 4 deletions src/test/ratingsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Rating an article', () => {
article = await Articles.create(articleObject);
});

it('User should not be able to fetch all ratings from articles', (done) => {
it('User should not be able to fetch ratings from articles', (done) => {
chai.request(app)
.get(`/api/articles/${article.id}/rating`)
.set('token', tokenGen)
Expand All @@ -44,8 +44,17 @@ describe('Rating an article', () => {
done();
});
});
it('should not be able to fetch ratings from articles because they have not been rated', (done) => {
chai.request(app)
.get(`/api/articles/${article.id}/ratings`)
.set('token', tokenGen)
.end((err, res) => {
res.should.have.status(404);
done();
});
});

it('User shoud not be able to fetch all ratings from bad article articles', (done) => {
it('User shoud not be able to fetch ratings from non existing articles', (done) => {
chai.request(app)
.get('/api/articles/10000/rating')
.set('token', tokenGen)
Expand Down Expand Up @@ -73,13 +82,13 @@ describe('Rating an article', () => {
.set('token', tokenGen)
.send({ rating: 3 })
.end((err, res) => {
res.should.have.status(204);
res.should.have.status(200);
res.should.be.an('object');
done();
});
});

it('User shoud be able to fetch all ratings of an article', (done) => {
it('User should be able to fetch all ratings of an article', (done) => {
chai.request(app)
.get(`/api/articles/${article.id}/ratings`)
.set('token', tokenGen)
Expand Down
11 changes: 6 additions & 5 deletions swagger.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"swagger": "2.0",
"info": {
"description": "A Social platform for the creative at heart.\n",
Expand Down Expand Up @@ -164,8 +164,7 @@
"type": "string"
}
}
}
},
},
"ratings": {
"required": [
"rating"
Expand All @@ -175,7 +174,8 @@
"type": "integer"
}
}
},
}
},
"paths" : {
"/users": {
"post": {
Expand Down Expand Up @@ -1534,6 +1534,7 @@
}
}
}
}}
}
}


0 comments on commit 1094180

Please sign in to comment.