-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,7 +246,8 @@ const articlesController = { | |
}); | ||
} | ||
}); | ||
} | ||
}, | ||
|
||
}; | ||
|
||
export default articlesController; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import GetAuthorsOfTheWeekHelpers from '../helpers/GetAuthorsOfTheWeekHelpers'; | ||
import models from '../models'; | ||
import helpers from '../helpers/helpers'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
})); | ||
}; |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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', () => { | ||
|
@@ -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', () => { | ||
|
@@ -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(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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