Skip to content

Commit

Permalink
Merge 5e2c4f9 into 0c7e7e3
Browse files Browse the repository at this point in the history
  • Loading branch information
Andhrah committed Mar 15, 2019
2 parents 0c7e7e3 + 5e2c4f9 commit d0720a8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
4 changes: 3 additions & 1 deletion controllers/articlesController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import models from '../models';
import Response from '../helpers/responseHelper';
import articleHelpers from '../helpers/articleHelpers';
import { STATUS } from '../helpers/constants';

/**
Expand All @@ -22,8 +23,9 @@ export default class ArticlesController {
const {
title, body, description,
} = req.body;
const readTime = articleHelpers.articleReadTime(req.body);
const content = {
title, body, description, slug, authorId,
title, body, description, slug, authorId, readTime
};
try {
const article = await models.Article.create(content);
Expand Down
3 changes: 3 additions & 0 deletions database/migrations/20190305135521-create-articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default {
description: {
type: Sequelize.STRING
},
readTime: {
type: Sequelize.STRING
},
/**
* @todo include tagId for articles tagList in tagList model
*/
Expand Down
19 changes: 19 additions & 0 deletions helpers/articleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import models from '../models';

const { Op } = models.Sequelize;

let readTime;

export default {
findArticleByAuthorId: async (authorId, title) => {
try {
Expand All @@ -24,4 +26,21 @@ export default {
return error;
}
},
articleReadTime: (articleBody) => {
try {
const defaultArticleLength = 250;
const articleLength = articleBody.body.split(' ').length;
if (articleLength <= defaultArticleLength) {
readTime = '1 minute read';
return readTime;
}
readTime = Math.round(articleLength / defaultArticleLength);
if (readTime === 1) {
return `${readTime} minute read`;
}
return `${readTime} minutes read`;
} catch (err) {
return err;
}
}
};
3 changes: 1 addition & 2 deletions middlewares/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import { STATUS } from '../helpers/constants';
function authenticate(request, response, next) {
// Get auth header value
const bearer = request.headers.authorization;
if (!bearer) return next(createError(STATUS.UNATHORIZED, 'You are unauthorized to access the requested resource. Please log in.'));
if (!bearer) return next(createError(401, 'You are unauthorized to access the requested resource. Please log in.'));

const token = bearer.split(' ')[1];

return jwt.verify(token, env('APP_KEY'), (err, decoded) => {
if (err || !decoded) return next(createError(STATUS.UNATHORIZED, 'Authentication failure: Invalid access token.'));

Expand Down
1 change: 1 addition & 0 deletions models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Article = (sequelize, DataTypes) => {
defaultValue: sequelize.NOW
},
description: DataTypes.STRING,
readTime: DataTypes.STRING,
updatedAt: {
allowNull: true,
type: DataTypes.DATE,
Expand Down
28 changes: 28 additions & 0 deletions test/unit/middlewares/articlesMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app from '../../../index';
import models from '../../../models';
import logger from '../../../helpers/logger';
import { STATUS } from '../../../helpers/constants';
import articleHelpers from '../../../helpers/articleHelpers';

logger.log('The test is running');
chai.use(chaiHttp);
Expand Down Expand Up @@ -262,4 +263,31 @@ describe('API endpoint: /api/articles (Middleware test)', () => {
});
});
});

describe('POST: /api/v1/articles', () => {
describe('Article read time', () => {
it('Should create an article with the minute(s) it will take to read it', (done) => {
const article = {
title: faker.random.words(),
description: 'dehjfjdh',
body: 'sentence I make all over again'
};
const readTime = articleHelpers.articleReadTime(article.body);
article.time = readTime;
chai
.request(app)
.post('/api/v1/articles')
.send(article)
.set({ Authorization: `Bearer ${dummyUser.token}` })
.end((err, res) => {
expect(res).to.have.status(CREATED);
expect(res.body).to.be.an('object');
expect(res.body).to.haveOwnProperty('code').to.equal(CREATED);
expect(res.body.data.slug).to.be.a('string');
expect(res.body).to.contain(readTime);
done();
});
});
});
});
});
2 changes: 1 addition & 1 deletion test/unit/middlewares/socialAuth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('social authentication', () => {
emails: [{ value: 'testing@test1.com' }]
},
};
it('should hit socail authentication API', async () => {
it('should hit social authentication API', async () => {
const facebookUserInfo = generateOrFindUser(
verifyCallback.accessToken,
verifyCallback.refreshToken,
Expand Down

0 comments on commit d0720a8

Please sign in to comment.