Fixing tests
							parent
							
								
									3a61855609
								
							
						
					
					
						commit
						e48bd97bf7
					
				|  | @ -8874,6 +8874,15 @@ | |||
|         "superagent": "^3.8.3" | ||||
|       } | ||||
|     }, | ||||
|     "supertest-session": { | ||||
|       "version": "4.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/supertest-session/-/supertest-session-4.0.0.tgz", | ||||
|       "integrity": "sha512-9d7KAL+K9hnnicov7USv/Nu1tSl40qSrOsB8zZHOEtfEzHaAoID6tbl1NeCVUg7SJreJMlNn+KJ88V7FW8gD6Q==", | ||||
|       "dev": true, | ||||
|       "requires": { | ||||
|         "object-assign": "^4.0.1" | ||||
|       } | ||||
|     }, | ||||
|     "supports-color": { | ||||
|       "version": "2.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", | ||||
|  |  | |||
|  | @ -7,7 +7,7 @@ | |||
|     "start": "node app.js", | ||||
|     "deploy": "gulp deploy", | ||||
|     "testdata": "node lib/testdata.js", | ||||
|     "test": "NODE_ENV=test ava test/test.js --verbose" | ||||
|     "test": "NODE_ENV=test ava --verbose" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "ajv": "^6.10.0", | ||||
|  | @ -61,6 +61,7 @@ | |||
|     "gulp-rename": "^1.4.0", | ||||
|     "mongodb-uri": "^0.9.7", | ||||
|     "supertest": "^3.4.2", | ||||
|     "supertest-session": "^4.0.0", | ||||
|     "wait.for": "^0.6.6" | ||||
|   }, | ||||
|   "main": "app.js", | ||||
|  |  | |||
|  | @ -4,7 +4,6 @@ const colors = require('colors'); | |||
| const async = require('async'); | ||||
| const _ = require('lodash'); | ||||
| const common = require('../lib/common'); | ||||
| const ObjectId = require('mongodb').ObjectID; | ||||
| 
 | ||||
| // These is the customer facing routes
 | ||||
| router.get('/payment/:orderId', async (req, res, next) => { | ||||
|  | @ -233,11 +232,13 @@ router.post('/product/updatecart', (req, res, next) => { | |||
| // Remove single product from cart
 | ||||
| router.post('/product/removefromcart', (req, res, next) => { | ||||
|     const db = req.app.db; | ||||
|     let itemRemoved = false; | ||||
| 
 | ||||
|     // remove item from cart
 | ||||
|     async.each(req.session.cart, (item, callback) => { | ||||
|         if(item){ | ||||
|             if(item.productId === req.body.cart_index){ | ||||
|             if(item.productId === req.body.cartId){ | ||||
|                 itemRemoved = true; | ||||
|                 req.session.cart = _.pull(req.session.cart, item); | ||||
|             } | ||||
|         } | ||||
|  | @ -249,7 +250,11 @@ router.post('/product/removefromcart', (req, res, next) => { | |||
|         }); | ||||
|         // update total cart amount
 | ||||
|         common.updateTotalCartAmount(req, res); | ||||
|         res.status(200).json({message: 'Product successfully removed', totalCartItems: Object.keys(req.session.cart).length}); | ||||
| 
 | ||||
|         if(itemRemoved === false){ | ||||
|             return res.status(400).json({message: 'Product not found in cart'}); | ||||
|         } | ||||
|         return res.status(200).json({message: 'Product successfully removed', totalCartItems: Object.keys(req.session.cart).length}); | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										72
									
								
								test/test.js
								
								
								
								
							
							
						
						
									
										72
									
								
								test/test.js
								
								
								
								
							|  | @ -2,7 +2,7 @@ const test = require('ava'); | |||
| const fs = require('fs'); | ||||
| const app = require('../app'); | ||||
| const common = require('../lib/common'); | ||||
| const request = require('supertest'); | ||||
| const session = require('supertest-session'); | ||||
| 
 | ||||
| // Get test data to compare in tests
 | ||||
| const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8'); | ||||
|  | @ -14,9 +14,11 @@ let config; | |||
| let products; | ||||
| let customers; | ||||
| let users; | ||||
| let request = null; | ||||
| 
 | ||||
| function setup(db){ | ||||
|     return Promise.all([ | ||||
|         db.cart.remove({}, {}), | ||||
|         db.users.remove({}, {}), | ||||
|         db.customers.remove({}, {}), | ||||
|         db.products.remove({}, {}), | ||||
|  | @ -34,7 +36,9 @@ function setup(db){ | |||
| 
 | ||||
| // Start up app and wait for it to be ready
 | ||||
| test.before(async () => { | ||||
|     return await new Promise(resolve => { | ||||
|     // Create a session
 | ||||
|     request = session(app); | ||||
|     await new Promise(resolve => { | ||||
|         app.on('appStarted', async () => { | ||||
|             // Set some stuff now we have the app started
 | ||||
|             config = app.config; | ||||
|  | @ -52,97 +56,115 @@ test.before(async () => { | |||
| }); | ||||
| 
 | ||||
| test.serial('[Success] Get products JSON', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .get('?json=true') | ||||
|         .expect(200) | ||||
|         .expect(200); | ||||
|     if(res.body.length < config.productsPerPage){ | ||||
|         t.is(res.body.length, testData.products.length); | ||||
|         t.is(res.body.length, products.length); | ||||
|     }else{ | ||||
|         t.is(res.body.length, config.productsPerPage); | ||||
|     } | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Success] User Login', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/admin/login_action') | ||||
|         .send({ | ||||
|             email: users[0].userEmail, | ||||
|             password: 'test' | ||||
|         }) | ||||
|         .expect(200) | ||||
|         .expect(200); | ||||
|     t.deepEqual(res.body.message, 'Login successful'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Fail] Incorrect user password', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/admin/login_action') | ||||
|         .send({ | ||||
|             email: users[0].userEmail, | ||||
|             password: 'test1' | ||||
|         }) | ||||
|         .expect(400) | ||||
|      | ||||
|         .expect(400); | ||||
|     t.deepEqual(res.body.message, 'Access denied. Check password and try again.'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Fail] Customer login with incorrect email', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/customer/login_action') | ||||
|         .send({ | ||||
|             loginEmail: 'test1@test.com', | ||||
|             loginPassword: 'test' | ||||
|         }) | ||||
|         .expect(400) | ||||
|          | ||||
|         .expect(400); | ||||
|     t.deepEqual(res.body.message, 'A customer with that email does not exist.'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Success] Customer login with correct email', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/customer/login_action') | ||||
|         .send({ | ||||
|             loginEmail: customers[0].email, | ||||
|             loginPassword: 'test' | ||||
|         }) | ||||
|         .expect(200) | ||||
|      | ||||
|         .expect(200); | ||||
|     t.deepEqual(res.body.message, 'Successfully logged in'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Success] Add product to cart', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/product/addtocart') | ||||
|         .send({ | ||||
|             productId: products[0]._id, | ||||
|             productQuantity: 1, | ||||
|             productOptions: JSON.stringify(products[0].productOptions) | ||||
|         }) | ||||
|         .expect(200) | ||||
|          | ||||
|         .expect(200); | ||||
|     const sessions = await db.cart.find({}).toArray(); | ||||
|     if(!sessions || sessions.length === 0){ | ||||
|         t.fail(); | ||||
|     } | ||||
|     t.deepEqual(res.body.message, 'Cart successfully updated'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Fail] Add product to cart with not enough stock', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/product/addtocart') | ||||
|         .send({ | ||||
|             productId: products[0]._id, | ||||
|             productQuantity: 100, | ||||
|             productOptions: JSON.stringify(products[0].productOptions) | ||||
|         }) | ||||
|         .expect(400) | ||||
|          | ||||
|         .expect(400); | ||||
|     t.deepEqual(res.body.message, 'There is insufficient stock of this product.'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Fail] Add incorrect product to cart', async t => { | ||||
|     const res = await request(app) | ||||
|     const res = await request | ||||
|         .post('/product/addtocart') | ||||
|         .send({ | ||||
|             id: 'fake_product_id', | ||||
|             state: false | ||||
|         }) | ||||
|         .expect(400) | ||||
|          | ||||
|         .expect(400); | ||||
|     t.deepEqual(res.body.message, 'Error updating cart. Please try again.'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Success] Remove item previously added to cart', async t => { | ||||
|     const res = await request | ||||
|         .post('/product/removefromcart') | ||||
|         .send({ | ||||
|             cartId: products[0]._id | ||||
|         }) | ||||
|         .expect(200); | ||||
|     t.deepEqual(res.body.message, 'Product successfully removed'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('[Fail] Try remove an item which is not in the cart', async t => { | ||||
|     const res = await request | ||||
|         .post('/product/removefromcart') | ||||
|         .send({ | ||||
|             cartId: 'bogus_product_id' | ||||
|         }) | ||||
|         .expect(400); | ||||
|     t.deepEqual(res.body.message, 'Product not found in cart'); | ||||
| }); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue