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 1 commit
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
"**/.yarn": true,
},
"eslint.debug": true,
<<<<<<< Updated upstream
"files.associations": {
".cursorrules": "markdown"
}
=======
"jestrunner.codeLensSelector": "**/*.{test,spec,integration-spec}.{js,jsx,ts,tsx}"
>>>>>>> Stashed changes
}
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 @@ -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
@@ -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',
},
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import Stripe from 'stripe';

import { BillingMeterEventTimeWindow } from 'src/engine/core-modules/billing/enums/billing-meter-event-time-window.enum';
import { BillingMeterStatus } from 'src/engine/core-modules/billing/enums/billing-meter-status.enum';
import { transformStripeMeterDataToMeterRepositoryData } from 'src/engine/core-modules/billing/utils/transform-stripe-meter-data-to-meter-repository-data.util';

describe('transformStripeMeterDataToMeterRepositoryData', () => {
it('should return the correct data with customer mapping', () => {
const data: Stripe.Billing.Meter = {
id: 'met_123',
object: 'billing.meter',
created: 1719859200,
display_name: 'Meter 1',
event_name: 'event_1',
status: 'active',
customer_mapping: {
event_payload_key: 'event_payload_key_1',
type: 'by_id',
},
default_aggregation: {
formula: 'count',
},
event_time_window: 'day',
livemode: false,
status_transitions: {
deactivated_at: null,
},
updated: 1719859200,
value_settings: {
event_payload_key: 'event_payload_key_1',
},
};

const result = transformStripeMeterDataToMeterRepositoryData(data);

expect(result).toEqual({
stripeMeterId: 'met_123',
displayName: 'Meter 1',
eventName: 'event_1',
status: BillingMeterStatus.ACTIVE,
customerMapping: {
event_payload_key: 'event_payload_key_1',
type: 'by_id',
},
eventTimeWindow: BillingMeterEventTimeWindow.DAY,
valueSettings: {
event_payload_key: 'event_payload_key_1',
},
});
});
it('should return the correct data with null values', () => {
const data: Stripe.Billing.Meter = {
id: 'met_1234',
object: 'billing.meter',
created: 1719859200,
display_name: 'Meter 2',
event_name: 'event_2',
status: 'inactive',
customer_mapping: {
event_payload_key: 'event_payload_key_2',
type: 'by_id',
},
default_aggregation: {
formula: 'sum',
},
event_time_window: null,
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 should also verify behavior with event_time_window='hour' since that's another valid value

livemode: false,
status_transitions: {
deactivated_at: 1719859200,
},
updated: 1719859200,
value_settings: {
event_payload_key: 'event_payload_key_2',
},
};

const result = transformStripeMeterDataToMeterRepositoryData(data);

expect(result).toEqual({
stripeMeterId: 'met_1234',
displayName: 'Meter 2',
eventName: 'event_2',
status: BillingMeterStatus.INACTIVE,
customerMapping: {
event_payload_key: 'event_payload_key_2',
type: 'by_id',
},
eventTimeWindow: undefined,
valueSettings: {
event_payload_key: 'event_payload_key_2',
},
});
});
});
Loading
Loading