Files
twenty/packages/twenty-server/test/integration/billing/suites/billing-controller.integration-spec.ts
Ana Sofia Marin Alexandre c39af5f063 Add Integration and unit tests on Billing (#9317)
Solves [ https://github.com/twentyhq/private-issues/issues/214 ]

**TLDR**
Add unit and integration tests to Billing. First approach to run jest
integration tests directly from VSCode.

**In order to run the unit tests:**
Run unit test using the CLI or with the jest extension directly from
VSCode.

**In order to run the integration tests:**
Ensure that your database has the billingTables. If that's not the case,
migrate the database with IS_BILLING_ENABLED set to true:
` npx nx run twenty-server:test:integration
test/integration/billing/suites/billing-controller.integration-spec.ts`

**Doing:**
- Unit test on transformSubscriptionEventToSubscriptionItem
- More tests cases in billingController integration tests.

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Weiko <corentin@twenty.com>
Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
2025-01-09 18:30:41 +01:00

113 lines
4.0 KiB
TypeScript

import request from 'supertest';
import { createMockStripeEntitlementUpdatedData } from 'test/integration/billing/utils/create-mock-stripe-entitlement-updated-data.util';
import { createMockStripePriceCreatedData } from 'test/integration/billing/utils/create-mock-stripe-price-created-data.util';
import { createMockStripeProductUpdatedData } from 'test/integration/billing/utils/create-mock-stripe-product-updated-data.util';
import { createMockStripeSubscriptionCreatedData } from 'test/integration/billing/utils/create-mock-stripe-subscription-created-data.util';
const client = request(`http://localhost:${APP_PORT}`);
describe('BillingController (integration)', () => {
it('should handle product.updated and price.created webhook events', async () => {
const productUpdatedPayload = {
type: 'product.updated',
data: createMockStripeProductUpdatedData(),
};
const priceCreatedPayload = {
type: 'price.created',
data: createMockStripePriceCreatedData(),
};
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'correct-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(productUpdatedPayload))
.expect(200)
.then((res) => {
expect(res.body.stripeProductId).toBeDefined();
});
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'correct-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(priceCreatedPayload))
.expect(200)
.then((res) => {
expect(res.body.stripePriceId).toBeDefined();
expect(res.body.stripeMeterId).toBeDefined();
});
});
it('should handle subscription.created webhook event', async () => {
const subscriptionCreatedPayload = {
type: 'customer.subscription.created',
data: createMockStripeSubscriptionCreatedData(),
};
const entitlementUpdatedPayload = {
type: 'entitlements.active_entitlement_summary.updated',
data: createMockStripeEntitlementUpdatedData(),
};
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'correct-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(subscriptionCreatedPayload))
.expect(200)
.then((res) => {
expect(res.body.stripeSubscriptionId).toBeDefined();
expect(res.body.stripeCustomerId).toBeDefined();
});
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'correct-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(entitlementUpdatedPayload))
.expect(200)
.then((res) => {
expect(res.body.stripeEntitlementCustomerId).toBeDefined();
});
});
it('should handle entitlements.active_entitlement_summary.updated when the subscription is not found', async () => {
const entitlementUpdatedPayload = {
type: 'entitlements.active_entitlement_summary.updated',
data: createMockStripeEntitlementUpdatedData({
customer: 'new_customer',
}),
};
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'correct-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(entitlementUpdatedPayload))
.expect(404);
});
it('should reject webhook with invalid signature', async () => {
const entitlementUpdatedPayload = {
type: 'customer.entitlement.created',
data: {
object: {
id: 'ent_test123',
},
},
};
await client
.post('/billing/webhooks')
.set('Authorization', `Bearer ${ACCESS_TOKEN}`)
.set('stripe-signature', 'invalid-signature')
.set('Content-Type', 'application/json')
.send(JSON.stringify(entitlementUpdatedPayload))
.expect(500);
});
});