Test fixes

master
Mark Moffat 2018-02-23 20:28:05 +01:00
parent 4d14589484
commit fa660edb0c
5 changed files with 137 additions and 86 deletions

View File

@ -56,6 +56,7 @@
"gulp-rename": "^1.2.2",
"mongodb-uri": "^0.9.7",
"run-sequence": "^2.2.1",
"supertest": "^3.0.0",
"wait.for": "^0.6.6"
},
"main": "app.js",

View File

@ -392,7 +392,7 @@ $(document).ready(function (){
location.reload();
})
.fail(function(msg){
showNotification(msg.responseJSON.err, 'danger');
showNotification(msg.responseJSON.message, 'danger');
});
}
e.preventDefault();

File diff suppressed because one or more lines are too long

View File

@ -137,14 +137,14 @@ router.post('/customer/login_action', async (req, res) => {
if(err){
// An error accurred
return res.status(400).json({
err: 'Access denied. Check password and try again.'
message: 'Access denied. Check password and try again.'
});
}
// check if customer exists with that email
if(customer === undefined || customer === null){
return res.status(400).json({
err: 'A customer with that email does not exist.'
message: 'A customer with that email does not exist.'
});
}
// we have a customer under that email so we compare the password
@ -153,7 +153,7 @@ router.post('/customer/login_action', async (req, res) => {
if(!result){
// password is not correct
return res.status(400).json({
err: 'Access denied. Check password and try again.'
message: 'Access denied. Check password and try again.'
});
}
@ -166,7 +166,7 @@ router.post('/customer/login_action', async (req, res) => {
})
.catch((err) => {
return res.status(400).json({
err: 'Access denied. Check password and try again.'
message: 'Access denied. Check password and try again.'
});
});
});

View File

@ -1,16 +1,17 @@
const test = require('ava');
const axios = require('axios');
const fs = require('fs');
const app = require('../app');
const common = require('../lib/common');
const request = require('supertest');
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);
let config;
// Setup some global DB objects for comparison
let db;
let baseUrl;
let config;
let products;
let customers;
let users;
@ -21,127 +22,176 @@ test.before.cb(t => {
// Set some stuff now we have the app started
config = app.config;
db = app.db;
baseUrl = `http://localhost:${app.port}`;
// 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();
});
});
});
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);
}else{
t.is(res.body.length, config.productsPerPage);
}
t.pass();
t.end();
});
});
test('[Success] Get products JSON', t => {
return new Promise((resolve, reject) => {
axios.get(`${baseUrl}?json=true`)
.then((response) => {
if(response.data.length < config.productsPerPage){
t.is(response.data.length, testData.products.length);
}else{
t.is(response.data.length, config.productsPerPage);
}
t.pass();
resolve();
})
.catch((error) => {
reject(new Error('Should not be allowed'));
});
});
});
test('[Success] User Login', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/admin/login_action`, {
test.cb('[Success] User Login', t => {
agent
.post('/admin/login_action')
.send({
email: users[0].userEmail,
password: 'test'
})
.then((response) => {
t.deepEqual(response.data.message, 'Login successful');
resolve();
})
.catch((error) => {
reject(new Error('Should not be allowed'));
});
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Login successful');
t.end();
});
});
test('[Fail] Incorrect user password', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/admin/login_action`, {
test.cb('[Fail] Incorrect user password', t => {
agent
.post('/admin/login_action')
.send({
email: users[0].userEmail,
password: 'test1'
})
.then((response) => {
reject(new Error('Should not be allowed'));
})
.catch((error) => {
t.deepEqual(error.response.data.message, 'Access denied. Check password and try again.');
resolve();
});
.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();
});
});
test('[Fail] Customer login with incorrect email', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/customer/login_action`, {
test.cb('[Fail] Customer login with incorrect email', t => {
agent
.post('/customer/login_action')
.send({
loginEmail: 'test1@test.com',
loginPassword: 'test'
})
.then((response) => {
reject(new Error('Should not be allowed'));
})
.catch((error) => {
t.deepEqual(error.response.data.err, 'A customer with that email does not exist.');
resolve();
});
.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();
});
});
test('[Success] Customer login with correct email', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/customer/login_action`, {
test.cb('[Success] Customer login with correct email', t => {
agent
.post('/customer/login_action')
.send({
loginEmail: 'test@test.com',
loginPassword: 'test'
})
.then((response) => {
t.deepEqual(response.data.message, 'Successfully logged in');
resolve();
})
.catch((error) => {
reject(new Error('Should not be allowed'));
});
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Successfully logged in');
t.end();
});
});
test('[Success] Add product to cart', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/product/addtocart`, {
test.cb('[Success] Add product to cart', t => {
agent
.post('/product/addtocart')
.send({
productId: products[0]._id,
productQuantity: 1,
productOptions: JSON.stringify(products[0].productOptions)
})
.then((response) => {
t.deepEqual(response.data.message, 'Cart successfully updated');
resolve();
})
.catch((error) => {
reject(new Error('Should not be allowed'));
});
.expect(200)
.end((err, res) => {
if(err){
t.fail();
t.end();
}
t.deepEqual(res.body.message, 'Cart successfully updated');
t.end();
});
});
test('[Fail] Add incorrect product to cart', t => {
return new Promise((resolve, reject) => {
axios.post(`${baseUrl}/product/addtocart`, {
productId: 'someid'
test.cb('[Fail] Add incorrect product to cart', t => {
agent
.post('/product/addtocart')
.send({
id: 'fake_product_id',
state: false
})
.then((response) => {
t.deepEqual(response.data.message, 'Successfully logged in');
reject(new Error('Should not be allowed'));
resolve();
})
.catch((error) => {
t.deepEqual(error.response.data.message, 'Error updating cart. Please try again.');
resolve();
});
.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();
});
});