Skip to content

Commit

Permalink
DE-554 - DE-596 Added e2e testing for create and list customers (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Alberto Blacutt <[email protected]>
  • Loading branch information
alberto-blacutt-maxio and Alberto Blacutt authored Nov 2, 2023
1 parent 701ce73 commit c361e7f
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 1 deletion.
231 changes: 231 additions & 0 deletions src/e2e/customersController.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import { createClient } from '../config'
import { CustomersController } from '../controllers/customersController';
import { CustomerResponse } from '../models/customerResponse';
import { cleanSite } from './utils';

describe("CustomersController", () => {
describe('Create customer', () => {

afterAll(async () => {
await cleanSite();
});

test("should create a customer given the correct payload body", async () => {
const client = createClient();
const customersController = new CustomersController(client);
const body = {
customer: {
firstName: "Martha",
lastName: "Washington",
email: "[email protected]",
}
}
const response = await customersController.createCustomer(body);
expect(response.statusCode).toBe(201);
expect(response.result.customer).toEqual(expect.objectContaining(body.customer));
});

test("should throw errors when required parameters was not sent to the body payload", async () => {
const client = createClient();
const customersController = new CustomersController(client);
const body = {
customer: {
firstName: "",
lastName: "",
email: "",
}
}
const promise = customersController.createCustomer(body);
expect(promise).rejects.toThrow()
const expectedErrors = [
'First name: cannot be blank.',
'Last name: cannot be blank.',
'Email address: cannot be blank.'
]
await promise.catch(response => {
expect(response.result.errors).toEqual(expectedErrors);
expect(response.statusCode).toEqual(422);

});
})

test("should throw error when the user try to create a customer with the same reference value", async () => {
const client = createClient();
const customersController = new CustomersController(client);
const body = {
customer: {
firstName: "Pedro",
lastName: "Lopez",
email: "[email protected]",
ccEmails: "[email protected]",
organization: "ABC, Inc.",
reference: "1033",
address: "123 Main Street",
address2: "Unit 10",
city: "Anytown",
state: "MA",
zip: "02120",
country: "US",
phone: "555-555-1212",
locale: "es-MX"
}
}

const bodyWithTheSameReference = {
customer: {
firstName: "Juan",
lastName: "Lopez",
email: "[email protected]",
ccEmails: "[email protected]",
organization: "ABC, Inc.",
reference: "1033",
address: "123 Main Street",
address2: "Unit 10",
city: "Anytown",
state: "MA",
zip: "02120",
country: "US",
phone: "555-555-1212",
locale: "es-MX"
}
}
const response = await customersController.createCustomer(body);
expect(response.statusCode).toBe(201);
const promise = customersController.createCustomer(bodyWithTheSameReference);
expect(promise).rejects.toThrow()
const expectedErrors = [
'Reference: must be unique - that value has been taken.',
]
await promise.catch(response => {
expect(response.result.errors).toEqual(expectedErrors);
expect(response.statusCode).toEqual(422);

});
})
})

describe('List/Find customer', () => {
afterAll(async () => {
await cleanSite();
})

beforeAll(async () => {
const client = createClient();
const customersController = new CustomersController(client);
const customers = [{
firstName: "Ferdinand",
lastName: "Lopez",
email: "[email protected]",
organization: "ABC, Inc.",
reference: '10001'

}, {
firstName: "Fernando",
lastName: "Pedregal",
email: "[email protected]",
organization: "Holand, Inc.",
reference: '10002'
},
{
firstName: "Solivan",
lastName: "Medrano",
email: "[email protected]",
organization: "ABC, Inc.",
reference: '10003'
},
{
firstName: "Edward",
lastName: "Kalua",
email: "[email protected]",
organization: "CPD, inc",
reference: '10004'
},
{
firstName: "Miguel",
lastName: "Santos",
email: "[email protected]",
organization: "CPD, inc",
reference: '10005'
},
{
firstName: "Henrique",
lastName: "Figeroa",
email: "[email protected]",
organization: "CPD, inc",
reference: '10006'
}]

const promises = customers.map(customer => {
return customersController.createCustomer({ customer })
})
await Promise.all(promises);
})

test('should get all customers list when the user send empty query criteria', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const response = await customersController.listCustomers()
const responseEmails = response.result.map((value: CustomerResponse) => value.customer.email).sort();
expect(response.result.length).toBe(6)
expect(responseEmails).toEqual([
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
])
})

test('should get filtered customers list when the user send a query criteria', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const response = await customersController.listCustomers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 'nando')
const responseEmails = response.result.map((value: CustomerResponse) => value.customer.email).sort();
expect(response.result.length).toBe(2)
expect(responseEmails).toEqual([
'[email protected]',
'[email protected]',
])
})

test('should get empty customers list when the user query criteria was not found in data', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const response = await customersController.listCustomers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 'invalid-criteria')
expect(response.result.length).toBe(0)
})

test('should get customers list based on organization criteria when the user query criteria search by ABC organization', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const response = await customersController.listCustomers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 'ABC')
expect(response.result.length).toBe(2)
const responseEmails = response.result.map((value: CustomerResponse) => value.customer.email).sort();
expect(responseEmails.includes('[email protected]')).toBeTruthy();
expect(responseEmails.includes('[email protected]')).toBeTruthy();
expect(responseEmails).toEqual([
'[email protected]',
'[email protected]',
])
})

test('should get customers list based on reference application criteria when the user search by reference', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const response = await customersController.listCustomers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, '10001')
expect(response.result.length).toBe(1)
const responseEmails = response.result.map((value: CustomerResponse) => value.customer.email);
expect(responseEmails).toEqual(['[email protected]'])
})

test('should get customers list based on page size of 5 when the user gets the first page', async () => {
const client = createClient();
const customersController = new CustomersController(client);
const firstPageResponse = await customersController.listCustomers(undefined, 1, 5, undefined, undefined, undefined, undefined, undefined)
const secondPageResponse = await customersController.listCustomers(undefined, 2, 5, undefined, undefined, undefined, undefined, undefined)
expect(firstPageResponse.result.length).toBe(5)
expect(secondPageResponse.result.length).toBe(1)
})
})
});
2 changes: 1 addition & 1 deletion src/e2e/sitesController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("SitesController", () => {
taxConfiguration: {
kind: 'custom',
destinationAddress: 'shipping_then_billing',
fullyConfigured: true
fullyConfigured: false
},
netTerms: {
defaultNetTerms: 0,
Expand Down
20 changes: 20 additions & 0 deletions src/e2e/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createClient } from '../../config'
import { SitesController } from '../../controllers/sitesController';


const DELAY_AFTER_CLEAN = 1000;
// Based on https://developers.chargify.com/docs/api-docs/c912e634019c9-clear-site-data
// doc recommendation added small delay a site verification or read retry command
export async function cleanSite() {
return new Promise((resolve, reject) => {
const client = createClient();
const sitesController = new SitesController(client);
sitesController.clearSite()
.then(response => {
setTimeout(() => {
resolve(response)
}, DELAY_AFTER_CLEAN);
})
.catch(error => reject(error))
});
}

0 comments on commit c361e7f

Please sign in to comment.