From b41461c8a320f12b52de253b73d61c82f6987ab4 Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sat, 9 Nov 2019 13:29:20 +1030 Subject: [PATCH] Refactoring product route --- routes/product.js | 19 ++++++++++++++++++- test/specs/products.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/routes/product.js b/routes/product.js index aa4e3f7..7f708c5 100644 --- a/routes/product.js +++ b/routes/product.js @@ -25,7 +25,7 @@ router.get('/admin/products', restrict, async (req, res, next) => { }); }); -router.get('/admin/products/filter/:search', async (req, res, next) => { +router.get('/admin/products/filter/:search', restrict, async (req, res, next) => { const db = req.app.db; const searchTerm = req.params.search; const productsIndex = req.app.productsIndex; @@ -37,6 +37,12 @@ router.get('/admin/products/filter/:search', async (req, res, next) => { // we search on the lunr indexes const results = await db.products.find({ _id: { $in: lunrIdArray } }).toArray(); + + if(req.apiAuthenticated){ + res.status(200).json(results); + return; + } + res.render('products', { title: 'Results', results: results, @@ -205,6 +211,11 @@ router.get('/admin/product/edit/:id', restrict, checkAccess, async (req, res) => const images = await common.getImages(req.params.id, req, res); const product = await db.products.findOne({ _id: common.getId(req.params.id) }); if(!product){ + // If API request, return json + if(req.apiAuthenticated){ + res.status(400).json({ message: 'Product not found' }); + return; + } req.session.message = 'Product not found'; req.session.messageType = 'danger'; res.redirect('/admin/products'); @@ -215,6 +226,12 @@ router.get('/admin/product/edit/:id', restrict, checkAccess, async (req, res) => options = product.productOptions; } + // If API request, return json + if(req.apiAuthenticated){ + res.status(200).json(product); + return; + } + res.render('product_edit', { title: 'Edit product', result: product, diff --git a/test/specs/products.js b/test/specs/products.js index c5d7d7f..95c5055 100644 --- a/test/specs/products.js +++ b/test/specs/products.js @@ -134,6 +134,37 @@ test('[Success] Search products', async t => { t.deepEqual(res.body.length, 2); }); +test('[Success] Filter products', async t => { + const res = await g.request + .get('/admin/products/filter/backpack') + .set('apiKey', g.users[0].apiKey) + .expect(200); + + // Should be two backpack products + t.deepEqual(res.body.length, 2); +}); + +test('[Success] Edit a product', async t => { + const res = await g.request + .get(`/admin/product/edit/${g.products[0]._id}`) + .set('apiKey', g.users[0].apiKey) + .expect(200); + + // Products should match + t.deepEqual(res.body._id.toString(), g.products[0]._id.toString()); + t.deepEqual(res.body.productPermalink, g.products[0].productPermalink); +}); + +test('[Fail] Edit an invalid product', async t => { + const res = await g.request + .get('/admin/product/edit/some_invalid_product') + .set('apiKey', g.users[0].apiKey) + .expect(400); + + // Check the returned message + t.deepEqual(res.body.message, 'Product not found'); +}); + test('[Success] Add a product', async t => { const product = { productPermalink: 'test-jacket',