Skip to content

Commit

Permalink
add error messages, remove TODO comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkvon committed Mar 27, 2018
1 parent acf84fb commit 374ba1c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ app.use('/messages', require('./routes/messages'));
app.use('/account', require('./routes/account'));
app.use('/contacts', require('./routes/contacts'));
app.use('/ideas', require('./routes/ideas'));
// vote for ideas, ... TODO to be generalized for not only ideas
// vote for ideas, ...
app.use('/ideas', require('./routes/votes'));
app.use('/comments', require('./routes/votes'));

Expand Down
26 changes: 22 additions & 4 deletions controllers/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function post(req, res, next) {

// what is the type of the object we vote for (i.e. ideas, comments, ...)
const primarys = req.baseUrl.substring(1);
const primary = primarys.slice(0, -1);

try {
// save the vote to database
Expand All @@ -26,11 +27,22 @@ async function post(req, res, next) {
switch (e.code) {
// duplicate vote
case 409: {
return res.status(409).end();
return res.status(409).json({
errors: [{
status: 409,
detail: 'duplicate vote'
}]
});
}
// missing idea
case 404: {
return res.status(404).end();
return res.status(404).json({
errors: [{
status: 404,
detail: `${primary} doesn't exist`
}]
});

}
default: {
return next(e);
Expand All @@ -50,6 +62,7 @@ async function del(req, res, next) {

// what is the type of the object we vote for (i.e. ideas, comments, ...)
const primarys = req.baseUrl.substring(1);
const primary = primarys.slice(0, -1);

try {
// remove the vote from database
Expand All @@ -59,9 +72,14 @@ async function del(req, res, next) {
} catch (e) {
// handle errors
switch (e.code) {
// missing idea
// primary object or vote doesn't exist
case 404: {
return res.status(404).end();
return res.status(404).json({
errors: [{
status: 404,
detail: `vote or ${primary} doesn't exist`
}]
});
}
default: {
return next(e);
Expand Down
37 changes: 33 additions & 4 deletions test/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,31 @@ function voteTestFactory(primary, only=false) {

voteBody.data.attributes.value = 1;

await agent
const response = await agent
.post(`/${primarys}/${existentPrimary.id}/votes`)
.send(voteBody)
.expect(409);

should(response.body).deepEqual({
errors: [{
status: 409,
detail: 'duplicate vote'
}]
});
});

it(`[${primary} doesn't exist] 404`, async () => {
await agent
const response = await agent
.post(`/${primarys}/1111111/votes`)
.send(voteBody)
.expect(404);

should(response.body).deepEqual({
errors: [{
status: 404,
detail: `${primary} doesn't exist`
}]
});
});
});

Expand Down Expand Up @@ -225,15 +239,30 @@ function voteTestFactory(primary, only=false) {
});

it('[vote doesn\'t exist] 404', async () => {
await agent
const response = await agent
.delete(`/${primarys}/${primary1.id}/votes/vote`)
.expect(404);

should(response.body).deepEqual({
errors: [{
status: 404,
detail: `vote or ${primary} doesn't exist`
}]
});

});

it(`[${primary} doesn't exist] 404`, async () => {
await agent
const response = await agent
.delete(`/${primarys}/111111/votes/vote`)
.expect(404);

should(response.body).deepEqual({
errors: [{
status: 404,
detail: `vote or ${primary} doesn't exist`
}]
});
});
});

Expand Down

0 comments on commit 374ba1c

Please sign in to comment.