-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
8f0847a
9396cf8
6bed5fa
a726630
80d0844
ab494f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
}, | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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