Skip to content

Commit

Permalink
Merge 67e47e4 into d659ca8
Browse files Browse the repository at this point in the history
  • Loading branch information
habinezadalvan committed Sep 3, 2019
2 parents d659ca8 + 67e47e4 commit 7c2864b
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/controllers/likes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class LikesController {
return util.send(res);
}
} catch (error) {
util.setError(400, 'server error contact admin');
util.setError(500, 'server error contact admin');
return util.send(res);
}
}
Expand All @@ -174,7 +174,7 @@ class LikesController {
return util.send(res);
}
} catch (error) {
util.setError(400, 'server error contact admin');
util.setError(500, 'server error contact admin');
return util.send(res);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/cloudinaryHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dotenv/config';
import cloudinary from 'cloudinary';

const generateCloudinaryUrl = async (images = []) => Promise.all(
export const generateCloudinaryUrl = async (images = []) => Promise.all(
images.map(async (file) => {
const { secure_url } = await cloudinary.v2.uploader.upload(file.path);
return secure_url;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/verification-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config';

const sgMail = require('@sendgrid/mail');

const sendEmail = (email, username, url) => {
export const sendEmail = (email, username, url) => {
sgMail.setApiKey(process.env.SendGridApiKey);

const msg = {
Expand Down
93 changes: 92 additions & 1 deletion test/like.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import sinon from 'sinon';
import 'dotenv/config';
import { chai, server, expect } from './test-setup';
import likeCtrl from '../src/controllers/likes.controller';

let usertoken, adminToken, mikeToken;

Expand Down Expand Up @@ -290,4 +291,94 @@ describe('/likes and dislikes feature', () => {
done();
});
});
it('should throw a server error with unlike function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await likeCtrl.unlike(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with dislike function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await likeCtrl.dislike(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with clap function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await likeCtrl.clap(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with getDislikes function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await likeCtrl.getDislikes(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with getclaps function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await likeCtrl.getClaps(req, res);
expect(res.status).to.have.been.calledWith(500);
});
});
12 changes: 12 additions & 0 deletions test/pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ describe('/Article pagination', () => {
done();
});
});
it('Should throw an error when offset is greater than the count of articles ', (done) => {
chai
.request(server)
.get('/api/v1/articles?page=4&&limit=10')
.end((error, res) => {
expect(res.body).have.status(200);
expect(res.body).to.be.an('object');
expect(res.body).to.have.keys('message', 'status', 'data', 'allArticle');
expect(res.body.message).to.deep.equal('List of all articles');
done();
});
});
});
38 changes: 38 additions & 0 deletions test/rates.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sinon from 'sinon';
import { chai, server, expect } from './test-setup';
import rateCtrl from '../src/controllers/rating.controller';

let adminToken, usertwotoken, userToken;

Expand Down Expand Up @@ -252,4 +254,40 @@ describe('/POST rate article', () => {
done();
});
});
it('should throw a server error with createOrUpdateRate function', async () => {
const req = [
{ params: { articleSlug: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await rateCtrl.createOrUpdateRate(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with getArticleRating function', async () => {
const req = [
{ params: { articleSlug: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await rateCtrl.getArticleRating(req, res);
expect(res.status).to.have.been.calledWith(500);
});
});
74 changes: 74 additions & 0 deletions test/reporting.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sinon from 'sinon';
import { chai, server, expect } from './test-setup';
import reportCtrl from '../src/controllers/report.comtroller';

let usertoken, admintoken, usertwotoken;

Expand Down Expand Up @@ -301,4 +303,76 @@ describe('/Report an article', () => {
done();
});
});
it('should throw a server error with getMyReport function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await reportCtrl.getMyReport(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with getreportForArticle function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await reportCtrl.getReportsForArticle(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with deleteReport function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await reportCtrl.deleteReport(req, res);
expect(res.status).to.have.been.calledWith(500);
});
it('should throw a server error with reportArticle function', async () => {
const req = [
{ params: { Article: 'fakeslug' } },
{
auth: {
email: 'userthree@gmail.com'
}
}
];
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await reportCtrl.reportArticle(req, res);
expect(res.status).to.have.been.calledWith(500);
});
});
16 changes: 16 additions & 0 deletions test/share.article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ describe('test for sharing an article', () => {
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('Should throw an error when trying to share unexisting article', async () => {
const req = {
params: { slug: 'fakeslugdxxx', channel: 'twitter' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(404);
});
});
34 changes: 34 additions & 0 deletions test/test-follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import EmailHelper from '../src/helpers/verification-email';
import Helper from '../src/helpers/helper';

let usertoken;
let userThreeToken;
describe('test for following and unfollowing a user', () => {
usertoken = Helper.generateToken({
id: 3,
email: 'user@gmail.com',
username: 'username',
verified: true
});
userThreeToken = Helper.generateToken({
id: 5,
email: 'userthree@gmail.com',
username: 'userthree',
verified: true
});

it('test for following a user', async () => {
const req = {
Expand Down Expand Up @@ -107,4 +114,31 @@ describe('test for following and unfollowing a user', () => {
done();
});
});
it('test for userthree following usertwo', async () => {
const req = {
params: { userId: 3 },
auth: {
email: 'userthree@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await followController.follow(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for getting all my followers when there are there', (done) => {
chai.request(server)
.get('/api/v1/users/profiles/followers')
.send({})
.set('Authorization', usertoken)
.end((error, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
});
9 changes: 9 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect } from './test-setup';
import { sendEmail } from '../src/helpers/verification-email';

describe('Users', () => {
it('should return something when email is send', async () => {
const results = await sendEmail('admin@gmail.com', 'admin', 'http://localhost:3000/api/v1/users/verify');
expect(results[0]).to.be.an('object');
});
});
Loading

0 comments on commit 7c2864b

Please sign in to comment.