Skip to content

Commit

Permalink
chore(test): Unification of error messages for better end to end test…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
Adam Koniar authored and lholmquist committed Mar 27, 2018
1 parent 2711820 commit 50a7f72
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 57 deletions.
8 changes: 8 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const db = require('./lib/db');
const fruits = require('./lib/routes/fruits');

app.use(bodyParser.json());
app.use(function (error, req, res, next) {
if (req.body === '' || (error instanceof SyntaxError && error.type === 'entity.parse.failed')) {
res.status(415);
return res.send('Invalid payload!');
} else {
next();
}
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
// expose the license.html at http[s]://[host]:[port]/licences/licenses.html
Expand Down
16 changes: 8 additions & 8 deletions lib/routes/fruits.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const validations = require('../validations');
const fruits = require('../api/fruits');

router.get('/fruits/:id', (request, response) => {
const {id} = request.params;
const { id } = request.params;
fruits.find(id).then(result => {
if (result.rowCount === 0) {
response.status(404);
Expand All @@ -27,8 +27,8 @@ router.get('/fruits', (request, response) => {
});
});

router.post('/fruits', validations.validateCreate, (request, response) => {
const {name, stock} = request.body;
router.post('/fruits', validations.validateCreateUpdateRequest, (request, response) => {
const { name, stock } = request.body;
return fruits.create(name, stock).then((result) => {
response.status(201);
return response.send(result.rows[0]);
Expand All @@ -38,10 +38,10 @@ router.post('/fruits', validations.validateCreate, (request, response) => {
});
});

router.put('/fruits/:id', validations.validateUpdate, (request, response) => {
const {name, stock} = request.body;
const {id} = request.params;
fruits.update({name, stock, id}).then((result) => {
router.put('/fruits/:id', validations.validateCreateUpdateRequest, (request, response) => {
const { name, stock } = request.body;
const { id } = request.params;
fruits.update({ name, stock, id }).then((result) => {
if (result.rowCount === 0) {
response.status(404);
return response.send(`Unknown item ${id}`);
Expand All @@ -54,7 +54,7 @@ router.put('/fruits/:id', validations.validateUpdate, (request, response) => {
});

router.delete('/fruits/:id', (request, response) => {
const {id} = request.params;
const { id } = request.params;
fruits.remove(id).then((result) => {
if (result.rowCount === 0) {
response.status(404);
Expand Down
45 changes: 13 additions & 32 deletions lib/validations/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
'use strict';

function validateCreate (request, response, next) {
// No need to check for no body, express will make body an empty object
const {name, stock, id} = request.body;

if (!name) {
response.status(400);
return response.send('The name must not be null');
function validateCreateUpdateRequest(request, response, next) {
if(Object.keys(request.body).length === 0){
response.status(415);
return response.send('Invalid payload!');
}

if (!stock) {
response.status(400);
return response.send('The stock must not be greater or equal to 0');
}

if (id) {
response.status(400);
return response.send('The created item already contains an id');
}

next();
}

function validateUpdate (request, response, next) {
// No need to check for no body, express will make body an empty object
const {name, stock, id} = request.body;
const { name, stock, id } = request.body;

if (!name) {
response.status(400);
return response.send('The name must not be null');
response.status(422);
return response.send('The name is required!');
}

if (!stock) {
response.status(400);
return response.send('The stock must not be greater or equal to 0');
if (stock == null || isNaN(stock) || stock < 0) {
response.status(422);
return response.send('The stock must be greater or equal to 0!');
}

if (id && id !== request.params.id) {
response.status(400);
return response.send('The id cannot be changed');
response.status(422);
return response.send('Id was invalidly set on request.');
}

next();
}

module.exports = {
validateCreate,
validateUpdate
validateCreateUpdateRequest
};
6 changes: 3 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
//In case of edit fruits, populate form with fruit data
$scope.edit = function (fruit) {
$scope.form.name = fruit.name;
$scope.form.stock = fruit.stock;
$scope.form.stock = parseInt(fruit.stock);
$scope.form.id = fruit.id;
};

Expand All @@ -104,7 +104,7 @@
}

function _error(response) {
alert(response.data.error || response.statusText);
alert(response.data || response.statusText);
}

//Clear the form
Expand All @@ -131,7 +131,7 @@ <h3>Add/Edit a fruit</h3>
<form ng-submit="update()">
<div class="row">
<div class="col-6"><input type="text" placeholder="Name" ng-model="form.name" size="60"/></div>
<div class="col-6"><input type="number" placeholder="Stock" ng-model="form.stock" size="60"/></div>
<div class="col-6"><input type="number" placeholder="Stock" ng-model="form.stock" size="60" min="0"/></div>
</div>
<input type="submit" value="Save"/>
</form>
Expand Down
Loading

0 comments on commit 50a7f72

Please sign in to comment.