From c361e7f0ee6e24a1c0651dd96d7a4f55cb9937a0 Mon Sep 17 00:00:00 2001 From: alberto-blacutt-maxio <148458573+alberto-blacutt-maxio@users.noreply.github.com> Date: Thu, 2 Nov 2023 19:34:26 +0000 Subject: [PATCH] DE-554 - DE-596 Added e2e testing for create and list customers (#4) Co-authored-by: Alberto Blacutt --- src/e2e/customersController.spec.ts | 231 ++++++++++++++++++++++++++++ src/e2e/sitesController.spec.ts | 2 +- src/e2e/utils/index.ts | 20 +++ 3 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/e2e/customersController.spec.ts create mode 100644 src/e2e/utils/index.ts diff --git a/src/e2e/customersController.spec.ts b/src/e2e/customersController.spec.ts new file mode 100644 index 00000000..cd158576 --- /dev/null +++ b/src/e2e/customersController.spec.ts @@ -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: "martha@example.com", + } + } + 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: "pedro@example.com", + ccEmails: "pedro@example.com", + 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: "Juan@example.com", + ccEmails: "Juan@example.com", + 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: "fernando@example.com", + organization: "ABC, Inc.", + reference: '10001' + + }, { + firstName: "Fernando", + lastName: "Pedregal", + email: "nando@example.com", + organization: "Holand, Inc.", + reference: '10002' + }, + { + firstName: "Solivan", + lastName: "Medrano", + email: "soli@example.com", + organization: "ABC, Inc.", + reference: '10003' + }, + { + firstName: "Edward", + lastName: "Kalua", + email: "kalua@example.com", + organization: "CPD, inc", + reference: '10004' + }, + { + firstName: "Miguel", + lastName: "Santos", + email: "santos@example.com", + organization: "CPD, inc", + reference: '10005' + }, + { + firstName: "Henrique", + lastName: "Figeroa", + email: "figeroa@example.com", + 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([ + 'fernando@example.com', + 'figeroa@example.com', + 'kalua@example.com', + 'nando@example.com', + 'santos@example.com', + 'soli@example.com' + ]) + }) + + 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([ + 'fernando@example.com', + 'nando@example.com', + ]) + }) + + 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('fernando@example.com')).toBeTruthy(); + expect(responseEmails.includes('soli@example.com')).toBeTruthy(); + expect(responseEmails).toEqual([ + 'fernando@example.com', + 'soli@example.com', + ]) + }) + + 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(['fernando@example.com']) + }) + + 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) + }) + }) +}); \ No newline at end of file diff --git a/src/e2e/sitesController.spec.ts b/src/e2e/sitesController.spec.ts index 60f48593..292d2efa 100644 --- a/src/e2e/sitesController.spec.ts +++ b/src/e2e/sitesController.spec.ts @@ -37,7 +37,7 @@ describe("SitesController", () => { taxConfiguration: { kind: 'custom', destinationAddress: 'shipping_then_billing', - fullyConfigured: true + fullyConfigured: false }, netTerms: { defaultNetTerms: 0, diff --git a/src/e2e/utils/index.ts b/src/e2e/utils/index.ts new file mode 100644 index 00000000..75cb407c --- /dev/null +++ b/src/e2e/utils/index.ts @@ -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)) + }); +} \ No newline at end of file