Skip to content

Commit

Permalink
BE #4 delete method & test added
Browse files Browse the repository at this point in the history
  • Loading branch information
tito433 committed May 17, 2020
1 parent 35f3e50 commit 39526f7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var articleRouter = require('./routes/article');
var connect = mongoose.connect(process.env.DB_URL,{
autoIndex: false,
useNewUrlParser: true,
useUnifiedTopology: true
useUnifiedTopology: true,
useFindAndModify: false
});

connect.then((db) => {
Expand Down
22 changes: 21 additions & 1 deletion routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ router.route('/')
.put((req, res, next) => {
res.statusCode = 403;
res.end('PUT operation not supported on /article');
})
.delete((req, res, next) => {
res.statusCode = 403;
res.end('DELETE operation not supported on /article');
});


Expand Down Expand Up @@ -136,7 +140,23 @@ router.route('/:articleId')
}, (err) => next(err))
.catch((err) => next(err));
}
else{
else {
var err = new Error('Article ' + req.params.articleId + ' not found');
err.status = 404;
return next(err);
}
})
.delete((req, res, next) => {
if (mongoose.Types.ObjectId.isValid(req.params.articleId)) {
Article.findByIdAndRemove(req.params.articleId)
.then((resp) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(resp);
}, (err) => next(err))
.catch((err) => next(err));
}
else {
var err = new Error('Article ' + req.params.articleId + ' not found');
err.status = 404;
return next(err);
Expand Down
29 changes: 27 additions & 2 deletions test/routes/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("Test POST /article/:articleId", () => {
.then(response => {
expect(response.statusCode).toBe(200);
const articles = JSON.parse(response.text);
if (articles && article.length) {
if (articles && articles.length) {
request(app)
.get("/article/"+articles[0]._id)
.then(response => {
Expand All @@ -65,7 +65,7 @@ describe("Test PUT /article/:articleId", () => {
expect(response.statusCode).toBe(200);

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

Expand All @@ -86,4 +86,29 @@ describe("Test PUT /article/:articleId", () => {
}
});
});
});

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

const articles = JSON.parse(response.text);
if (articles && articles.length) {
let artId=articles[0]._id;
request(app)
.delete("/article/"+artId)
.then(resp => {
expect(resp.statusCode).toBe(200);
done();
});
}
else {
done();
}
});
});
});

0 comments on commit 39526f7

Please sign in to comment.