Skip to content

Commit

Permalink
[feature #165412927] format return messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kagaramag committed May 17, 2019
1 parent fff2cd1 commit ff7c5ad
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 37 deletions.
9 changes: 8 additions & 1 deletion src/controllers/TagController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class TagsController {
static async update(req, res) {
const action = req.method === 'DELETE' ? 'delete' : 'update';
const response = await Tag.update(req.body.tagList, req.params.slug, action);
return res.status(action === 'update' ? status.CREATED : status.OK).send({

let statusCode = status.BAD_REQUEST;
if (action === 'update') {
statusCode = status.CREATED;
} else if (action === 'delete') {
statusCode = status.OK;
}
return res.status(statusCode).send({
response
});
}
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/validation/createTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Joi from 'joi';

export default (input) => {
const schema = Joi.object().keys({
tagList: Joi.array()
.min(1)
.max(5)
.items(
Joi.string()
.min(2)
.max(25)
.label('Tag element')
)
.required()
});

return Joi.validate(input, schema, { abortEarly: false });
};
3 changes: 2 additions & 1 deletion src/helpers/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import password from './password';
import createArticle from './createArticle';
import updateArticle from './updateArticle';
import newUser from './newUser';
import createTag from './createTag';

export {
isUser, email, password, createArticle, updateArticle, newUser
isUser, email, password, createArticle, updateArticle, createTag, newUser
};
4 changes: 2 additions & 2 deletions src/middlewares/validation/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class articles {
* @returns {object} Object representing the response returned
*/
static update(req, res, next) {
const result = validate.updateArticle(req.body);
return result.error ? Error.joiErrorHandler(res, result) : next();
const response = validate.updateArticle(req.body);
return !response.error ? next() : Error.joiErrorHandler(res, response);
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/middlewares/validation/validateTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi';
import Error from '../../helpers/errorHandler';
import * as validate from '../../helpers/validation';

/**
* Author: Gilles Kagarama
Expand All @@ -13,17 +13,8 @@ class tags {
* @returns {object} Object representing the response returned
*/
static create(req, res, next) {
const schema = Joi.object().keys({
tagList: Joi.array()
.min(1)
.max(5)
.required()
});
const result = Joi.validate(req.body, schema, { abortEarly: false });
if (!result.error) {
return next();
}
return Error.joiErrorHandler(res, result);
const result = validate.createTag(req.body);
return result.error ? Error.joiErrorHandler(res, result) : next();
}
}

Expand Down
25 changes: 11 additions & 14 deletions src/queries/articles/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ const { Op } = Sequelize;
* @param {object} condition condition for query
* @returns {object} Object representing the response returned
*/
export default async (condition = {}) => {
condition = { ...condition, status: { [Op.ne]: 'deleted' } };
return db.Article.findOne({
where: condition,
logging: false,
include: [
{
model: db.User,
as: 'author',
attributes: ['username', 'bio', 'image']
}
]
});
};
export default async (condition = {}) => db.Article.findOne({
where: { ...condition, status: { [Op.ne]: 'deleted' } },
logging: false,
include: [
{
model: db.User,
as: 'author',
attributes: ['username', 'bio', 'image']
}
]
});
2 changes: 1 addition & 1 deletion src/seeders/20190509090036-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
password: '123123',
username: 'johndoe',
role: 'normal',
permissions: '{"can-edit"}',
permissions: ['read'],
image: 'user.png',
bio: 'I can do magic easy',
createdAt: '2019-04-29T22:00:00',
Expand Down
26 changes: 21 additions & 5 deletions src/tests/controllers/TagController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ chai.should();
const article = Factory.article.build();
chai.use(chaiHttp);
const token = 'token-string';
let findArticle = '';
describe('TAGS', () => {
let response = '';
before(async () => {
Expand All @@ -22,6 +23,7 @@ describe('TAGS', () => {
article.slug = 'lorem-ipsum-foo-hasli234rhjav';
article.userId = findUser[0].dataValues.id;
response = await db.Article.create(article, { logging: false });
findArticle = await db.Article.findAll({ limit: 1, logging: false });
});
it('should create a tag', (done) => {
chai
Expand All @@ -36,16 +38,16 @@ describe('TAGS', () => {
done();
});
});
it('should not create tags if there are more than 5', (done) => {
it('should not create tags if they are more than 5', (done) => {
chai
.request(app)
.put(`/api/v1/articles/${response.dataValues.slug}/tags`)
.send({ tagList: ['Morning', 'Bonjour', 'Bonjourno', 'Dias'] })
.send({ tagList: ['Morning', 'Bonjour', 'Bonjourno', 'Dias', 'Hakuna Matata', 'Energy'] })
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.CREATED);
expect(res).to.have.status(status.BAD_REQUEST);
res.body.should.be.an('object');
res.body.response.should.equal('You can not create more than 5 tags per article');
res.body.errors[0].should.equal('tagList must contain less than or equal to 5 items');
done();
});
});
Expand Down Expand Up @@ -75,10 +77,11 @@ describe('TAGS', () => {
});
});
it('should not create tags if it is a string', (done) => {
const tagList = 'text';
chai
.request(app)
.put(`/api/v1/articles/${response.dataValues.slug}/tags`)
.send({ tagList: 'text' })
.send({ tagList })
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.BAD_REQUEST);
Expand All @@ -87,4 +90,17 @@ describe('TAGS', () => {
done();
});
});
it('should not create tags if tagList is not provided', (done) => {
chai
.request(app)
.put(`/api/v1/articles/${response.dataValues.slug}/tags`)
.send()
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.BAD_REQUEST);
res.body.should.be.an('object');
res.body.errors[0].should.equal('tagList is required');
done();
});
});
});
1 change: 1 addition & 0 deletions src/tests/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './users';
import './articles';
import './tags';
import './tokens';
14 changes: 13 additions & 1 deletion src/tests/queries/tags/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
import chai from 'chai';
// eslint-disable-next-line import/no-extraneous-dependencies
import chaiHttp from 'chai-http';
import { Tag } from '../../../queries';
import { Tag, Article } from '../../../queries';
import * as Factory from '../../../helpers/factory';
import db from '../../../models';

const article = Factory.article.build();
delete article.id;
const tagList = ['Morning', 'Bonjour', 'Bonjourno', 'Dias', 'Hakuna Matata'];
chai.use(chaiHttp);
describe('Tag query', () => {
let fetchArticle;
before(async () => {
fetchArticle = await db.Article.findAll({ limit: 1, logging: false });
return fetchArticle;
});
it('should update tag', async () => {
const action = 'update';
const findArticle = await db.Article.findAll({ limit: 1, logging: false });
const response = await Tag.update(article.tagList, findArticle[0].dataValues.slug, action);
response.should.be.an('string');
response.should.equal('Tag list has been updated');
});
it('should not create tags if they are more than 5', async () => {
const action = 'update';
const { slug } = fetchArticle[0].dataValues;
const updateTags = await Tag.update(tagList, slug, action);
updateTags.should.be.an('string');
});
});

0 comments on commit ff7c5ad

Please sign in to comment.