Skip to content

Commit

Permalink
DE-675 e2e tests for proforma invoices part 2 (#61)
Browse files Browse the repository at this point in the history
* DE-573: added e2e testing for invoice-proforma controller

* DE-675 added e2e tests for proforma-invoice consolidate, preview, signUp

* DE-675 added e2e tests for consolidate proforma and list proforma invoices

---------

Co-authored-by: Alberto Blacutt <[email protected]>
  • Loading branch information
alberto-blacutt-maxio and Alberto Blacutt authored Feb 26, 2024
1 parent 487afa6 commit a27e408
Show file tree
Hide file tree
Showing 7 changed files with 519 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { ProformaInvoicesController } from 'advanced-billing-sdk';
import { createClient, createInvalidClient } from '../config';
import { signUpSubscriptionGroup } from '../utils/subscriptionsGroups';
import { waitForSeconds } from '../utils';

describe('Proforma Invoices Controller', () => {
let proformaInvoicesController: ProformaInvoicesController;
let invalidProformaInvoicesController: ProformaInvoicesController;

beforeAll(async () => {
const client = createClient();
const invalidClient = createInvalidClient();
proformaInvoicesController = new ProformaInvoicesController(client);
invalidProformaInvoicesController = new ProformaInvoicesController(
invalidClient
);
});

describe('Create Consolidate Proforma Invoice', () => {
test('should create consolidate proforma invoice with a success status code', async () => {
const { subscriptionGroupsSignedResponse } =
await signUpSubscriptionGroup();
const uid = subscriptionGroupsSignedResponse.uid || '';
const response =
await proformaInvoicesController.createConsolidatedProformaInvoice(uid);
expect(response.statusCode).toBe(201);
});

test('should throw 404 error when create consolidate proforma invoice with an invalid uid', async () => {
const promise =
proformaInvoicesController.createConsolidatedProformaInvoice('invalid');
expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(404);
});
});

test('should throw 401 error when create consolidate proforma invoice with an invalid client', async () => {
const promise =
invalidProformaInvoicesController.createConsolidatedProformaInvoice(
'invalid'
);
expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(401);
});
});
});

describe('List Subscription Group Proforma Invoices', () => {
test('should list subscription group proforma invoices already created for the subscription group', async () => {
const { subscriptionGroupsSignedResponse, customerResponse } =
await signUpSubscriptionGroup();
const uid = subscriptionGroupsSignedResponse.uid || '';
await proformaInvoicesController.createConsolidatedProformaInvoice(uid);

// required to wait async creation on the service
await waitForSeconds(5);

const listResponse =
await proformaInvoicesController.listSubscriptionGroupProformaInvoices(
uid
);

const [proformaInvoice] = listResponse.result.proformaInvoices || [];
expect(listResponse.statusCode).toBe(200);
expect(listResponse.result.proformaInvoices?.length).toBeGreaterThan(0);
expect(proformaInvoice.siteId).toBe(4511);
expect(proformaInvoice.customerId).toBe(customerResponse.customer.id);
});

test('should throw 404 when listSubscriptionGroupProformaInvoices with an invalid uid', async () => {
const promise =
proformaInvoicesController.listSubscriptionGroupProformaInvoices(
'invalid'
);
expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(404);
});
});

test('should throw 404 when listSubscriptionGroupProformaInvoices with an invalid client', async () => {
const promise =
invalidProformaInvoicesController.listSubscriptionGroupProformaInvoices(
'invalid'
);
expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(401);
});
});
});
});
134 changes: 134 additions & 0 deletions e2e/src/proformaInvoiceController/previewSignUpProformaInvoice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { ProformaInvoicesController } from 'advanced-billing-sdk';
import { createClient, createInvalidClient } from '../config';
import { uid } from 'uid';
import createProduct from '../utils/products';
import { createCustomer } from '../utils/customers';

describe('Create a Preview Sign Up Proforma Invoice', () => {
let proformaInvoicesController: ProformaInvoicesController;
let invalidProformaInvoicesController: ProformaInvoicesController;

beforeAll(async () => {
const client = createClient();
const invalidClient = createInvalidClient();
proformaInvoicesController = new ProformaInvoicesController(client);
invalidProformaInvoicesController = new ProformaInvoicesController(
invalidClient
);
});

test('should create a preview signup proforma invoice ', async () => {
const productHandle = uid();
const { productResponse, productFamilyResponse } = await createProduct({
productHandle,
});
const customer = await createCustomer();

const response =
await proformaInvoicesController.previewSignupProformaInvoice(undefined, {
subscription: {
productHandle,
customerReference: uid(),
customerId: customer.id,
},
});

const { currentProformaInvoice } = response.result.proformaInvoicePreview;
expect(response.statusCode).toBe(201);
expect(currentProformaInvoice?.customerId).toBe(customer.id);

expect(currentProformaInvoice?.productName).toBe(
productResponse.product.name
);
expect(currentProformaInvoice?.productFamilyName).toBe(
productFamilyResponse?.productFamily?.name
);
});

test('should create a preview signup proforma invoice with next_proforma_invoice attribute', async () => {
const productHandle = uid();
const { productResponse, productFamilyResponse } = await createProduct({
productHandle,
});
const customer = await createCustomer();

const response =
await proformaInvoicesController.previewSignupProformaInvoice(
'nextProformaId',
{
subscription: {
productHandle,
customerReference: uid(),
customerId: customer.id,
},
}
);

expect(response.request.url).toContain(
'next_proforma_invoice=nextProformaId'
);
const { currentProformaInvoice } = response.result.proformaInvoicePreview;
expect(response.statusCode).toBe(201);
expect(currentProformaInvoice?.customerId).toBe(customer.id);

expect(currentProformaInvoice?.productName).toBe(
productResponse.product.name
);
expect(currentProformaInvoice?.productFamilyName).toBe(
productFamilyResponse?.productFamily?.name
);
});

test('should throw an error when user sends an invalid payload with a missing product', async () => {
const promise = proformaInvoicesController.previewSignupProformaInvoice(
'',
{
subscription: {
customerReference: uid(),
},
}
);

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(422);
expect(reason.result.errors.base).toEqual(['Missing required product']);
});
});

test('should throw an error when user sends an invalid payload with a missing customer', async () => {
const productHandle = uid();
await createProduct({
productHandle,
});
const promise = proformaInvoicesController.previewSignupProformaInvoice(
'',
{
subscription: {
productHandle,
customerReference: uid(),
},
}
);

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(422);
expect(reason.result.errors.customer).toEqual([
'Missing required customer attributes',
]);
});
});

test('should throw an 401 error with invalid credentials', async () => {
const promise =
invalidProformaInvoicesController.previewSignupProformaInvoice('', {
subscription: {},
});

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(401);
});
});
});
114 changes: 114 additions & 0 deletions e2e/src/proformaInvoiceController/signUpProformaInvoice.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ProformaInvoicesController } from 'advanced-billing-sdk';
import { createClient, createInvalidClient } from '../config';
import { uid } from 'uid';
import createProduct from '../utils/products';
import { createCustomer } from '../utils/customers';

describe('Create Sign up Proforma Invoice', () => {
let proformaInvoicesController: ProformaInvoicesController;
let invalidProformaInvoicesController: ProformaInvoicesController;

beforeAll(async () => {
const client = createClient();
const invalidClient = createInvalidClient();
proformaInvoicesController = new ProformaInvoicesController(client);
invalidProformaInvoicesController = new ProformaInvoicesController(
invalidClient
);
});

test('should create sign up proforma invoice when user sends a valid product and customer', async () => {
const productHandle = uid();
const { productResponse, productFamilyResponse } = await createProduct({
productHandle,
});
const customer = await createCustomer();
const response =
await proformaInvoicesController.createSignupProformaInvoice({
subscription: {
productHandle: productHandle,
customerReference: uid(),
customerId: customer.id,
},
});

expect(response.statusCode).toBe(201);
expect(response.result.customerId).toBe(customer.id);
expect(response.result.productName).toBe(productResponse.product.name);
expect(response.result.productFamilyName).toBe(
productFamilyResponse?.productFamily?.name
);
});

test('should create sign up proforma invoice when user sends a valid product and customer attributes', async () => {
const productHandle = uid();
const { productResponse, productFamilyResponse } = await createProduct({
productHandle,
});
const response =
await proformaInvoicesController.createSignupProformaInvoice({
subscription: {
productHandle: productHandle,
customerReference: uid(),
customerAttributes: {
firstName: 'Martha',
lastName: 'Perez',
email: '[email protected]',
},
},
});

expect(response.statusCode).toBe(201);
expect(response.result.productName).toBe(productResponse.product.name);
expect(response.result.productFamilyName).toBe(
productFamilyResponse?.productFamily?.name
);
});

test('should throw an error when user sends an invalid payload with a missing product', async () => {
const promise = proformaInvoicesController.createSignupProformaInvoice({
subscription: {
customerReference: uid(),
},
});

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(422);
expect(reason.result.errors.base).toEqual(['Missing required product']);
});
});

test('should throw an error when user sends an invalid payload with a missing customer', async () => {
const productHandle = uid();
await createProduct({
productHandle,
});
const promise = proformaInvoicesController.createSignupProformaInvoice({
subscription: {
productHandle,
customerReference: uid(),
},
});

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(422);
expect(reason.result.errors.customer).toEqual([
'Missing required customer attributes',
]);
});
});

test('should throw an 401 error with invalid credentials', async () => {
const promise =
invalidProformaInvoicesController.createSignupProformaInvoice({
subscription: {},
});

expect(promise).rejects.toThrow();
await promise.catch((reason) => {
expect(reason.statusCode).toBe(401);
});
});
});
30 changes: 30 additions & 0 deletions e2e/src/utils/components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ComponentsController, PricingScheme } from 'advanced-billing-sdk';
import { createClient } from '../config';
import { uid } from 'uid';

export async function createComponent({
productFamilyId,
}: {
productFamilyId: number;
}) {
const client = createClient();
const componentsController = new ComponentsController(client);
const meteredComponent = {
productFamilyId,
name: 'test',
description: 'test',
quantity: 1,
unitName: 'test',
price: 1,
handle: uid(),
pricingScheme: PricingScheme.PerUnit,
unitPrice: 1,
};
const response = await componentsController.createMeteredComponent(
productFamilyId,
{
meteredComponent,
}
);
return response.result;
}
8 changes: 8 additions & 0 deletions e2e/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ export async function cleanSite() {
const response = await sitesController.clearSite();
return response;
}

export async function waitForSeconds(time: number) {
return new Promise((resolve) =>
setTimeout(() => {
resolve('success');
}, time * 1000)
);
}
Loading

0 comments on commit a27e408

Please sign in to comment.