Skip to content

Commit

Permalink
BE #4 article update using PUT and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tito433 committed May 17, 2020
1 parent a6dd6bf commit 9bea2b7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 15 deletions.
74 changes: 73 additions & 1 deletion routes/article.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var debug = require('debug')
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var slugify = require('slugify')

Expand Down Expand Up @@ -34,7 +35,7 @@ var createUniqueUrl = (title, callBack, counter = 0) => {
router.route('/')
.get((req, res, next) => {
Article.find({})
.sort({'updatedAt':-1})
.sort({ 'updatedAt': -1 })
.limit(5)
.exec(function (err, articles) {
if (err) {
Expand Down Expand Up @@ -70,5 +71,76 @@ router.route('/')
})
});
})
.put((req, res, next) => {
res.statusCode = 403;
res.end('PUT operation not supported on /article');
});


/* with id */
router.route('/:articleId')
.get((req, res, next) => {
Article.findById(req.params.articleId)
//.populate('author')
.then((article) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(article);
}, (err) => next(err))
.catch((err) => next(err));
})
.post((req, res, next) => {
res.statusCode = 403;
res.end('POST operation not supported on /article/' + req.params.articleId);
})
.put((req, res, next) => {

if (mongoose.Types.ObjectId.isValid(req.params.articleId)) {

Article.findById(req.params.articleId)
//.populate('author')
.then((article) => {

if (null === article) {
var err = new Error('Article ' + req.params.articleId + ' not found');
err.status = 404;
return next(err);
}
else {
//update url as title is changed
if (article.title !== req.body.title) {
createUniqueUrl(req.body.title, (url) => {
Article.findByIdAndUpdate(req.params.articleId, {
$set: { "title": req.body.title, "url": url, "body": req.body.body }
}, { new: true })
.then((upudatedArt) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(upudatedArt);
}, (err) => next(err))
.catch((err) => next(err));
});
}
else {
Article.findByIdAndUpdate(req.params.articleId, {
$set: req.body
}, { new: true })
.then((upudatedArt) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(upudatedArt);
}, (err) => next(err))
.catch((err) => next(err));
}
}
}, (err) => next(err))
.catch((err) => next(err));
}
else{
var err = new Error('Article ' + req.params.articleId + ' not found');
err.status = 404;
return next(err);
}
});

module.exports = router;
83 changes: 69 additions & 14 deletions test/routes/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,76 @@ describe("Test GET /article", () => {


const article = {
title: "Test: how to test express API POST request?",
body: ["<h2>Article body sub heading</h2>","<p>Article body paragraph</p>"]
title: "Test: how to test express API POST request?",
body: ["<h2>Article body sub heading</h2>", "<p>Article body paragraph</p>"]
};

/* POST /article */
describe("Test POST /article", () => {
test("It should response 200 on POST method", done => {
request(app)
.post("/article")
.send(article)
.then(response => {
const respArticle=JSON.parse(response.text);
expect(response.statusCode).toBe(200);
expect(respArticle.title).toBe(article.title);
done();
});
});
});
test("It should response 200 on POST method", done => {
request(app)
.post("/article")
.send(article)
.then(response => {
const respArticle = JSON.parse(response.text);
expect(response.statusCode).toBe(200);
expect(respArticle.title).toBe(article.title);
done();
});
});
});

/* GET /article/:artId */
describe("Test POST /article/:articleId", () => {
test("It should response 200 on GET method", done => {
request(app)
.get("/article")
.then(response => {
expect(response.statusCode).toBe(200);
const articles = JSON.parse(response.text);
if (articles && article.length) {
request(app)
.get("/article/"+articles[0]._id)
.then(response => {
expect(response.statusCode).toBe(200);
done();
});
}
else {
done();
}
});
});
});

/* PUT /article/:artId */
describe("Test PUT /article/:articleId", () => {
test("It should response 200 on PUT method", done => {
request(app)
.get("/article")
.then(response => {
expect(response.statusCode).toBe(200);

const articles = JSON.parse(response.text);
if (articles && article.length) {
let art=articles[0],
newTitle='Test: Hello test world';

art.title=newTitle;

request(app)
.put("/article/"+art._id)
.send(art)
.then(resp => {
expect(resp.statusCode).toBe(200);
const respArticle = JSON.parse(resp.text);
expect(respArticle.title).toBe(newTitle);
done();
});
}
else {
done();
}
});
});
});

0 comments on commit 9bea2b7

Please # to comment.