Skip to content

Commit 50a7f72

Browse files
Adam Koniarlholmquist
Adam Koniar
authored andcommitted
chore(test): Unification of error messages for better end to end testing.
1 parent 2711820 commit 50a7f72

File tree

5 files changed

+244
-57
lines changed

5 files changed

+244
-57
lines changed

Diff for: app.js

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ const db = require('./lib/db');
3030
const fruits = require('./lib/routes/fruits');
3131

3232
app.use(bodyParser.json());
33+
app.use(function (error, req, res, next) {
34+
if (req.body === '' || (error instanceof SyntaxError && error.type === 'entity.parse.failed')) {
35+
res.status(415);
36+
return res.send('Invalid payload!');
37+
} else {
38+
next();
39+
}
40+
});
3341
app.use(bodyParser.urlencoded({ extended: false }));
3442
app.use(express.static(path.join(__dirname, 'public')));
3543
// expose the license.html at http[s]://[host]:[port]/licences/licenses.html

Diff for: lib/routes/fruits.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const validations = require('../validations');
77
const fruits = require('../api/fruits');
88

99
router.get('/fruits/:id', (request, response) => {
10-
const {id} = request.params;
10+
const { id } = request.params;
1111
fruits.find(id).then(result => {
1212
if (result.rowCount === 0) {
1313
response.status(404);
@@ -27,8 +27,8 @@ router.get('/fruits', (request, response) => {
2727
});
2828
});
2929

30-
router.post('/fruits', validations.validateCreate, (request, response) => {
31-
const {name, stock} = request.body;
30+
router.post('/fruits', validations.validateCreateUpdateRequest, (request, response) => {
31+
const { name, stock } = request.body;
3232
return fruits.create(name, stock).then((result) => {
3333
response.status(201);
3434
return response.send(result.rows[0]);
@@ -38,10 +38,10 @@ router.post('/fruits', validations.validateCreate, (request, response) => {
3838
});
3939
});
4040

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

5656
router.delete('/fruits/:id', (request, response) => {
57-
const {id} = request.params;
57+
const { id } = request.params;
5858
fruits.remove(id).then((result) => {
5959
if (result.rowCount === 0) {
6060
response.status(404);

Diff for: lib/validations/index.js

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,31 @@
11
'use strict';
22

3-
function validateCreate (request, response, next) {
4-
// No need to check for no body, express will make body an empty object
5-
const {name, stock, id} = request.body;
6-
7-
if (!name) {
8-
response.status(400);
9-
return response.send('The name must not be null');
3+
function validateCreateUpdateRequest(request, response, next) {
4+
if(Object.keys(request.body).length === 0){
5+
response.status(415);
6+
return response.send('Invalid payload!');
107
}
11-
12-
if (!stock) {
13-
response.status(400);
14-
return response.send('The stock must not be greater or equal to 0');
15-
}
16-
17-
if (id) {
18-
response.status(400);
19-
return response.send('The created item already contains an id');
20-
}
21-
22-
next();
23-
}
24-
25-
function validateUpdate (request, response, next) {
268
// No need to check for no body, express will make body an empty object
27-
const {name, stock, id} = request.body;
9+
const { name, stock, id } = request.body;
2810

2911
if (!name) {
30-
response.status(400);
31-
return response.send('The name must not be null');
12+
response.status(422);
13+
return response.send('The name is required!');
3214
}
3315

34-
if (!stock) {
35-
response.status(400);
36-
return response.send('The stock must not be greater or equal to 0');
16+
if (stock == null || isNaN(stock) || stock < 0) {
17+
response.status(422);
18+
return response.send('The stock must be greater or equal to 0!');
3719
}
3820

3921
if (id && id !== request.params.id) {
40-
response.status(400);
41-
return response.send('The id cannot be changed');
22+
response.status(422);
23+
return response.send('Id was invalidly set on request.');
4224
}
4325

4426
next();
4527
}
4628

4729
module.exports = {
48-
validateCreate,
49-
validateUpdate
30+
validateCreateUpdateRequest
5031
};

Diff for: public/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
//In case of edit fruits, populate form with fruit data
8282
$scope.edit = function (fruit) {
8383
$scope.form.name = fruit.name;
84-
$scope.form.stock = fruit.stock;
84+
$scope.form.stock = parseInt(fruit.stock);
8585
$scope.form.id = fruit.id;
8686
};
8787

@@ -104,7 +104,7 @@
104104
}
105105

106106
function _error(response) {
107-
alert(response.data.error || response.statusText);
107+
alert(response.data || response.statusText);
108108
}
109109

110110
//Clear the form
@@ -131,7 +131,7 @@ <h3>Add/Edit a fruit</h3>
131131
<form ng-submit="update()">
132132
<div class="row">
133133
<div class="col-6"><input type="text" placeholder="Name" ng-model="form.name" size="60"/></div>
134-
<div class="col-6"><input type="number" placeholder="Stock" ng-model="form.stock" size="60"/></div>
134+
<div class="col-6"><input type="number" placeholder="Stock" ng-model="form.stock" size="60" min="0"/></div>
135135
</div>
136136
<input type="submit" value="Save"/>
137137
</form>

0 commit comments

Comments
 (0)