From ce4aa2820d1e6d316771174e700a57bda2b597ca Mon Sep 17 00:00:00 2001 From: Alberto Blacutt Date: Mon, 12 Feb 2024 08:48:18 -0400 Subject: [PATCH] DE-573: added e2e testing for invoice-proforma controller --- .../previewProformaInvoice.spec.ts | 71 +++++++++++ .../proformaInvoicesController.spec.ts | 110 ++++++++++++++++++ .../voidProformaInvoice.spec.ts | 74 ++++++++++++ 3 files changed, 255 insertions(+) create mode 100644 e2e/src/proformaInvoiceController/previewProformaInvoice.spec.ts create mode 100644 e2e/src/proformaInvoiceController/proformaInvoicesController.spec.ts create mode 100644 e2e/src/proformaInvoiceController/voidProformaInvoice.spec.ts diff --git a/e2e/src/proformaInvoiceController/previewProformaInvoice.spec.ts b/e2e/src/proformaInvoiceController/previewProformaInvoice.spec.ts new file mode 100644 index 00000000..6d59047f --- /dev/null +++ b/e2e/src/proformaInvoiceController/previewProformaInvoice.spec.ts @@ -0,0 +1,71 @@ +import { Environment, ProformaInvoicesController } from 'advanced-billing-sdk'; +import { createClient, CONFIG } from '../config'; +import { createSubscription } from '../utils/subscription'; + +describe('Proforma Invoices Controller', () => { + let proformaInvoicesController: ProformaInvoicesController; + let invalidProformaInvoiceController: ProformaInvoicesController; + + beforeAll(async () => { + const client = createClient(); + const invalidClient = createClient({ + timeout: 0, + domain: CONFIG.DOMAIN, + environment: Environment.Production, + subdomain: CONFIG.SUBDOMAIN, + basicAuthUserName: 'invalidKey', + basicAuthPassword: CONFIG.PASSWORD, + }); + proformaInvoicesController = new ProformaInvoicesController(client); + invalidProformaInvoiceController = new ProformaInvoicesController( + invalidClient + ); + }); + + describe('Preview Proforma Invoice', () => { + test('should preview a proforma invoice', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-01-preview-proforma', + productHandle: 'scenario-01-preview-proforma-handler', + customerReference: 'scenario-01-preview-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + await proformaInvoicesController.createProformaInvoice(subscriptId || 0); + + const previewResponse = + await proformaInvoicesController.previewProformaInvoice( + subscriptId || 0 + ); + expect(previewResponse.statusCode).toBe(200); + expect(previewResponse.result.subscriptionId).toBe(subscriptId); + }); + + test('should throw a 404 error when the user sends invalid subscription_id', async () => { + const promise = proformaInvoicesController.previewProformaInvoice(0); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(404); + }); + }); + + test('should throw a 401 error when the user sends incorrect credentials', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-02-preview-proforma', + productHandle: 'scenario-02-preview-proforma-handler', + customerReference: 'scenario-02-preview-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + const promise = invalidProformaInvoiceController.previewProformaInvoice( + subscriptId || 0 + ); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(401); + }); + }); + }); +}); diff --git a/e2e/src/proformaInvoiceController/proformaInvoicesController.spec.ts b/e2e/src/proformaInvoiceController/proformaInvoicesController.spec.ts new file mode 100644 index 00000000..1b15fc43 --- /dev/null +++ b/e2e/src/proformaInvoiceController/proformaInvoicesController.spec.ts @@ -0,0 +1,110 @@ +import { ProformaInvoicesController } from 'advanced-billing-sdk'; +import { createClient } from '../config'; +import { createSubscription } from '../utils/subscription'; + +describe('Proforma Invoices Controller', () => { + let proformaInvoicesController: ProformaInvoicesController; + + beforeAll(async () => { + const client = createClient(); + proformaInvoicesController = new ProformaInvoicesController(client); + }); + + describe('Create Proforma Invoices', () => { + test('should create a valid proforma invoice', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-01-proforma', + productHandle: 'scenario-01-proforma-handler', + customerReference: 'scenario-01-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + const createResponse = + await proformaInvoicesController.createProformaInvoice( + subscriptId || 0 + ); + expect(createResponse.statusCode).toBe(201); + expect(createResponse.result.subscriptionId).toBe(subscriptId); + }); + + test('should throw a 404 error when the user sends invalid subscription_id', async () => { + const promise = proformaInvoicesController.createProformaInvoice(0); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(404); + }); + }); + }); + + describe('Read Proforma Invoices', () => { + test('should read a valid proforma invoice', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-02-read-proforma', + productHandle: 'scenario-02-read-proforma-handler', + customerReference: 'scenario-02-read-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + const createResponse = + await proformaInvoicesController.createProformaInvoice( + subscriptId || 0 + ); + + const proformaUid = createResponse.result.uid; + const readResponse = await proformaInvoicesController.readProformaInvoice( + proformaUid || '' + ); + expect(readResponse.statusCode).toBe(200); + expect(readResponse.result.uid).toBe(proformaUid); + }); + + test('should throw a 404 error when the user sends a non-existent proforma invoice UID', async () => { + const promise = proformaInvoicesController.readProformaInvoice('test'); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(404); + }); + }); + }); + + describe('List Proforma invoices', () => { + test('should list proforma invoices when user sends valid subscriptionId', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-04-list-proforma', + productHandle: 'scenario-04-list-proforma-handler', + customerReference: 'scenario-04-list-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + await proformaInvoicesController.createProformaInvoice(subscriptId || 0); + const listResponse = + await proformaInvoicesController.listProformaInvoices({ + subscriptionId: subscriptId || 0, + }); + const proformaInvoicesLength = + listResponse?.result?.proformaInvoices?.length || 0; + expect(listResponse.statusCode).toBe(200); + expect(proformaInvoicesLength >= 1).toBeTruthy(); + }); + + test('should throw a 404 error when the request body is missing', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-05-list-proforma', + productHandle: 'scenario-05-list-proforma-handler', + customerReference: 'scenario-05-list-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + await proformaInvoicesController.createProformaInvoice(subscriptId || 0); + const promise = proformaInvoicesController.listProformaInvoices({ + subscriptionId: 0, + }); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(404); + }); + }); + }); +}); diff --git a/e2e/src/proformaInvoiceController/voidProformaInvoice.spec.ts b/e2e/src/proformaInvoiceController/voidProformaInvoice.spec.ts new file mode 100644 index 00000000..734a4900 --- /dev/null +++ b/e2e/src/proformaInvoiceController/voidProformaInvoice.spec.ts @@ -0,0 +1,74 @@ +import { ProformaInvoicesController } from 'advanced-billing-sdk'; +import { createClient } from '../config'; +import { createSubscription } from '../utils/subscription'; + +describe('Proforma Invoices Controller', () => { + let proformaInvoicesController: ProformaInvoicesController; + + beforeAll(async () => { + const client = createClient(); + proformaInvoicesController = new ProformaInvoicesController(client); + }); + + describe('Void Proforma Invoice', () => { + test('should void a proforma invoice', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-01-void-proforma', + productHandle: 'scenario-01-void-proforma-handler', + customerReference: 'scenario-01-void-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + const createResponse = + await proformaInvoicesController.createProformaInvoice( + subscriptId || 0 + ); + + const proformaUid = createResponse.result.uid; + const voidResponse = await proformaInvoicesController.voidProformaInvoice( + proformaUid || '', + { + mVoid: { + reason: 'Duplicate proforma invoice', + }, + } + ); + expect(voidResponse.statusCode).toBe(201); + expect(voidResponse.result.uid).toBe(proformaUid); + }); + + test('should throw a 404 error when the user sends a non-existent proforma invoice UID', async () => { + const promise = proformaInvoicesController.voidProformaInvoice(''); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(404); + }); + }); + + test('should throw a 422 error when the user does not provide reason in the request body.', async () => { + const subsResponse = await createSubscription({ + productFamilyName: 'scenario-03-void-proforma', + productHandle: 'scenario-03-void-proforma-handler', + customerReference: 'scenario-03-void-proforma-reference', + }); + + const subscriptId = subsResponse.subscriptionResponse?.subscription?.id; + + const createResponse = + await proformaInvoicesController.createProformaInvoice( + subscriptId || 0 + ); + + const proformaUid = createResponse.result.uid; + const promise = proformaInvoicesController.voidProformaInvoice( + proformaUid || '' + ); + expect(promise).rejects.toThrow(); + await promise.catch((reason) => { + expect(reason.statusCode).toBe(422); + expect(reason.result.errors).toEqual({ void: "can't be blank" }); + }); + }); + }); +});