expressCart/test/test.js

182 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-02-23 03:41:24 +10:00
const test = require('ava');
const fs = require('fs');
const app = require('../app');
const common = require('../lib/common');
2019-06-11 15:12:07 +10:00
const session = require('supertest-session');
2018-02-23 03:41:24 +10:00
// Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
2019-06-09 22:32:28 +10:00
const jsonData = JSON.parse(rawTestData);
2018-02-23 03:41:24 +10:00
2018-02-24 05:28:05 +10:00
// Setup some global DB objects for comparison
2018-02-23 03:41:24 +10:00
let db;
2018-02-24 05:28:05 +10:00
let config;
2018-02-23 03:41:24 +10:00
let products;
let customers;
let users;
2019-06-11 15:12:07 +10:00
let request = null;
2018-02-23 03:41:24 +10:00
2019-06-11 20:03:56 +10:00
function setup(db, app){
2019-06-09 22:32:28 +10:00
return Promise.all([
2019-06-11 15:12:07 +10:00
db.cart.remove({}, {}),
2019-06-09 22:32:28 +10:00
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)),
2019-06-12 18:56:08 +10:00
db.menu.insertOne(jsonData.menu)
2019-06-09 22:32:28 +10:00
]);
});
}
2018-02-24 05:28:05 +10:00
2019-06-09 22:32:28 +10:00
// Start up app and wait for it to be ready
test.before(async () => {
2019-06-11 15:12:07 +10:00
// Create a session
request = session(app);
await new Promise(resolve => {
2019-06-09 22:32:28 +10:00
app.on('appStarted', async () => {
// Set some stuff now we have the app started
config = app.config;
db = app.db;
2019-06-11 20:03:56 +10:00
await setup(db, app);
2019-06-12 18:56:08 +10:00
await common.runIndexing(app);
2019-06-09 22:32:28 +10:00
// 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();
});
2018-02-23 03:41:24 +10:00
});
});
2019-06-09 22:32:28 +10:00
test.serial('[Success] Get products JSON', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.get('?json=true')
2019-06-11 15:12:07 +10:00
.expect(200);
2019-06-09 22:32:28 +10:00
if(res.body.length < config.productsPerPage){
2019-06-11 15:12:07 +10:00
t.is(res.body.length, products.length);
2019-06-09 22:32:28 +10:00
}else{
t.is(res.body.length, config.productsPerPage);
}
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:32:28 +10:00
test.serial('[Success] User Login', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/admin/login_action')
.send({
2018-02-23 03:41:24 +10:00
email: users[0].userEmail,
password: 'test'
})
2019-06-11 15:12:07 +10:00
.expect(200);
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'Login successful');
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:32:28 +10:00
test.serial('[Fail] Incorrect user password', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/admin/login_action')
.send({
2018-02-23 03:41:24 +10:00
email: users[0].userEmail,
password: 'test1'
})
2019-06-11 15:12:07 +10:00
.expect(400);
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'Access denied. Check password and try again.');
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:32:28 +10:00
test.serial('[Fail] Customer login with incorrect email', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/customer/login_action')
.send({
2018-02-23 03:41:24 +10:00
loginEmail: 'test1@test.com',
loginPassword: 'test'
})
2019-06-11 15:12:07 +10:00
.expect(400);
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'A customer with that email does not exist.');
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:32:28 +10:00
test.serial('[Success] Customer login with correct email', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/customer/login_action')
.send({
2019-06-09 22:32:28 +10:00
loginEmail: customers[0].email,
2018-02-23 03:41:24 +10:00
loginPassword: 'test'
})
2019-06-11 15:12:07 +10:00
.expect(200);
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'Successfully logged in');
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:32:28 +10:00
test.serial('[Success] Add product to cart', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/product/addtocart')
.send({
2018-02-23 03:41:24 +10:00
productId: products[0]._id,
productQuantity: 1,
productOptions: JSON.stringify(products[0].productOptions)
})
2019-06-11 15:12:07 +10:00
.expect(200);
const sessions = await db.cart.find({}).toArray();
if(!sessions || sessions.length === 0){
t.fail();
}
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'Cart successfully updated');
2018-02-23 03:41:24 +10:00
});
2019-06-09 22:49:50 +10:00
test.serial('[Fail] Add product to cart with not enough stock', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2019-06-09 22:49:50 +10:00
.post('/product/addtocart')
.send({
productId: products[0]._id,
productQuantity: 100,
productOptions: JSON.stringify(products[0].productOptions)
})
2019-06-11 15:12:07 +10:00
.expect(400);
2019-06-09 22:49:50 +10:00
t.deepEqual(res.body.message, 'There is insufficient stock of this product.');
});
2019-06-09 22:32:28 +10:00
test.serial('[Fail] Add incorrect product to cart', async t => {
2019-06-11 15:12:07 +10:00
const res = await request
2018-02-24 05:28:05 +10:00
.post('/product/addtocart')
.send({
id: 'fake_product_id',
state: false
2018-02-23 03:41:24 +10:00
})
2019-06-11 15:12:07 +10:00
.expect(400);
2019-06-09 22:32:28 +10:00
t.deepEqual(res.body.message, 'Error updating cart. Please try again.');
2018-02-23 03:41:24 +10:00
});
2019-06-11 15:12:07 +10:00
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');
});
2019-06-11 20:03:56 +10:00
test.serial('[Success] Search products', async t => {
const res = await request
.get('/category/backpack?json=true')
.expect(200);
console.log('res.body', res.body);
2019-06-11 20:03:56 +10:00
// Should be two backpack products
t.deepEqual(res.body.length, 2);
});