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", "gulp-rename": "^1.2.2",
"mongodb-uri": "^0.9.7", "mongodb-uri": "^0.9.7",
"run-sequence": "^2.2.1", "run-sequence": "^2.2.1",
"supertest": "^3.0.0",
"wait.for": "^0.6.6" "wait.for": "^0.6.6"
}, },
"main": "app.js", "main": "app.js",

View File

@ -392,7 +392,7 @@ $(document).ready(function (){
location.reload(); location.reload();
}) })
.fail(function(msg){ .fail(function(msg){
showNotification(msg.responseJSON.err, 'danger'); showNotification(msg.responseJSON.message, 'danger');
}); });
} }
e.preventDefault(); 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){ if(err){
// An error accurred // An error accurred
return res.status(400).json({ 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 // check if customer exists with that email
if(customer === undefined || customer === null){ if(customer === undefined || customer === null){
return res.status(400).json({ 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 // 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){ if(!result){
// password is not correct // password is not correct
return res.status(400).json({ 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) => { .catch((err) => {
return res.status(400).json({ 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 test = require('ava');
const axios = require('axios');
const fs = require('fs'); const fs = require('fs');
const app = require('../app'); const app = require('../app');
const common = require('../lib/common'); const common = require('../lib/common');
const request = require('supertest');
const agent = request.agent(app);
// Get test data to compare in tests // Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8'); const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
const testData = JSON.parse(rawTestData); const testData = JSON.parse(rawTestData);
let config; // Setup some global DB objects for comparison
let db; let db;
let baseUrl; let config;
let products; let products;
let customers; let customers;
let users; let users;
@ -21,127 +22,176 @@ test.before.cb(t => {
// Set some stuff now we have the app started // Set some stuff now we have the app started
config = app.config; config = app.config;
db = app.db; db = app.db;
baseUrl = `http://localhost:${app.port}`;
// Get some data from DB to use in compares
await common.testData(app); await common.testData(app);
products = await db.products.find({}).toArray(); products = await db.products.find({}).toArray();
customers = await db.customers.find({}).toArray(); customers = await db.customers.find({}).toArray();
users = await db.users.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(); t.end();
}); });
}); });
test('[Success] Get products JSON', t => { test.cb('[Success] User Login', t => {
return new Promise((resolve, reject) => { agent
axios.get(`${baseUrl}?json=true`) .post('/admin/login_action')
.then((response) => { .send({
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`, {
email: users[0].userEmail, email: users[0].userEmail,
password: 'test' password: 'test'
}) })
.then((response) => { .expect(200)
t.deepEqual(response.data.message, 'Login successful'); .end((err, res) => {
resolve(); if(err){
}) t.fail();
.catch((error) => { t.end();
reject(new Error('Should not be allowed')); }
});
t.deepEqual(res.body.message, 'Login successful');
t.end();
}); });
}); });
test('[Fail] Incorrect user password', t => { test.cb('[Fail] Incorrect user password', t => {
return new Promise((resolve, reject) => { agent
axios.post(`${baseUrl}/admin/login_action`, { .post('/admin/login_action')
.send({
email: users[0].userEmail, email: users[0].userEmail,
password: 'test1' password: 'test1'
}) })
.then((response) => { .expect(400)
reject(new Error('Should not be allowed')); .end((err, res) => {
}) if(err){
.catch((error) => { t.fail();
t.deepEqual(error.response.data.message, 'Access denied. Check password and try again.'); t.end();
resolve(); }
});
t.deepEqual(res.body.message, 'Access denied. Check password and try again.');
t.end();
}); });
}); });
test('[Fail] Customer login with incorrect email', t => { test.cb('[Fail] Customer login with incorrect email', t => {
return new Promise((resolve, reject) => { agent
axios.post(`${baseUrl}/customer/login_action`, { .post('/customer/login_action')
.send({
loginEmail: 'test1@test.com', loginEmail: 'test1@test.com',
loginPassword: 'test' loginPassword: 'test'
}) })
.then((response) => { .expect(400)
reject(new Error('Should not be allowed')); .end((err, res) => {
}) if(err){
.catch((error) => { t.fail();
t.deepEqual(error.response.data.err, 'A customer with that email does not exist.'); t.end();
resolve(); }
});
t.deepEqual(res.body.message, 'A customer with that email does not exist.');
t.end();
}); });
}); });
test('[Success] Customer login with correct email', t => { test.cb('[Success] Customer login with correct email', t => {
return new Promise((resolve, reject) => { agent
axios.post(`${baseUrl}/customer/login_action`, { .post('/customer/login_action')
.send({
loginEmail: 'test@test.com', loginEmail: 'test@test.com',
loginPassword: 'test' loginPassword: 'test'
}) })
.then((response) => { .expect(200)
t.deepEqual(response.data.message, 'Successfully logged in'); .end((err, res) => {
resolve(); if(err){
}) t.fail();
.catch((error) => { t.end();
reject(new Error('Should not be allowed')); }
});
t.deepEqual(res.body.message, 'Successfully logged in');
t.end();
}); });
}); });
test('[Success] Add product to cart', t => { test.cb('[Success] Add product to cart', t => {
return new Promise((resolve, reject) => { agent
axios.post(`${baseUrl}/product/addtocart`, { .post('/product/addtocart')
.send({
productId: products[0]._id, productId: products[0]._id,
productQuantity: 1, productQuantity: 1,
productOptions: JSON.stringify(products[0].productOptions) productOptions: JSON.stringify(products[0].productOptions)
}) })
.then((response) => { .expect(200)
t.deepEqual(response.data.message, 'Cart successfully updated'); .end((err, res) => {
resolve(); if(err){
}) t.fail();
.catch((error) => { t.end();
reject(new Error('Should not be allowed')); }
});
t.deepEqual(res.body.message, 'Cart successfully updated');
t.end();
}); });
}); });
test('[Fail] Add incorrect product to cart', t => { test.cb('[Fail] Add incorrect product to cart', t => {
return new Promise((resolve, reject) => { agent
axios.post(`${baseUrl}/product/addtocart`, { .post('/product/addtocart')
productId: 'someid' .send({
id: 'fake_product_id',
state: false
}) })
.then((response) => { .expect(400)
t.deepEqual(response.data.message, 'Successfully logged in'); .end((err, res) => {
reject(new Error('Should not be allowed')); if(err){
resolve(); t.fail();
}) t.end();
.catch((error) => { }
t.deepEqual(error.response.data.message, 'Error updating cart. Please try again.'); t.deepEqual(res.body.message, 'Error updating cart. Please try again.');
resolve(); 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();
}); });
}); });