Skip to content

Commit

Permalink
chore(web,api): remove redundant code; add/update e2e tests (#6688)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmaraX authored Oct 16, 2024
1 parent 7a2ad59 commit bfb8b95
Show file tree
Hide file tree
Showing 44 changed files with 1,585 additions and 2,668 deletions.
2 changes: 1 addition & 1 deletion .source
4 changes: 0 additions & 4 deletions apps/api/src/app/integrations/e2e/update-integration.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,6 @@ describe('Update Integration - /integrations/:integrationId (PUT)', function ()
_environmentId: session.environment._id,
});

process.env.IS_IMPROVED_BILLING_ENABLED = 'true';

await communityOrganizationRepository.update(
{ _id: session.organization._id },
{ $set: { apiServiceLevel: ApiServiceLevelEnum.BUSINESS } }
Expand Down Expand Up @@ -1013,8 +1011,6 @@ describe('Update Integration - /integrations/:integrationId (PUT)', function ()
_environmentId: session.environment._id,
});

process.env.IS_IMPROVED_BILLING_ENABLED = 'true';

await communityOrganizationRepository.update(
{ _id: session.organization._id },
{ $set: { apiServiceLevel: ApiServiceLevelEnum.FREE } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,7 @@ export class UpdateIntegration {
command: UpdateIntegrationCommand,
existingIntegration: IntegrationEntity
): Promise<boolean> {
const [isImprovedBillingEnabled, organization] = await Promise.all([
this.getFeatureFlag.execute(
GetFeatureFlagCommand.create({
userId: 'system',
environmentId: 'system',
organizationId: command.organizationId,
key: FeatureFlagsKeysEnum.IS_IMPROVED_BILLING_ENABLED,
})
),
this.communityOrganizationRepository.findOne({ _id: command.organizationId }),
]);

if (!isImprovedBillingEnabled) {
return false;
}
const organization = await this.communityOrganizationRepository.findOne({ _id: command.organizationId });

const isRemoveNovuBrandingDefined = typeof command.removeNovuBranding !== 'undefined';
const isRemoveNovuBrandingChanged =
Expand Down
112 changes: 112 additions & 0 deletions apps/api/src/app/testing/billing/create-checkout-session.e2e-ee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable global-require */
import sinon from 'sinon';
import { expect } from 'chai';
// eslint-disable-next-line no-restricted-imports
import { StripeBillingIntervalEnum } from '@novu/ee-billing/src/stripe/types';
import { ApiServiceLevelEnum } from '@novu/shared';

const checkoutSessionCreateParamsMock = {
mode: 'subscription',
customer: 'customer_id',
payment_method_types: ['card'],
tax_id_collection: {
enabled: true,
},
automatic_tax: {
enabled: true,
},
billing_address_collection: 'auto',
customer_update: {
name: 'auto',
address: 'auto',
},
success_url: `${process.env.FRONT_BASE_URL}/manage-account/billing?result=success&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.FRONT_BASE_URL}/manage-account/billing?result=canceled`,
};

describe('Create checkout session', async () => {
if (!require('@novu/ee-billing').CreateCheckoutSession) {
throw new Error("CreateCheckoutSession doesn't exist");
}

const { CreateCheckoutSession } = require('@novu/ee-billing');

const getOrCreateCustomer = {
execute: () => Promise.resolve({ id: 'customer_id' }),
};
const getPrices = {
execute: () =>
Promise.resolve({
licensed: [{ id: 'licensed_price_id_1' }],
metered: [{ id: 'metered_price_id_1' }],
}),
};
const userRepository = {
findById: () =>
Promise.resolve({
_id: 'user_id',
email: '[email protected]',
}),
};

const stripeStub = {
checkout: {
sessions: {
create: () => {},
},
},
};
let checkoutCreateStub: sinon.SinonStub;

beforeEach(() => {
checkoutCreateStub = sinon.stub(stripeStub.checkout.sessions, 'create').resolves({ url: 'url' });
});

afterEach(() => {
checkoutCreateStub.reset();
});

it('Create checkout session with 1 subscription containing 1 licensed item and 1 metered item for monthly billing interval', async () => {
const usecase = new CreateCheckoutSession(stripeStub, getOrCreateCustomer, userRepository, getPrices);

const result = await usecase.execute({
organizationId: 'organization_id',
userId: 'user_id',
billingInterval: StripeBillingIntervalEnum.MONTH,
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
});

expect(checkoutCreateStub.lastCall.args.at(0)).to.deep.equal({
...checkoutSessionCreateParamsMock,
line_items: [{ price: 'licensed_price_id_1', quantity: 1 }, { price: 'metered_price_id_1' }],
metadata: {
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
billingInterval: StripeBillingIntervalEnum.MONTH,
},
});

expect(result).to.deep.equal({ stripeCheckoutUrl: 'url' });
});

it('Create checkout session with 1 subscription containing 1 licensed item for annual billing interval', async () => {
const usecase = new CreateCheckoutSession(stripeStub, getOrCreateCustomer, userRepository, getPrices);

const result = await usecase.execute({
organizationId: 'organization_id',
userId: 'user_id',
billingInterval: StripeBillingIntervalEnum.YEAR,
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
});

expect(checkoutCreateStub.lastCall.args.at(0)).to.deep.equal({
...checkoutSessionCreateParamsMock,
line_items: [{ price: 'licensed_price_id_1', quantity: 1 }],
metadata: {
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
billingInterval: StripeBillingIntervalEnum.YEAR,
},
});

expect(result).to.deep.equal({ stripeCheckoutUrl: 'url' });
});
});
Loading

0 comments on commit bfb8b95

Please sign in to comment.