expressCart/test/test.js

198 lines
4.8 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');
2018-02-24 05:28:05 +10:00
const request = require('supertest');
const agent = request.agent(app);
2018-02-23 03:41:24 +10:00
// Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
const testData = JSON.parse(rawTestData);
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;
// 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;
2018-02-24 05:28:05 +10:00
// Get some data from DB to use in compares
2018-02-23 03:41:24 +10:00
await common.testData(app);
products = await db.products.find({}).toArray();
customers = await db.customers.find({}).toArray();
users = await db.users.find({}).toArray();
2018-02-24 05:28:05 +10:00
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();
});
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Success] Get products JSON', t => {
agent
.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);
2018-02-23 03:41:24 +10:00
}else{
2018-02-24 05:28:05 +10:00
t.is(res.body.length, config.productsPerPage);
2018-02-23 03:41:24 +10:00
}
t.pass();
2018-02-24 05:28:05 +10:00
t.end();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Success] User Login', t => {
agent
.post('/admin/login_action')
.send({
2018-02-23 03:41:24 +10:00
email: users[0].userEmail,
password: 'test'
})
2018-02-24 05:28:05 +10:00
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Login successful');
t.end();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Fail] Incorrect user password', t => {
agent
.post('/admin/login_action')
.send({
2018-02-23 03:41:24 +10:00
email: users[0].userEmail,
password: 'test1'
})
2018-02-24 05:28:05 +10:00
.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();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Fail] Customer login with incorrect email', t => {
agent
.post('/customer/login_action')
.send({
2018-02-23 03:41:24 +10:00
loginEmail: 'test1@test.com',
loginPassword: 'test'
})
2018-02-24 05:28:05 +10:00
.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();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Success] Customer login with correct email', t => {
agent
.post('/customer/login_action')
.send({
2018-02-23 03:41:24 +10:00
loginEmail: 'test@test.com',
loginPassword: 'test'
})
2018-02-24 05:28:05 +10:00
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Successfully logged in');
t.end();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Success] Add product to cart', t => {
agent
.post('/product/addtocart')
.send({
2018-02-23 03:41:24 +10:00
productId: products[0]._id,
productQuantity: 1,
productOptions: JSON.stringify(products[0].productOptions)
})
2018-02-24 05:28:05 +10:00
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Cart successfully updated');
t.end();
2018-02-23 03:41:24 +10:00
});
});
2018-02-24 05:28:05 +10:00
test.cb('[Fail] Add incorrect product to cart', t => {
agent
.post('/product/addtocart')
.send({
id: 'fake_product_id',
state: false
2018-02-23 03:41:24 +10:00
})
2018-02-24 05:28:05 +10:00
.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
2018-02-23 03:41:24 +10:00
})
2018-02-24 05:28:05 +10:00
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.end();
2018-02-23 03:41:24 +10:00
});
});