Fixed tests

master
Mark Moffat 2019-06-09 22:02:28 +09:30
parent 01cd784a0e
commit aeb6a8e05e
1 changed files with 64 additions and 125 deletions

View File

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