Skip to content

Commit

Permalink
Merge pull request #16 from ebenezerdon/ch-add-validation-161455757
Browse files Browse the repository at this point in the history
#[161455757] Add input validation
  • Loading branch information
ebenezerdon committed Oct 26, 2018
2 parents 82f4821 + 06a95d7 commit 22e8920
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
51 changes: 51 additions & 0 deletions server/middleware/validateinput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const validateUserInput = (req, res, next) => {
const { body } = req;
/* if (null(body)) {
return (
res.status(400).json('Hi! You\'d have to send something')
);
} */
if (!String(body.fullName)) {
return (
res.status(400).json('The user\'s name has to be a string!')
);
}
if (!String(body.emailAddress)) {
return (
res.status(400).json('The user\'s email adress has to be a string!')
);
}
if (body.password.length < 6) {
return (
res.status(400).json('The user\'s password has to be more than 6 characters!')
);
}
if (!body.type) {
return (
res.status(400).json('There really has to be a type!')
);
}
return next();
};

const validateProductInput = (req, res, next) => {
const { body } = req;
if (!body.name || !body.description || !body.price || !body.quantity) {
return (
res.status(400).json('Hi! Some details are missing. Can you check and try again?')
);
}
return next();
};

const validateSaleInput = (req, res, next) => {
const { body } = req;
if (!body.productName || !body.price || !body.quantity) {
return (
res.status(400).json('Hi! Some details are missing. Can you check and try again?')
);
}
return next();
};

export { validateUserInput, validateProductInput, validateSaleInput };
19 changes: 11 additions & 8 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import express from 'express';
import {
getAllSales, getOneSale, addSale,
} from '../controllers/salesController';
import {
getAllProducts, getOneProduct, addProduct,
} from '../controllers/productsController';
import {
getAllSales, getOneSale, addSale,
} from '../controllers/salesController';
import {
getAllUsers, getOneUser, addUser, loginUser,
} from '../controllers/usersController';
import { authenticate, verifyAdmin, verifyAttendant } from '../middleware/verify';
import {
validateUserInput, validateProductInput, validateSaleInput,
} from '../middleware/validateinput';

const router = express.Router();

/* GET home page. */
/* router.get('/', (req, res, next) => {
res.send('index.html');
}); */
}); */

/* Products Router */
router.get('/products', authenticate, getAllProducts);
router.get('/products/:id', authenticate, getOneProduct);
router.post('/products', authenticate, verifyAdmin, addProduct);
router.post('/products', authenticate, verifyAdmin, validateProductInput, addProduct);

/* Sales Router */
router.get('/sales', authenticate, verifyAdmin, getAllSales);
router.get('/sales/:id', authenticate, getOneSale);
router.post('/sales', authenticate, verifyAttendant, addSale);
router.get('/sales/:id', authenticate, verifyAdmin, getOneSale);
router.post('/sales', authenticate, verifyAttendant, validateSaleInput, addSale);

/* Users Router */
router.get('/users', authenticate, verifyAdmin, getAllUsers);
router.get('/users/:id', authenticate, verifyAdmin, getOneUser);
router.post('/users', authenticate, verifyAdmin, addUser);
router.post('/users', authenticate, verifyAdmin, validateUserInput, addUser);
router.post('/login', loginUser);

export default router;
8 changes: 4 additions & 4 deletions server/tests/sales.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('Get A sale record', () => {
it('it should have a status 404', (done) => {
chai.request(app).post('/api/v1/login')
.send({
emailAdress: 'joshodogwu@gmail.com',
password: 'realsecret',
type: 'attendant',
emailAdress: 'sarahbeth@gmail.com',
password: 'supersecretstuff',
type: 'admin',
})
.end((err, res) => {
const { token } = res.body;
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('Create New sale', () => {
const { token } = res.body;
chai.request(app).post('/api/v1/sales')
.send({
name: 'Ankara',
productName: 'Ankara',
description: 'Akara for everybody',
quantity: '4',
price: '₦5500',
Expand Down
6 changes: 3 additions & 3 deletions server/tests/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Create New user', () => {
chai.request(app).post('/api/v1/users')
.send({
fullName: 'Winifred Briggs',
emailAdress: 'winibrigs@gmail.com',
emailAddress: 'winibrigs@gmail.com',
password: 'anothersecretstuff',
type: 'attendant',
})
Expand All @@ -109,7 +109,7 @@ describe('Create New user', () => {
});
});

it('it should return error if req has no data', (done) => {
/* it('it should return error if req has no data', (done) => {
chai.request(app).post('/api/v1/login')
.send({
emailAdress: 'sarahbeth@gmail.com',
Expand All @@ -125,7 +125,7 @@ describe('Create New user', () => {
done();
});
});
});
}); */

it('it should have status 401 if user not logged in', (done) => {
chai.request(app).post('/api/v1/users')
Expand Down

0 comments on commit 22e8920

Please sign in to comment.