2019-11-09 12:14:56 +10:00
|
|
|
import{ serial as test }from'ava';
|
|
|
|
const {
|
|
|
|
runBefore,
|
|
|
|
g
|
|
|
|
} = require('../helper');
|
|
|
|
|
|
|
|
test.before(async () => {
|
|
|
|
await runBefore();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('[Success] Create a customer', async t => {
|
|
|
|
const customer = {
|
|
|
|
email: 'sarah.jones@test.com',
|
|
|
|
firstName: 'Sarah',
|
|
|
|
lastName: 'Jones',
|
|
|
|
address1: '1 Sydney Street',
|
|
|
|
address2: '',
|
|
|
|
country: 'Australia',
|
|
|
|
state: 'NSW',
|
|
|
|
postcode: '2000',
|
|
|
|
phone: '0400000000',
|
|
|
|
password: 'password'
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await g.request
|
|
|
|
.post('/customer/create')
|
|
|
|
.send(customer)
|
|
|
|
.expect(200);
|
|
|
|
|
2019-11-16 09:02:15 +10:00
|
|
|
t.deepEqual(res.body.email, customer.email);
|
|
|
|
t.deepEqual(res.body.firstName, customer.firstName);
|
2019-11-09 12:14:56 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
test('[Fail] Try create a duplicate customer', async t => {
|
|
|
|
const customer = {
|
|
|
|
email: 'sarah.jones@test.com',
|
|
|
|
firstName: 'Sarah',
|
|
|
|
lastName: 'Jones',
|
|
|
|
address1: '1 Sydney Street',
|
|
|
|
address2: '',
|
|
|
|
country: 'Australia',
|
|
|
|
state: 'NSW',
|
|
|
|
postcode: '2000',
|
|
|
|
phone: '0400000000',
|
|
|
|
password: 'password'
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await g.request
|
|
|
|
.post('/customer/create')
|
|
|
|
.send(customer)
|
|
|
|
.expect(400);
|
|
|
|
|
2019-11-23 08:35:19 +10:00
|
|
|
t.deepEqual(res.body.message, 'A customer already exists with that email address');
|
2019-11-09 12:14:56 +10:00
|
|
|
});
|
|
|
|
|
2019-11-16 20:57:48 +10:00
|
|
|
test('[Fail] Create with invalid email address', async t => {
|
2019-11-16 12:23:25 +10:00
|
|
|
const customer = {
|
|
|
|
email: 'sarah.jones@test',
|
|
|
|
firstName: 'Sarah',
|
|
|
|
lastName: 'Jones',
|
|
|
|
address1: '1 Sydney Street',
|
|
|
|
address2: '',
|
|
|
|
country: 'Australia',
|
|
|
|
state: 'NSW',
|
|
|
|
postcode: '2000',
|
|
|
|
phone: '0400000000',
|
|
|
|
password: 'password'
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await g.request
|
|
|
|
.post('/customer/create')
|
|
|
|
.send(customer)
|
|
|
|
.expect(400);
|
|
|
|
|
|
|
|
t.deepEqual(res.body[0].message, 'should match format "emailAddress"');
|
|
|
|
});
|
|
|
|
|
2019-11-16 20:57:48 +10:00
|
|
|
test('[Success] Update existing customer', async t => {
|
|
|
|
const customer = {
|
|
|
|
customerId: g.customers[1]._id,
|
|
|
|
email: 'sarah.jones@test.com',
|
|
|
|
firstName: 'Sarah',
|
|
|
|
lastName: 'Jones',
|
|
|
|
address1: '1 Sydney Street',
|
|
|
|
address2: '',
|
|
|
|
country: 'Australia',
|
|
|
|
state: 'NSW',
|
|
|
|
postcode: '2000',
|
|
|
|
phone: '0444444444'
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await g.request
|
|
|
|
.post('/admin/customer/update')
|
|
|
|
.send(customer)
|
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
t.deepEqual(res.body.message, 'Customer updated');
|
|
|
|
t.deepEqual(res.body.customer.email, customer.email);
|
|
|
|
t.deepEqual(res.body.customer.firstName, customer.firstName);
|
|
|
|
t.deepEqual(res.body.customer.lastName, customer.lastName);
|
|
|
|
t.deepEqual(res.body.customer.address1, customer.address1);
|
|
|
|
t.deepEqual(res.body.customer.country, customer.country);
|
|
|
|
t.deepEqual(res.body.customer.state, customer.state);
|
|
|
|
t.deepEqual(res.body.customer.postcode, customer.postcode);
|
|
|
|
t.deepEqual(res.body.customer.phone, customer.phone);
|
|
|
|
});
|
|
|
|
|
2019-11-09 12:14:56 +10:00
|
|
|
test('[Success] Get customer list', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.get('/admin/customers')
|
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
// Check the returned customers length
|
2019-11-16 20:57:48 +10:00
|
|
|
t.deepEqual(3, res.body.length);
|
2019-11-09 12:14:56 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
test('[Success] Filter customers', async t => {
|
|
|
|
const res = await g.request
|
2019-11-16 21:05:02 +10:00
|
|
|
.get('/admin/customers/filter/Testy')
|
2019-11-09 12:14:56 +10:00
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
// Check the returned customers length
|
2019-11-16 21:05:02 +10:00
|
|
|
t.deepEqual(1, res.body.customers.length);
|
2019-11-09 12:14:56 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
test('[Success] Get single customer', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.get('/admin/customer/view/' + g.customers[0]._id)
|
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
// Check the returned customer matches ID
|
|
|
|
t.deepEqual(g.customers[0]._id.toString(), res.body._id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('[Fail] Customer login with incorrect email', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.post('/customer/login_action')
|
|
|
|
.send({
|
2019-11-16 20:57:48 +10:00
|
|
|
loginEmail: 'test1111@test.com',
|
2019-11-09 12:14:56 +10:00
|
|
|
loginPassword: 'test'
|
|
|
|
})
|
|
|
|
.expect(400);
|
|
|
|
t.deepEqual(res.body.message, 'A customer with that email does not exist.');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('[Success] Customer login with correct email', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.post('/customer/login_action')
|
|
|
|
.send({
|
|
|
|
loginEmail: g.customers[0].email,
|
|
|
|
loginPassword: 'test'
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
t.deepEqual(res.body.message, 'Successfully logged in');
|
|
|
|
});
|
2019-11-23 08:35:19 +10:00
|
|
|
|
|
|
|
test('[Success] Delete a customer', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.delete('/admin/customer')
|
|
|
|
.send({
|
|
|
|
customerId: g.customers[0]._id
|
|
|
|
})
|
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(200);
|
|
|
|
t.deepEqual(res.body.message, 'Customer deleted');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('[Success] Failed deleting an incorrect customer', async t => {
|
|
|
|
const res = await g.request
|
|
|
|
.delete('/admin/customer')
|
|
|
|
.send({
|
|
|
|
customerId: g.customers[0]._id
|
|
|
|
})
|
|
|
|
.set('apiKey', g.users[0].apiKey)
|
|
|
|
.expect(400);
|
|
|
|
t.deepEqual(res.body.message, 'Failed to delete customer. Customer not found');
|
|
|
|
});
|