Adding tests for updating products

master
Mark Moffat 2019-06-20 12:44:04 +09:30
parent fc1580ddd6
commit fad68a51ea
4 changed files with 114 additions and 18 deletions

View File

@ -2,6 +2,9 @@
"id": "/editProduct",
"type": "object",
"properties": {
"productId": {
"type": "string"
},
"productPermalink": {
"type": "string"
},
@ -31,10 +34,6 @@
}
},
"required": [
"productPermalink",
"productTitle",
"productPrice",
"productDescription",
"productPublished"
"productId"
]
}

View File

@ -198,7 +198,7 @@ router.post('/admin/product/insert', restrict, checkAccess, (req, res) => {
// If API request, return json
if(req.apiAuthenticated){
res.status(200).json({ error: 'New product successfully created' });
res.status(200).json({ message: 'New product successfully created' });
return;
}
@ -244,24 +244,44 @@ router.get('/admin/product/edit/:id', restrict, checkAccess, (req, res) => {
router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
const db = req.app.db;
db.products.findOne({ _id: common.getId(req.body.frmProductId) }, (err, product) => {
db.products.findOne({ _id: common.getId(req.body.productId) }, (err, product) => {
if(err){
console.info(err.stack);
req.session.message = 'Failed updating product.';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.frmProductId);
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to update product' });
return;
}
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
db.products.count({ 'productPermalink': req.body.productPermalink, _id: { $ne: common.getId(product._id) } }, (err, count) => {
if(err){
console.info(err.stack);
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to update product' });
return;
}
req.session.message = 'Failed updating product.';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.frmProductId);
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
if(count > 0 && req.body.productPermalink !== ''){
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Permalink already exists. Pick a new one' });
return;
}
// permalink exits
req.session.message = 'Permalink already exists. Pick a new one.';
req.session.messageType = 'danger';
@ -277,9 +297,9 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
req.session.productStock = req.body.productStock ? req.body.productStock : null;
// redirect to insert
res.redirect('/admin/product/edit/' + req.body.frmProductId);
res.redirect('/admin/product/edit/' + req.body.productId);
}else{
common.getImages(req.body.frmProductId, req, res, (images) => {
common.getImages(req.body.productId, req, res, (images) => {
// Process supplied options
let productOptions = req.body.productOptions;
if(productOptions && typeof productOptions !== 'object'){
@ -291,6 +311,7 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
}
let productDoc = {
productId: req.body.productId,
productPermalink: req.body.productPermalink,
productTitle: common.cleanHtml(req.body.productTitle),
productPrice: common.safeParseInt(req.body.productPrice),
@ -311,7 +332,6 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
return;
}
console.log('schemaResult errors', schemaResult.errors);
req.session.message = 'Form invalid. Please check values and try again.';
req.session.messageType = 'danger';
@ -326,10 +346,13 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
req.session.productStock = req.body.productStock ? parseInt(req.body.productStock) : null;
// redirect to insert
res.redirect('/admin/product/edit/' + req.body.frmProductId);
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
// Remove productId from doc
delete productDoc.productId;
// if no featured image
if(!product.productImage){
if(images.length > 0){
@ -341,19 +364,31 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
productDoc['productImage'] = product.productImage;
}
db.products.update({ _id: common.getId(req.body.frmProductId) }, { $set: productDoc }, {}, (err, numReplaced) => {
db.products.update({ _id: common.getId(req.body.productId) }, { $set: productDoc }, {}, (err, numReplaced) => {
if(err){
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to save. Please try again' });
return;
}
console.error(colors.red('Failed to save product: ' + err));
req.session.message = 'Failed to save. Please try again';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.frmProductId);
res.redirect('/admin/product/edit/' + req.body.productId);
}else{
// Update the index
indexProducts(req.app)
.then(() => {
// If API request, return json
if(req.apiAuthenticated){
res.status(200).json({ message: 'Successfully saved', product: productDoc });
return;
}
req.session.message = 'Successfully saved';
req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.frmProductId);
res.redirect('/admin/product/edit/' + req.body.productId);
});
}
});

View File

@ -334,3 +334,65 @@ test.serial('[Success] Get single customer', async t => {
// Check the returned customer matches ID
t.deepEqual(customers[0]._id.toString(), res.body._id);
});
test.serial('[Success] Add a product', async t => {
const product = {
productPermalink: 'test-jacket',
productTitle: 'Test Jacket',
productPrice: 100,
productDescription: 'Test desc',
productPublished: true,
productTags: 'organic, jacket',
productOptions: {
Size: {
optName: 'Size',
optLabel: 'Select size',
optType: 'select',
optOptions: ['S', 'M', 'L', 'XL']
}
},
productComment: 'test comment',
productStock: 50
};
const res = await request
.post('/admin/product/insert')
.send(product)
.set('apiKey', users[0].apiKey)
.expect(200);
// Check the returned message
t.deepEqual(res.body.message, 'New product successfully created');
});
test.serial('[Success] Update a product', async t => {
const product = {
productId: products[0]._id,
productTitle: 'Test Jacket',
productPrice: 200,
productDescription: 'Test desc',
productPublished: true,
productTags: 'organic, jacket',
productOptions: {
Size: {
optName: 'Size',
optLabel: 'Select size',
optType: 'select',
optOptions: ['S', 'M', 'L', 'XL']
}
},
productComment: 'test comment',
productStock: 50
};
const res = await request
.post('/admin/product/update')
.send(product)
.set('apiKey', users[0].apiKey)
.expect(200);
// Check the returned message
t.deepEqual(res.body.message, 'Successfully saved');
t.deepEqual(res.body.product.productTitle, product.productTitle);
t.deepEqual(res.body.product.productPrice, product.productPrice);
});

View File

@ -151,7 +151,7 @@
{{/if}}
</div>
</div>
<input type="hidden" name="frmProductId" id="frmProductId" value="{{result._id}}" />
<input type="hidden" name="productId" id="productId" value="{{result._id}}" />
</form>
</div>