expressCart/test/helper.js

147 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-11-09 12:14:56 +10:00
const fs = require('fs');
const _ = require('lodash');
const moment = require('moment');
const supertest = require('supertest');
2019-11-09 12:14:56 +10:00
const app = require('../app.js');
2019-11-09 15:20:47 +10:00
const { newId } = require('../lib/common');
2019-11-09 15:08:05 +10:00
const { runIndexing } = require('../lib/indexing');
2019-11-09 12:14:56 +10:00
// Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
const jsonData = JSON.parse(rawTestData);
// Setup some global DB objects for comparison
const g = {
db: {},
config: {},
products: {},
discounts: {},
2019-11-09 12:14:56 +10:00
customers: {},
users: {},
request: null,
jsonData
};
const setup = (db) => {
return Promise.all([
db.cart.deleteMany({}, {}),
db.users.deleteMany({}, {}),
db.customers.deleteMany({}, {}),
db.products.deleteMany({}, {}),
db.discounts.deleteMany({}, {}),
db.orders.deleteMany({}, {}),
db.sessions.deleteMany({}, {})
2019-11-09 12:14:56 +10:00
])
.then(() => {
return Promise.all([
2019-11-09 15:08:05 +10:00
db.users.insertMany(addApiKey(jsonData.users)),
2019-11-09 12:14:56 +10:00
db.customers.insertMany(jsonData.customers),
db.products.insertMany(fixProductDates(jsonData.products)),
db.discounts.insertMany(fixDiscountDates(jsonData.discounts))
2019-11-09 12:14:56 +10:00
]);
});
};
const runBefore = async () => {
// Create a session
g.request = supertest.agent(app);
2019-11-09 12:14:56 +10:00
await new Promise(resolve => {
app.on('appStarted', async () => {
// Set some stuff now we have the app started
g.config = app.config;
g.db = app.db;
await setup(g.db);
// Get some data from DB to use in compares
g.products = await g.db.products.find({}).toArray();
g.customers = await g.db.customers.find({}).toArray();
g.discounts = await g.db.discounts.find({}).toArray();
2019-11-09 15:18:13 +10:00
g.users = await g.db.users.find({}).toArray();
2019-11-09 12:14:56 +10:00
// Insert orders using product ID's
_(jsonData.orders).each(async (order) => {
order.orderProducts.push({
productId: g.products[0]._id,
title: g.products[0].productTitle,
quantity: 1,
totalItemPrice: g.products[0].productPrice,
options: {
size: '7.5'
},
productImage: g.products[0].productImage,
productComment: null
});
order.orderDate = new Date();
await g.db.orders.insertOne(order);
});
2020-02-23 13:40:35 +10:00
// Get csrf token
const csrf = await g.request
.get('/admin/csrf');
g.csrf = csrf.body.csrf;
2019-11-09 12:14:56 +10:00
// Index everything
await runIndexing(app);
resolve();
});
});
};
2019-11-09 15:08:05 +10:00
const fixProductDates = (products) => {
let index = 0;
products.forEach(() => {
products[index].productAddedDate = new Date();
index++;
});
return products;
};
const fixDiscountDates = (discounts) => {
let index = 0;
discounts.forEach(() => {
let startDate = moment().subtract(1, 'days').toDate();
let endDate = moment().add(7, 'days').toDate();
const expiredStart = moment().subtract(14, 'days').toDate();
const expiredEnd = moment().subtract(7, 'days').toDate();
const futureStart = moment().add(7, 'days').toDate();
const futureEnd = moment().add(14, 'days').toDate();
// If code is expired, make sure the dates are correct
if(discounts[index].code.substring(0, 7) === 'expired'){
startDate = expiredStart;
endDate = expiredEnd;
}
// If code is future, make sure the dates are correct
if(discounts[index].code.substring(0, 6) === 'future'){
startDate = futureStart;
endDate = futureEnd;
}
// Set the expiry dates
discounts[index].start = startDate;
discounts[index].end = endDate;
index++;
});
return discounts;
};
2019-11-09 15:08:05 +10:00
const addApiKey = (users) => {
let index = 0;
users.forEach(() => {
users[index].apiKey = newId();
index++;
});
return users;
};
2019-11-09 12:14:56 +10:00
module.exports = {
runBefore,
setup,
2019-11-09 15:08:05 +10:00
g,
fixProductDates,
fixDiscountDates
2019-11-09 12:14:56 +10:00
};