Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Add Integration and unit tests on Billing #9317

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ module.exports = {
rules: {},
},
{
files: ['*.spec.@(ts|tsx|js|jsx)', '*.test.@(ts|tsx|js|jsx)'],
files: [
'*.spec.@(ts|tsx|js|jsx)',
'*.integration-spec.@(ts|tsx|js|jsx)',
'*.test.@(ts|tsx|js|jsx)',
],
Comment on lines +99 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: test file patterns could be consolidated into a single pattern like '*.@(spec|integration-spec|test).@(ts|tsx|js|jsx)' for better maintainability

env: {
jest: true,
},
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ jobs:
PGPASSWORD_SUPERUSER: postgres
ALLOW_NOSSL: "true"
SPILO_PROVIDER: "local"
IS_BILLING_ENABLED: "true"
ports:
- 5432:5432
options: >-
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@
"files.associations": {
".cursorrules": "markdown"
},
"jestrunner.codeLensSelector": "**/*.{test,spec,integration-spec}.{js,jsx,ts,tsx}"
}
}
1 change: 1 addition & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"!{projectRoot}/**/tsconfig.spec.json",
"!{projectRoot}/**/*.test.(ts|tsx)",
"!{projectRoot}/**/*.spec.(ts|tsx)",
"!{projectRoot}/**/*.integration-spec.ts",
"!{projectRoot}/**/__tests__/*"
],
"production": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import {
} from '@nestjs/common';

import { Response } from 'express';
import Stripe from 'stripe';

import {
BillingException,
BillingExceptionCode,
} from 'src/engine/core-modules/billing/billing.exception';
import { WebhookEvent } from 'src/engine/core-modules/billing/enums/billing-webhook-events.enum';
import { BillingWebhookEvent } from 'src/engine/core-modules/billing/enums/billing-webhook-events.enum';
import { BillingRestApiExceptionFilter } from 'src/engine/core-modules/billing/filters/billing-api-exception.filter';
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import { BillingWebhookEntitlementService } from 'src/engine/core-modules/billing/services/billing-webhook-entitlement.service';
Expand Down Expand Up @@ -53,67 +54,58 @@ export class BillingController {
req.rawBody,
);

if (event.type === WebhookEvent.SETUP_INTENT_SUCCEEDED) {
await this.billingSubscriptionService.handleUnpaidInvoices(event.data);
}

if (
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_CREATED ||
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED ||
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_DELETED
) {
const workspaceId = event.data.object.metadata?.workspaceId;
try {
const result = await this.handleStripeEvent(event);

if (!workspaceId) {
res.status(200).send(result).end();
} catch (error) {
if (error instanceof BillingException) {
res.status(404).end();

return;
}

await this.billingWebhookSubscriptionService.processStripeEvent(
workspaceId,
event.data,
);
}
if (
event.type === WebhookEvent.CUSTOMER_ACTIVE_ENTITLEMENT_SUMMARY_UPDATED
) {
try {
await this.billingWebhookEntitlementService.processStripeEvent(
}

private async handleStripeEvent(event: Stripe.Event) {
switch (event.type) {
case BillingWebhookEvent.SETUP_INTENT_SUCCEEDED:
return await this.billingSubscriptionService.handleUnpaidInvoices(
event.data,
);
case BillingWebhookEvent.PRICE_UPDATED:
case BillingWebhookEvent.PRICE_CREATED:
return await this.billingWebhookPriceService.processStripeEvent(
event.data,
);
} catch (error) {
if (
error instanceof BillingException &&
error.code === BillingExceptionCode.BILLING_CUSTOMER_NOT_FOUND
) {
res.status(404).end();
}
}
}

if (
event.type === WebhookEvent.PRODUCT_CREATED ||
event.type === WebhookEvent.PRODUCT_UPDATED
) {
await this.billingWebhookProductService.processStripeEvent(event.data);
}
if (
event.type === WebhookEvent.PRICE_CREATED ||
event.type === WebhookEvent.PRICE_UPDATED
) {
try {
await this.billingWebhookPriceService.processStripeEvent(event.data);
} catch (error) {
if (
error instanceof BillingException &&
error.code === BillingExceptionCode.BILLING_PRODUCT_NOT_FOUND
) {
res.status(404).end();
case BillingWebhookEvent.PRODUCT_UPDATED:
case BillingWebhookEvent.PRODUCT_CREATED:
return await this.billingWebhookProductService.processStripeEvent(
event.data,
);
case BillingWebhookEvent.CUSTOMER_ACTIVE_ENTITLEMENT_SUMMARY_UPDATED:
return await this.billingWebhookEntitlementService.processStripeEvent(
event.data,
);

case BillingWebhookEvent.CUSTOMER_SUBSCRIPTION_CREATED:
case BillingWebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED:
case BillingWebhookEvent.CUSTOMER_SUBSCRIPTION_DELETED: {
const workspaceId = event.data.object.metadata?.workspaceId;

if (!workspaceId) {
throw new BillingException(
'Workspace ID is required for subscription events',
BillingExceptionCode.BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND,
);
}

return await this.billingWebhookSubscriptionService.processStripeEvent(
workspaceId,
event.data,
);
}
default:
return {};
}

res.status(200).end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export class BillingException extends CustomException {
export enum BillingExceptionCode {
BILLING_CUSTOMER_NOT_FOUND = 'BILLING_CUSTOMER_NOT_FOUND',
BILLING_PRODUCT_NOT_FOUND = 'BILLING_PRODUCT_NOT_FOUND',
BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND = 'BILLING_SUBSCRIPTION_EVENT_WORKSPACE_NOT_FOUND',
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum WebhookEvent {
export enum BillingWebhookEvent {
CUSTOMER_SUBSCRIPTION_CREATED = 'customer.subscription.created',
CUSTOMER_SUBSCRIPTION_UPDATED = 'customer.subscription.updated',
CUSTOMER_SUBSCRIPTION_DELETED = 'customer.subscription.deleted',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export class BillingRestApiExceptionFilter implements ExceptionFilter {
response,
404,
);
case BillingExceptionCode.BILLING_PRODUCT_NOT_FOUND:
return this.httpExceptionHandlerService.handleError(
exception,
response,
404,
);
default:
return this.httpExceptionHandlerService.handleError(
exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export class BillingSubscriptionService {
billingSubscription.stripeSubscriptionId,
);
}

return {
handleUnpaidInvoiceStripeSubscriptionId:
billingSubscription.stripeSubscriptionId,
};
}

async getWorkspaceEntitlementByKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ export class BillingWebhookEntitlementService {
skipUpdateIfNoValuesChanged: true,
},
);

return {
stripeEntitlementCustomerId: data.object.customer,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@ export class BillingWebhookPriceService {
skipUpdateIfNoValuesChanged: true,
},
);

return {
stripePriceId: data.object.id,
stripeMeterId: meterId,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class BillingWebhookProductService {
conflictPaths: ['stripeProductId'],
skipUpdateIfNoValuesChanged: true,
});

return {
stripeProductId: data.object.id,
};
}

isStripeValidProductMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BillingWebhookSubscriptionService {
});

if (!workspace) {
return;
return { noWorkspace: true };
}

await this.billingCustomerRepository.upsert(
Expand Down Expand Up @@ -110,5 +110,10 @@ export class BillingWebhookSubscriptionService {
String(data.object.customer),
workspaceId,
);

return {
stripeSubscriptionId: data.object.id,
stripeCustomerId: data.object.customer,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Stripe from 'stripe';

import { BillingPlanKey } from 'src/engine/core-modules/billing/enums/billing-plan-key.enum';
import { BillingUsageType } from 'src/engine/core-modules/billing/enums/billing-usage-type.enum';
import { isStripeValidProductMetadata } from 'src/engine/core-modules/billing/utils/is-stripe-valid-product-metadata.util';
describe('isStripeValidProductMetadata', () => {
it('should return true if metadata is empty', () => {
const metadata: Stripe.Metadata = {};

expect(isStripeValidProductMetadata(metadata)).toBe(true);
});
Comment on lines +7 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Empty metadata returning true but missing required keys returning false (line 49-55) seems inconsistent. Should validate this is the intended behavior.

it('should return true if metadata has the correct keys with correct values', () => {
const metadata: Stripe.Metadata = {
planKey: BillingPlanKey.PRO,
priceUsageBased: BillingUsageType.METERED,
};

expect(isStripeValidProductMetadata(metadata)).toBe(true);
});

it('should return true if metadata has extra keys', () => {
const metadata: Stripe.Metadata = {
planKey: BillingPlanKey.ENTERPRISE,
priceUsageBased: BillingUsageType.METERED,
randomKey: 'randomValue',
};

expect(isStripeValidProductMetadata(metadata)).toBe(true);
});

it('should return false if metadata has invalid keys', () => {
const metadata: Stripe.Metadata = {
planKey: 'invalid',
priceUsageBased: BillingUsageType.METERED,
};

expect(isStripeValidProductMetadata(metadata)).toBe(false);
});

it('should return false if metadata has invalid values', () => {
const metadata: Stripe.Metadata = {
planKey: BillingPlanKey.PRO,
priceUsageBased: 'invalid',
};

expect(isStripeValidProductMetadata(metadata)).toBe(false);
});

it('should return false if the metadata does not have the required keys', () => {
const metadata: Stripe.Metadata = {
randomKey: 'randomValue',
};

expect(isStripeValidProductMetadata(metadata)).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Stripe from 'stripe';

import { BillingEntitlementKey } from 'src/engine/core-modules/billing/enums/billing-entitlement-key.enum';
import { transformStripeEntitlementUpdatedEventToEntitlementRepositoryData } from 'src/engine/core-modules/billing/utils/transform-stripe-entitlement-updated-event-to-entitlement-repository-data.util';

describe('transformStripeEntitlementUpdatedEventToEntitlementRepositoryData', () => {
it('should return the SSO key with true value', () => {
const data: Stripe.EntitlementsActiveEntitlementSummaryUpdatedEvent.Data = {
object: {
customer: 'cus_123',
entitlements: {
data: [
{
lookup_key: 'SSO',
feature: 'SSO',
livemode: false,
id: 'ent_123',
object: 'entitlements.active_entitlement',
},
],
object: 'list',
has_more: false,
url: '',
},
livemode: false,
object: 'entitlements.active_entitlement_summary',
},
};

const result =
transformStripeEntitlementUpdatedEventToEntitlementRepositoryData(
'workspaceId',
data,
);

expect(result).toEqual([
{
workspaceId: 'workspaceId',
key: BillingEntitlementKey.SSO,
value: true,
stripeCustomerId: 'cus_123',
},
]);
});

it('should return the SSO key with false value,should only render the values that are listed in BillingEntitlementKeys', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Test description contains a comma splice. Consider splitting into two test cases for clarity: one for SSO false value and one for BillingEntitlementKeys filtering

const data: Stripe.EntitlementsActiveEntitlementSummaryUpdatedEvent.Data = {
object: {
customer: 'cus_123',
entitlements: {
data: [
{
id: 'ent_123',
object: 'entitlements.active_entitlement',
lookup_key: 'DIFFERENT_KEY',
feature: 'DIFFERENT_FEATURE',
livemode: false,
},
],
object: 'list',
has_more: false,
url: '',
},
livemode: false,
object: 'entitlements.active_entitlement_summary',
},
};

const result =
transformStripeEntitlementUpdatedEventToEntitlementRepositoryData(
'workspaceId',
data,
);

expect(result).toEqual([
{
workspaceId: 'workspaceId',
key: BillingEntitlementKey.SSO,
value: false,
stripeCustomerId: 'cus_123',
},
]);
});
});
Loading
Loading