Adding APIkey and order tests

master
Mark Moffat 2019-06-15 11:23:14 +09:30
parent 92e4d7c047
commit 113bfe05cc
2 changed files with 103 additions and 5 deletions

View File

@ -100,6 +100,44 @@
"isAdmin" : true "isAdmin" : true
} }
], ],
"orders": [
{
"orderPaymentId" : "ch_1ElPw5L7TBqK1az83hbmOFuJ",
"orderPaymentGateway" : "Stripe",
"orderPaymentMessage" : "Payment complete.",
"orderTotal" : 310,
"orderEmail" : "test@test.com",
"orderFirstname" : "Testy",
"orderLastname" : "Cles",
"orderAddr1" : "1 test street",
"orderAddr2" : "testvile",
"orderCountry" : "Netherlands",
"orderState" : "SA",
"orderPostcode" : "2000TW",
"orderPhoneNumber" : "123456789",
"orderComment" : "",
"orderStatus" : "Paid",
"orderProducts" : []
},
{
"orderPaymentId" : "ch_1ElPw5L7TBqK1az83hbmOFuJ",
"orderPaymentGateway" : "Stripe",
"orderPaymentMessage" : "Payment complete.",
"orderTotal" : 310,
"orderEmail" : "test@test.com",
"orderFirstname" : "Testy",
"orderLastname" : "Cles",
"orderAddr1" : "1 test street",
"orderAddr2" : "testvile",
"orderCountry" : "Netherlands",
"orderState" : "SA",
"orderPostcode" : "2000TW",
"orderPhoneNumber" : "123456789",
"orderComment" : "",
"orderStatus" : "Declined",
"orderProducts" : []
}
],
"menu": { "menu": {
"items": [ "items": [
{ {

View File

@ -1,5 +1,6 @@
const test = require('ava'); const test = require('ava');
const fs = require('fs'); const fs = require('fs');
const _ = require('lodash');
const app = require('../app'); const app = require('../app');
const common = require('../lib/common'); const common = require('../lib/common');
const session = require('supertest-session'); const session = require('supertest-session');
@ -14,22 +15,22 @@ let config;
let products; let products;
let customers; let customers;
let users; let users;
let orders;
let request = null; let request = null;
function setup(db, app){ function setup(db){
return Promise.all([ return Promise.all([
db.cart.remove({}, {}), db.cart.remove({}, {}),
db.users.remove({}, {}), db.users.remove({}, {}),
db.customers.remove({}, {}), db.customers.remove({}, {}),
db.products.remove({}, {}), db.products.remove({}, {}),
db.menu.remove({}, {}) db.orders.remove({}, {})
]) ])
.then(() => { .then(() => {
return Promise.all([ return Promise.all([
db.users.insertMany(jsonData.users), db.users.insertMany(jsonData.users),
db.customers.insertMany(jsonData.customers), db.customers.insertMany(jsonData.customers),
db.products.insertMany(common.fixProductDates(jsonData.products)), db.products.insertMany(common.fixProductDates(jsonData.products))
db.menu.insertOne(jsonData.menu)
]); ]);
}); });
} }
@ -44,13 +45,33 @@ test.before(async () => {
config = app.config; config = app.config;
db = app.db; db = app.db;
await setup(db, app); await setup(db);
await common.runIndexing(app); await common.runIndexing(app);
// Get some data from DB to use in compares // Get some data from DB to use in compares
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();
// Insert orders using product ID's
_(jsonData.orders).each(async (order) => {
order.orderProducts.push({
productId: products[0]._id,
title: products[0].productTitle,
quantity: 1,
totalItemPrice: products[0].productPrice,
options: {
size: '7.5'
},
productImage: products[0].productImage,
productComment: null
});
order.orderDate = new Date();
await db.orders.insert(order);
});
// Get orders
orders = await db.orders.find({}).toArray();
resolve(); resolve();
}); });
}); });
@ -78,6 +99,16 @@ test.serial('[Success] User Login', async t => {
t.deepEqual(res.body.message, 'Login successful'); t.deepEqual(res.body.message, 'Login successful');
}); });
test.serial('[Success] Create API key', async t => {
const res = await request
.post('/admin/createApiKey')
.expect(200);
users[0].apiKey = res.body.apiKey;
t.deepEqual(res.body.message, 'API Key generated');
t.deepEqual(res.body.apiKey.length, 24);
});
test.serial('[Fail] Incorrect user password', async t => { test.serial('[Fail] Incorrect user password', async t => {
const res = await request const res = await request
.post('/admin/login_action') .post('/admin/login_action')
@ -214,6 +245,35 @@ test.serial('[Success] Create a customer', async t => {
t.deepEqual(res.body.message, 'Successfully logged in'); t.deepEqual(res.body.message, 'Successfully logged in');
}); });
test.serial('[Success] Get orders', async t => {
const res = await request
.get('/admin/orders')
.set('apiKey', users[0].apiKey)
.expect(200);
// Check the returned order length
t.deepEqual(orders.length, res.body.orders.length);
});
test.serial('[Fail] Try get orderes with a bogus apiKey', async t => {
const res = await request
.get('/admin/orders')
.set('apiKey', '123456789012345678901234')
.expect(400);
t.deepEqual(res.body.message, 'Access denied');
});
test.serial('[Success] Get orders by <Paid> status', async t => {
const res = await request
.get('/admin/orders/bystatus/Paid')
.set('apiKey', users[0].apiKey)
.expect(200);
// Check the returned order length
t.deepEqual(1, res.body.orders.length);
});
test.serial('[Fail] Try create a duplicate customer', async t => { test.serial('[Fail] Try create a duplicate customer', async t => {
const customer = { const customer = {
email: 'sarah.jones@test.com', email: 'sarah.jones@test.com',