Skip to content

Commit

Permalink
Merge 237a115 into d1d5020
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-42 committed Jul 26, 2019
2 parents d1d5020 + 237a115 commit 9ed5345
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
8 changes: 5 additions & 3 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export default {
try {
const {
user, body: {
description, body, title, tags, readTime, categoryId
description, body, title, tags,
readTime, categoryId, publish
}
} = req;

const categoryExist = await db.Category.findOne({
where: {
id: categoryId
Expand All @@ -94,7 +94,8 @@ export default {
body,
title,
readTime,
categoryId
categoryId,
publish
}
);

Expand Down Expand Up @@ -182,6 +183,7 @@ export default {
const article = await foundArticle.update({
description: data.description || foundArticle.description,
body: data.body || foundArticle.body,
publish: data.publish || foundArticle.publish,
title: data.title || foundArticle.title,
image: data.image || foundArticle.image,
readTime: data.readTime || foundArticle.readTime,
Expand Down
8 changes: 7 additions & 1 deletion db/migrations/20190717104034-create-alter-articles-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Articles', 'reads', {
type: Sequelize.INTEGER,
defaultValue: 0,
}).then(() => {
queryInterface.addColumn('Articles', 'publish', {
type: Sequelize.BOOLEAN
});
}),
down: queryInterface => queryInterface.removeColumn('Articles', 'reads')
down: queryInterface => queryInterface.removeColumn('Articles', 'reads').then(() => {
queryInterface.removeColumn('Articles', 'publish');
})
};
1 change: 1 addition & 0 deletions db/models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = (sequelize, DataTypes) => {
rating: DataTypes.FLOAT,
readTime: DataTypes.STRING,
flagged: DataTypes.BOOLEAN,
publish: DataTypes.BOOLEAN,
categoryId: DataTypes.INTEGER,
},
{}
Expand Down
5 changes: 5 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,10 @@ definitions:
type: string
image:
type: string
publish:
type: boolean
categoryId:
type: Integer
xml:
name: NewUser
NewArticle:
Expand All @@ -810,6 +814,7 @@ definitions:
- body
- images
- categoryId
- publish
properties:
title:
type: string
Expand Down
33 changes: 32 additions & 1 deletion tests/routes/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('ARTICLES TEST', () => {
title: 'React course by hamza',
description: 'very good book',
body: 'learning react is good for your career...',
publish: true,
categoryId: category.id
};
register = {
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('ARTICLES TEST', () => {
.field('body', 'learning react is good for your career...')
.field('tags', 'Javascript')
.field('categoryId', category.id)
.field('publish', true)
.attach('image', `${__dirname}/test.jpg`)
.set('x-access-token', token);
expect(res.statusCode).to.equal(201);
Expand Down Expand Up @@ -105,6 +107,7 @@ describe('ARTICLES TEST', () => {
.field('description', 'very good book')
.field('body', 'learning react is good for your career...')
.field('tags', 'Javascript')
.field('publish', true)
.field('categoryId', 345678)
.attach('image', `${__dirname}/test.jpg`)
.set('x-access-token', token);
Expand Down Expand Up @@ -165,6 +168,7 @@ describe('ARTICLES TEST', () => {
.field('description', 'very good book')
.field('body', 'learning react is good for your career...')
.field('categoryId', category.id)
.field('publish', true)
.attach('image', `${__dirname}/test.jpg`)
.set('x-access-token', token);
expect(res.statusCode).to.equal(201);
Expand Down Expand Up @@ -220,7 +224,7 @@ describe('ARTICLES TEST', () => {
expect(res.body.article.description).to.include(article.description);
expect(res.body.article.body).to.include(article.body);
});
it('should not update an article does not exist', async () => {
it('should not update an article that does not exist', async () => {
const newUser = await createUser(register);
const userResponse = newUser.response();
const { token } = userResponse;
Expand All @@ -235,6 +239,22 @@ describe('ARTICLES TEST', () => {
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.include('Article not found');
});
it('should not update an article if info is incorrect', async () => {
const newUser = await createUser(register);
const userResponse = newUser.response();
const { token } = userResponse;
const res = await chai
.request(app)
.put('/api/v1/articles/9')
.set('x-access-token', token)
.send({ title: 980 });
expect(res.statusCode).to.equal(400);
expect(res.body).to.be.an('object');
expect(res.body.message).to.be.an('array');
expect(res.body.message[0]).to.include.all.keys('message');
expect(res.body.message[0].message).to.be.a('string');
expect(res.body.message[0].message).to.include('title must be a string');
});
it('Only author of an article should be able to edit an article', async () => {
const newUser = await createUser(register);
register.email = 'john@andela.com';
Expand Down Expand Up @@ -388,6 +408,17 @@ describe('ARTICLES TEST', () => {
expect(res.body.articles.length).to.equal(0);
expect(res.body.articlesCount).to.equal(0);
});
it('should not get any articles if none exist', async () => {
const res = await chai
.request(app)
.get('/api/v1/articles?limit=true');
expect(res.statusCode).to.equal(400);
expect(res.body).to.be.an('object');
expect(res.body.message).to.be.an('array');
expect(res.body.message[0]).to.include.all.keys('message');
expect(res.body.message[0].message).to.be.a('string');
expect(res.body.message[0].message).to.include('limit must be an integer');
});
});

describe('Get Single article', () => {
Expand Down
1 change: 1 addition & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const sanitizeRules = {
username: 'trim',
email: 'trim',
password: 'trim',
publish: 'to_boolean'
};

validations.unique = async (data, field, message, args, get) => {
Expand Down
3 changes: 3 additions & 0 deletions validators/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default {
body: 'required|string',
image: 'object',
title: 'required|string',
categoryId: 'required|integer',
publish: 'required|boolean'
};

const data = { ...req.body, ...req.files };
Expand All @@ -27,6 +29,7 @@ export default {
body: 'string',
image: 'object',
title: 'string',
publish: 'boolean',
slug: 'string|required'
};

Expand Down

0 comments on commit 9ed5345

Please sign in to comment.