Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#161280586 user can get an authors article
 #44

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ const articlesController = {
});
}
});
}
},

};

export default articlesController;
38 changes: 38 additions & 0 deletions server/controllers/authorController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import GetAuthorsOfTheWeekHelpers from '../helpers/GetAuthorsOfTheWeekHelpers';
import models from '../models';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file extension for "../models" import/extensions

import helpers from '../helpers/helpers';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file extension for "../helpers/helpers" import/extensions


const { parsedId } = helpers;
const { Articles } = models;

const {
getArticlesAndLikesCountForTheWeek, getAuthors
Expand All @@ -19,3 +24,36 @@ const authorController = async (req, res) => {
};

export default authorController;

/**
* @method getArticlesByAuthorsId
* @description fetch all users's articles
* @param {*} req
* @param {*} res
* @returns {Object} the a list of articles that the user has created
*/
export const getArticlesByAuthorsId = (req, res) => {
const authorId = parsedId(req.params.authorId);
if (!(Number.isInteger(authorId))) {
return res.status(400).jsend.error({
message: 'Invalid user details'
});
}
Articles.findAll({
where: {
userId: authorId
}
})
.then(articles => ((articles.length > 0)
? res.status(200).jsend.success({
message: 'All articles',
articles
})
: res.status(200).jsend.success({
message: 'You do not have any article',
articles: []
})))
.catch(err => res.status(500).jsend.error({
message: err
}));
};
8 changes: 4 additions & 4 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ const userController = {
const {
firstname, lastname, username, email, bio,
} = req.body;
const interest = req.body.interest ? req.body.interest.split(',') : null;
const interests = req.body.interests ? req.body.interests.split(',') : null;
const updateUser = (image) => {
const updates = {
firstname,
Expand All @@ -334,7 +334,7 @@ const userController = {
email,
bio,
image,
interest
interests
};
if (!req.file) {
delete updates.image;
Expand Down Expand Up @@ -383,12 +383,12 @@ const userController = {
Users.findOne({ where: { id: userId } })
.then((user) => {
if (!user || user === undefined) {
res.status(404).jsend.fail({
return res.status(404).jsend.fail({
message: 'No user found'
});
}
delete user.password;
res.status(200).jsend.success({
return res.status(200).jsend.success({
message: 'User details',
user
});
Expand Down
2 changes: 1 addition & 1 deletion server/routes/articleRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
create,
like,
getArticles,
getFeaturedArticles
getFeaturedArticles,
} = articleController;
const { addComment, updateComment, updateReply } = commentController;
const {
Expand Down
6 changes: 5 additions & 1 deletion server/routes/authorRoutes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express';
import authorController from '../controllers/authorController';
import authorController, { getArticlesByAuthorsId } from '../controllers/authorController';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file extension for "../controllers/authorController" import/extensions

import auth from '../middleware/auth';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file extension for "../middleware/auth" import/extensions


const authorRoutes = express.Router();

// Get authors of the week
authorRoutes.get('/authors-of-the-week', authorController);

// getall authors article
authorRoutes.get('/articles/:authorId', auth, getArticlesByAuthorsId);

export default authorRoutes;
45 changes: 42 additions & 3 deletions test/authors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../server/app';
import GetAuthorsOfTheWeekHelpers from '../server/helpers/GetAuthorsOfTheWeekHelpers';
import generateToken from '../server/helpers/generateToken';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing file extension for "../server/helpers/generateToken" import/extensions


chai.use(chaiHttp);
const { assert, expect, should } = chai;
Expand All @@ -11,6 +12,8 @@ const {
getThisWeekSunday, getArticlesAndLikesCountForTheWeek, getAuthors
} = GetAuthorsOfTheWeekHelpers;

const verifiedToken = generateToken(7200, { id: 2, isVerified: true, roleId: 2 });

describe('Tests for Getting authors of the week', () => {
describe('Unit Tests for GetAuthorsOfTheWeekHelpers functions', () => {
it('getThisWeekSunday should return an integer greater than 1.5 trillion', () => {
Expand All @@ -20,14 +23,14 @@ describe('Tests for Getting authors of the week', () => {
const result = await getArticlesAndLikesCountForTheWeek();

assert.isArray(result);
expect(result).to.have.lengthOf(4);
expect(result).to.have.lengthOf(3);
});
it('getAuthors should return an array with length of 3', async () => {
const result = await getArticlesAndLikesCountForTheWeek();
const authors = getAuthors(result);

assert.isArray(authors);
expect(authors).to.have.lengthOf(3);
expect(authors).to.have.lengthOf(2);
});
});
describe('Integration Tests for authors of the week', () => {
Expand All @@ -38,9 +41,45 @@ describe('Tests for Getting authors of the week', () => {
.end((err, res) => {
res.body.status.should.equal('success');
res.body.data.authors.should.be.an('array');
res.body.data.authors.should.have.lengthOf(3);
res.body.data.authors.should.have.lengthOf(2);
done();
});
});
});
});

describe('Checks the validity of the user', () => {
it('should return authors articles', (done) => {
chai.request(app)
.get('/api/v1/authors/articles/1')
.set('Authorization', verifiedToken)
.end((error, res) => {
expect(res).to.have.status(200);
expect(res.body.data.message).to.equal('All articles');
assert.isObject(res.body, 'is an object containing the user details');
assert.isArray(res.body.data.articles);
done();
});
});
it('should show a not found message', (done) => {
chai.request(app)
.get('/api/v1/authors/articles/13333')
.set('Authorization', verifiedToken)
.end((message, res) => {
expect(res).to.have.status(200);
expect(res.body.data.message).to.equal('You do not have any article');
assert.isArray(res.body.data.articles);
done();
});
});
it('should not return a user if id is an alphabet', (done) => {
chai.request(app)
.get('/api/v1/authors/articles/abd')
.set('Authorization', verifiedToken)
.end((message, res) => {
expect(res).to.have.status(400);
expect(res.body.message).to.equal('Invalid user details');
done();
});
});
});