Skip to content

Commit

Permalink
[feature #165412917] Refactor validation to remove similar codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Kagarama authored and Gilles Kagarama committed May 3, 2019
1 parent 8727aa1 commit 8d357be
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/middlewares/validations/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class articles {
*/
static create(req, res, next) {
const result = validate.createArticle(req.body);
if (!result.error) {
return next();
if (result.error) {
return Error.joiErrorHandler(res, result);
}
Error.joiErrorHandler(res, result);
return next();
}

/**
Expand All @@ -29,10 +29,7 @@ class articles {
*/
static update(req, res, next) {
const result = validate.updateArticle(req.body);
if (!result.error) {
return next();
}
Error.joiErrorHandler(res, result);
return result.error ? Error.joiErrorHandler(res, result) : next();
}

/**
Expand Down
76 changes: 70 additions & 6 deletions src/tests/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('Article', () => {
});
});
});
describe('Get all article', () => {
describe('Get articles', () => {
it('Should get all articles', async () => {
const token = 'token-string';
chai
Expand All @@ -193,6 +193,24 @@ describe('Article', () => {
response.articles[0].coverUrl.should.be.a('string');
});
});
it('Should get one article', async () => {
const token = 'token-string';
chai
.request(app)
.get('/api/articles/going-home-like-a-dog-2aijv735e7w')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
const response = res.body;
expect(res).to.have.status(status.OK);
response.article.should.be.a('object');
response.article.id.should.be.a('number');
response.article.title.should.be.a('string');
response.article.body.should.be.a('string');
response.article.description.should.be.a('string');
response.article.slug.should.be.a('string');
response.article.coverUrl.should.be.a('string');
});
});
});
describe('Update article', () => {
it('Should update the article', async () => {
Expand All @@ -216,8 +234,6 @@ describe('Article', () => {
expect(res).to.have.status(status.CREATED);
});
});
});
describe('Publish article', () => {
it('Should publish article', async () => {
const token = 'token-string';
chai
Expand All @@ -230,6 +246,56 @@ describe('Article', () => {
});
});
});
describe('Publish article', () => {
it('Should not publish a non existing article', async () => {
const token = 'token-string';
chai
.request(app)
.put('/api/articles/invalid-1632jv5quc9c/publish')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.NOT_FOUND);
// response.should.be.a('objenpm ct');
});
});
it('Should publish an article', async () => {
const token = 'token-string';
chai
.request(app)
.put('/api/articles/going-home-like-a-dog-2aijv735e7w/publish')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.OK);
res.body.should.be.a('object');
res.body.message.should.equal('Article published successfully');
});
});
});
describe('Unpublish article', () => {
it('Should not unpublish a non existing article', async () => {
const token = 'token-string';
chai
.request(app)
.put('/api/articles/invalid-1632jv5quc9c/unpublish')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.NOT_FOUND);
res.body.should.be.a('object');
});
});
it('Should unpublish an article', async () => {
const token = 'token-string';
chai
.request(app)
.put('/api/articles/going-home-like-a-dog-2aijv735e7w/unpublish')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
expect(res).to.have.status(status.OK);
res.body.should.be.a('object');
res.body.message.should.equal('Article unpublished successfully');
});
});
});
describe('Delete article', () => {
it('Should not delete a non existing article', async () => {
const token = 'token-string';
Expand All @@ -252,10 +318,8 @@ describe('Article', () => {
// const response = res.body;
expect(res).to.have.status(status.OK);
res.body.should.be.a('object');
res.body.message.should.equal('Article deleted successfully');
});
});
});
// after(async () => {
// db.Article.destroy({ truncate: true, cascade: true, logging: false });
// });
});

0 comments on commit 8d357be

Please sign in to comment.