Skip to content

Commit

Permalink
Merge 82142a8 into f35b173
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalyesubula committed Aug 29, 2019
2 parents f35b173 + 82142a8 commit 7e9c7a7
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 71 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"method-override": "^2.3.10",
"multer": "^1.4.2",
"nyc": "^14.1.1",
"open": "^6.4.0",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth": "^2.0.0",
Expand Down
42 changes: 42 additions & 0 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import articleService from '../services/article.service';
import Helper from '../helpers/helper';
import NotificationServices from '../services/notification.service';
import cloudinaryHelper from '../helpers/cloudinaryHelper';
import OpenUrlHelper from '../helpers/share.article.helper';
import Util from '../helpers/util';


const { notifyViaEmailAndPush } = NotificationServices;
const util = new Util();

const db = models.Article;
/**
Expand Down Expand Up @@ -195,6 +199,44 @@ class Articles {
updatedArticle
});
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
static async shareArticle(req, res) {
const article = await db.findOne({
where: { slug: req.params.slug }
});

if (!article) {
util.setError(404, 'Article is not found.');
return util.send(res);
}
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/articles/${req.params.slug}`;
switch (req.params.channel) {
case 'facebook':
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
case 'twitter':
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
case 'mail':
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
default:
break;
}
}
}

export default Articles;
7 changes: 7 additions & 0 deletions src/helpers/share.article.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import open from 'open';

const openUrl = url => open(url);

const OpenUrlHelper = { openUrl };

export default OpenUrlHelper;
1 change: 1 addition & 0 deletions src/routes/api/article/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ router.get('/', checkQuery, articleController.getAllArticles);
router.get('/:slug', articleController.getOneArticle);
router.delete('/:slug', [auth, confirmEmailAuth], articleController.deleteArticle);
router.patch('/:slug', [auth, confirmEmailAuth], imageUpload.array('images', 10), articleController.UpdateArticle);
router.post('/:slug/share/:channel', [auth, confirmEmailAuth], articleController.shareArticle);

router.post('/:articleId/tags', [auth, confirmEmailAuth], checkArticle, tagLimit, tagLength, createArticleTag);
router.get('/:articleId/tags', checkArticle, getArticleTags);
Expand Down
26 changes: 26 additions & 0 deletions src/routes/api/article/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'
/articles/{slug}/share/{channel}:
post:
description: >
Share an article on mail and social media channels
parameters:
- name: slug
in: path
description: Slug for article
type: string
required: true
- name: channel
in: path
description: Channel to share to
type: string
required: true
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Article successfully shared
/articles:
post:
summary: Create an article
Expand Down Expand Up @@ -438,3 +462,5 @@ definitions:
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'


4 changes: 2 additions & 2 deletions src/services/notification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NotificationServices {
.filter(eachUser => eachUser.inAppNotification === true);
const message = `${followedUser.username} just published an article`;
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;


if (myFollowersWithEmailSub.length !== 0) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class NotificationServices {
if (usersWhoFavoritedWithEmailSub.length !== 0) {
const emailAddresses = usersWhoFavoritedWithEmailSub.map(each => each.email);
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;
const emailTemplate = newCommentOnFavoritedArticlesTemplate(req.auth.username, url);
await sendEmail(emailAddresses, 'Hi there', emailTemplate);
}
Expand Down
96 changes: 48 additions & 48 deletions test/search.test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
// import { chai, server, expect } from './test-setup';
// import Helper from '../src/helpers/helper';
import { chai, server, expect } from './test-setup';
import Helper from '../src/helpers/helper';

// const usertoken = Helper.generateToken({
// id: 2,
// email: 'user@gmail.com',
// username: 'user',
// verified: false
// });
const usertoken = Helper.generateToken({
id: 2,
email: 'user@gmail.com',
username: 'user',
verified: false
});

// describe('search article query builder', () => {
// it('List of all articles', (done) => {
// chai
// .request(server)
// .get('/api/v1/articles')
// .set('Content-Type', 'application/json')
// .set('Authorization', usertoken)
// .end((err, res) => {
// expect(res.status).to.be.deep.equal(200);
// expect(res.body).to.have.deep.property('message', 'List of all articles');
// });
// done();
// });
// it('Title and description in the query', (done) => {
// chai
// .request(server)
// .get('/api/v1/articles?title=Title&&keyword=here')
// .set('Content-Type', 'application/json')
// .set('Authorization', usertoken)
// .end((err, res) => {
// if (err) done(err);
// expect(res.status).to.be.deep.equal(200);
// expect(res.body).to.have.deep.property('message', 'List of all articles');
// });
// done();
// });
// it('if invalid query key', (done) => {
// chai
// .request(server)
// .get('/api/v1/articles?key=Tilltle')
// .set('Content-Type', 'application/json')
// .set('Authorization', usertoken)
// .end((err, res) => {
// if (err) done(err);
// expect(res.status).to.be.deep.equal(400);
// expect(res.body).to.have.deep.property('error');
// });
// done();
// });
// });
describe('search article query builder', () => {
it('List of all articles', (done) => {
chai
.request(server)
.get('/api/v1/articles')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'List of all articles');
done();
});
});
it('Title and description in the query', (done) => {
chai
.request(server)
.get('/api/v1/articles?title=Title&&keyword=here')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
if (err) done(err);
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'List of all articles');
done();
});
});
it('if invalid query key', (done) => {
chai
.request(server)
.get('/api/v1/articles?key=Tilltle')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
if (err) done(err);
expect(res.status).to.be.deep.equal(400);
expect(res.body).to.have.deep.property('error');
done();
});
});
});
56 changes: 56 additions & 0 deletions test/share.article.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sinon from 'sinon';
import { expect } from './test-setup';
import Article from '../src/controllers/articles.controller';
import OpenUrlHelper from '../src/helpers/share.article.helper';

describe('test for sharing an article', () => {
it('test for sharing an article via facebook', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'facebook' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
sinon.stub(OpenUrlHelper, 'openUrl').returns(true);
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for sharing an article via twitter', async () => {
const req = {
params: { slug: 'fakeslug', 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(200);
});
it('test for sharing an article via mail', async () => {
const req = {
params: { slug: 'fakeslug', 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(200);
});
});
42 changes: 21 additions & 21 deletions test/test.articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('Articles', () => {
.end((err, res) => {
expect(res.status).to.be.deep.equal(400);
expect(res.body).to.have.deep.property('status');
done();
});
done();
});
it('create an article', async () => {
const req = {
Expand All @@ -49,24 +49,24 @@ describe('Articles', () => {
await articleController.createArticles(req, res);
expect(res.status).to.have.been.calledWith(201);
});
// it('List of all articles', (done) => {
// chai
// .request(server)
// .get('/api/v1/articles')
// .end((err, res) => {
// expect(res.status).to.be.deep.equal(200);
// expect(res.body).to.have.deep.property('message', 'List of all articles');
// });
// done();
// });
// it('view single article', (done) => {
// chai
// .request(server)
// .get('/api/v1/articles/fakeslug')
// .end((err, res) => {
// expect(res.status).to.be.deep.equal(200);
// expect(res.body).to.have.deep.property('message', 'Article successfully retrieved');
// });
// done();
// });
it('List of all articles', (done) => {
chai
.request(server)
.get('/api/v1/articles')
.end((err, res) => {
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'List of all articles');
done();
});
});
it('view single article', (done) => {
chai
.request(server)
.get('/api/v1/articles/fakeslug')
.end((err, res) => {
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'Article successfully retrieved');
done();
});
});
});

0 comments on commit 7e9c7a7

Please sign in to comment.