diff --git a/test/test.js b/test/test.js index deca930..0c89126 100644 --- a/test/test.js +++ b/test/test.js @@ -7,7 +7,7 @@ const agent = request.agent(app); // Get test data to compare in tests const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8'); -const testData = JSON.parse(rawTestData); +const jsonData = JSON.parse(rawTestData); // Setup some global DB objects for comparison let db; @@ -16,133 +16,102 @@ let products; let customers; let users; -// Start up app and wait for it to be ready -test.before.cb(t => { - app.on('appStarted', async () => { - // Set some stuff now we have the app started - config = app.config; - db = app.db; +function setup(db){ + return Promise.all([ + db.users.remove({}, {}), + db.customers.remove({}, {}), + db.products.remove({}, {}), + db.menu.remove({}, {}) + ]) + .then(() => { + return Promise.all([ + db.users.insertMany(jsonData.users), + db.customers.insertMany(jsonData.customers), + db.products.insertMany(common.fixProductDates(jsonData.products)), + db.menu.insertOne(jsonData.menu) + ]); + }); +} - // Get some data from DB to use in compares - await common.testData(app); - products = await db.products.find({}).toArray(); - customers = await db.customers.find({}).toArray(); - users = await db.users.find({}).toArray(); - agent - .post('/admin/login_action') - .send({ - email: users[0].userEmail, - password: 'test' - }) - .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - t.end(); - }); +// Start up app and wait for it to be ready +test.before(async () => { + return await new Promise(resolve => { + app.on('appStarted', async () => { + // Set some stuff now we have the app started + config = app.config; + db = app.db; + + await setup(db); + + // Get some data from DB to use in compares + products = await db.products.find({}).toArray(); + customers = await db.customers.find({}).toArray(); + users = await db.users.find({}).toArray(); + resolve(); + }); }); }); -test.cb('[Success] Get products JSON', t => { - agent +test.serial('[Success] Get products JSON', async t => { + const res = await request(app) .get('?json=true') .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - if(res.body.length < config.productsPerPage){ - t.is(res.body.length, testData.products.length); - }else{ - t.is(res.body.length, config.productsPerPage); - } - t.pass(); - t.end(); - }); + if(res.body.length < config.productsPerPage){ + t.is(res.body.length, testData.products.length); + }else{ + t.is(res.body.length, config.productsPerPage); + } }); -test.cb('[Success] User Login', t => { - agent +test.serial('[Success] User Login', async t => { + const res = await request(app) .post('/admin/login_action') .send({ email: users[0].userEmail, password: 'test' }) .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - t.deepEqual(res.body.message, 'Login successful'); - t.end(); - }); + t.deepEqual(res.body.message, 'Login successful'); }); -test.cb('[Fail] Incorrect user password', t => { - agent +test.serial('[Fail] Incorrect user password', async t => { + const res = await request(app) .post('/admin/login_action') .send({ email: users[0].userEmail, password: 'test1' }) .expect(400) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - t.deepEqual(res.body.message, 'Access denied. Check password and try again.'); - t.end(); - }); + + t.deepEqual(res.body.message, 'Access denied. Check password and try again.'); }); -test.cb('[Fail] Customer login with incorrect email', t => { - agent +test.serial('[Fail] Customer login with incorrect email', async t => { + const res = await request(app) .post('/customer/login_action') .send({ loginEmail: 'test1@test.com', loginPassword: 'test' }) .expect(400) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - t.deepEqual(res.body.message, 'A customer with that email does not exist.'); - t.end(); - }); + + t.deepEqual(res.body.message, 'A customer with that email does not exist.'); }); -test.cb('[Success] Customer login with correct email', t => { - agent +test.serial('[Success] Customer login with correct email', async t => { + const res = await request(app) .post('/customer/login_action') .send({ - loginEmail: 'test@test.com', + loginEmail: customers[0].email, loginPassword: 'test' }) .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - t.deepEqual(res.body.message, 'Successfully logged in'); - t.end(); - }); + + t.deepEqual(res.body.message, 'Successfully logged in'); }); -test.cb('[Success] Add product to cart', t => { - agent +test.serial('[Success] Add product to cart', async t => { + const res = await request(app) .post('/product/addtocart') .send({ productId: products[0]._id, @@ -150,48 +119,18 @@ test.cb('[Success] Add product to cart', t => { productOptions: JSON.stringify(products[0].productOptions) }) .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - - t.deepEqual(res.body.message, 'Cart successfully updated'); - t.end(); - }); + + t.deepEqual(res.body.message, 'Cart successfully updated'); }); -test.cb('[Fail] Add incorrect product to cart', t => { - agent +test.serial('[Fail] Add incorrect product to cart', async t => { + const res = await request(app) .post('/product/addtocart') .send({ id: 'fake_product_id', state: false }) .expect(400) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - t.deepEqual(res.body.message, 'Error updating cart. Please try again.'); - t.end(); - }); -}); - -test.cb('[Sucess] Change product publish status', t => { - agent - .post('/admin/product/published_state') - .send({ - id: products[0]._id, - state: false - }) - .expect(200) - .end((err, res) => { - if(err){ - t.fail(); - t.end(); - } - t.end(); - }); + + t.deepEqual(res.body.message, 'Error updating cart. Please try again.'); });