Files
speckle-server/packages/server/modules/gatekeeper/tests/unit/checkout.spec.ts
T
Gergő Jedlicska af3857a209 gergo/web 2038 billing graphql api (#3379)
* feat(gatekeeper): add gatekeeper module feature flag

* feat(gatekeeper): add workspace pricing table domain

* feat(gatekeeper): add checkout session creation

* feat(gatekeeper): verify stripe signature

* wip(gatekeeper): checkout callbacks

* feat(gatekeeper): add unlimited and academia plan types

* refactor(envHelper): getStringFromEnv helper

* chore(gatekeeper): add future todos

* feat(gatekeeper): add productId to the subscription domain

* feat(gatekeeper): add in memory repositories

* feat(gatekeeper): add more errors

* feat(gatekeeper): complete checkout session service

* feat(gatekeeper): add stripe client implementation

* feat(gatekeeper): add checkout session completion webhook callback path

* feat(gendo): fix not needing env vars if gendo module is not enabled

* feat(gatekeeper): require a license for billing

* chore(gatekeeper): cleanup before testing

* feat(gatekeeper): subscriptionData parsing model

* ci: add billing integration and gatekeeper modules to test config

* test(gatekeeper): add checkout service tests

* feat(gatekeeper): make completeCheckout callback idempotent properly

* feat(gatekeeper): move to knex based repositories

* test(gatekeeper): billing repository tests

* feat(gatekeeper): add yearly billing cycle toggle

* feat(ci): add stripe integration context to test job

* feat(billingPage): conditionally render the checkout CTAs

* fix(gatekeeper): remove flaky test condition

* feat(helm): add billing integration feature flag

* WIP billing gql api

* feat(gatekeeper): cancel checkout session api

* feat(gatekeeper): handle existing checkout sessions, when trying to create a new one

* feat(gatekeeper): add workspace plans gql api

* feat(gatekeeper): handle cancelation and subscription updates

* fix(gatekeeper): scope initialization

* fix(gatekeeper): eliminate stripe client import sideeffect

* fix(gatekeeper): eliminate stripe client import sideeffect 2

* fix(mainConstants): fitler gatekeeper scopes with feature flag
2024-10-25 10:46:09 +02:00

604 lines
21 KiB
TypeScript

import {
CheckoutSessionNotFoundError,
WorkspaceAlreadyPaidError,
WorkspaceCheckoutSessionInProgressError
} from '@/modules/gatekeeper/errors/billing'
import {
completeCheckoutSessionFactory,
startCheckoutSessionFactory
} from '@/modules/gatekeeper/services/checkout'
import { expectToThrow } from '@/test/assertionHelper'
import { expect } from 'chai'
import cryptoRandomString from 'crypto-random-string'
import {
CheckoutSession,
PaidWorkspacePlan,
SubscriptionData,
WorkspaceSubscription
} from '@/modules/gatekeeper/domain/billing'
import {
PaidWorkspacePlans,
WorkspacePlanBillingIntervals
} from '@/modules/gatekeeper/domain/workspacePricing'
describe('checkout @gatekeeper', () => {
describe('startCheckoutSessionFactory creates a function, that', () => {
it('does not allow checkout for workspace plans, that is in a valid state', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(() =>
startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
name: 'pro',
status: 'valid',
workspaceId
}),
getWorkspaceCheckoutSession: () => {
expect.fail()
},
countRole: () => {
expect.fail()
},
createCheckoutSession: () => {
expect.fail()
},
saveCheckoutSession: () => {
expect.fail()
},
deleteCheckoutSession: () => {
expect.fail()
}
})({
workspaceId,
billingInterval: 'monthly',
workspacePlan: 'business',
workspaceSlug: cryptoRandomString({ length: 10 })
})
)
expect(err.message).to.be.equal(new WorkspaceAlreadyPaidError().message)
})
it('does not allow checkout for workspace plans, that is in a paymentFailed state', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(() =>
startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
name: 'pro',
status: 'paymentFailed',
workspaceId
}),
getWorkspaceCheckoutSession: () => {
expect.fail()
},
countRole: () => {
expect.fail()
},
createCheckoutSession: () => {
expect.fail()
},
deleteCheckoutSession: () => {
expect.fail()
},
saveCheckoutSession: () => {
expect.fail()
}
})({
workspaceId,
billingInterval: 'monthly',
workspacePlan: 'business',
workspaceSlug: cryptoRandomString({ length: 10 })
})
)
expect(err.message).to.be.equal(new WorkspaceAlreadyPaidError().message)
})
it('does not allow checkout for a workspace, that already has a recent checkout session', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(() =>
startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
name: 'team',
status: 'trial',
workspaceId
}),
getWorkspaceCheckoutSession: async () => ({
billingInterval: 'monthly',
id: cryptoRandomString({ length: 10 }),
paymentStatus: 'unpaid',
url: '',
workspaceId,
workspacePlan: 'business',
createdAt: new Date(),
updatedAt: new Date()
}),
countRole: () => {
expect.fail()
},
createCheckoutSession: () => {
expect.fail()
},
deleteCheckoutSession: () => {
expect.fail()
},
saveCheckoutSession: () => {
expect.fail()
}
})({
workspaceId,
billingInterval: 'monthly',
workspacePlan: 'business',
workspaceSlug: cryptoRandomString({ length: 10 })
})
)
expect(err.message).to.be.equal(
new WorkspaceCheckoutSessionInProgressError().message
)
})
it('does not allow checkout for a workspace, that already has a checkout session', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(() =>
startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
name: 'team',
status: 'trial',
workspaceId
}),
getWorkspaceCheckoutSession: async () => ({
billingInterval: 'monthly',
id: cryptoRandomString({ length: 10 }),
paymentStatus: 'unpaid',
url: '',
workspaceId,
workspacePlan: 'business',
createdAt: new Date(),
updatedAt: new Date()
}),
countRole: () => {
expect.fail()
},
createCheckoutSession: () => {
expect.fail()
},
deleteCheckoutSession: () => {
expect.fail()
},
saveCheckoutSession: () => {
expect.fail()
}
})({
workspaceId,
billingInterval: 'monthly',
workspacePlan: 'business',
workspaceSlug: cryptoRandomString({ length: 10 })
})
)
expect(err.message).to.be.equal(
new WorkspaceCheckoutSessionInProgressError().message
)
})
it('creates and stores a checkout for workspaces that are not on a plan', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
const checkoutSession: CheckoutSession = {
id: cryptoRandomString({ length: 10 }),
workspaceId,
workspacePlan,
url: 'https://example.com',
billingInterval,
paymentStatus: 'unpaid',
createdAt: new Date(),
updatedAt: new Date()
}
let storedCheckoutSession: CheckoutSession | undefined = undefined
const createdCheckoutSession = await startCheckoutSessionFactory({
getWorkspacePlan: async () => null,
getWorkspaceCheckoutSession: async () => null,
countRole: async () => 1,
deleteCheckoutSession: () => {
expect.fail()
},
createCheckoutSession: async () => checkoutSession,
saveCheckoutSession: async ({ checkoutSession }) => {
storedCheckoutSession = checkoutSession
}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
expect(checkoutSession).deep.equal(storedCheckoutSession)
expect(checkoutSession).deep.equal(createdCheckoutSession)
})
it('creates and stores a checkout for workspaces without a plan', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
const checkoutSession: CheckoutSession = {
id: cryptoRandomString({ length: 10 }),
workspaceId,
workspacePlan,
url: 'https://example.com',
billingInterval,
paymentStatus: 'unpaid',
createdAt: new Date(),
updatedAt: new Date()
}
let storedCheckoutSession: CheckoutSession | undefined = undefined
const createdCheckoutSession = await startCheckoutSessionFactory({
getWorkspacePlan: async () => null,
getWorkspaceCheckoutSession: async () => null,
countRole: async () => 1,
deleteCheckoutSession: () => {
expect.fail()
},
createCheckoutSession: async () => checkoutSession,
saveCheckoutSession: async ({ checkoutSession }) => {
storedCheckoutSession = checkoutSession
}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
expect(checkoutSession).deep.equal(storedCheckoutSession)
expect(checkoutSession).deep.equal(createdCheckoutSession)
})
it('creates and stores a checkout for TRIAL workspaces', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
const checkoutSession: CheckoutSession = {
id: cryptoRandomString({ length: 10 }),
workspaceId,
workspacePlan,
url: 'https://example.com',
billingInterval,
paymentStatus: 'unpaid',
createdAt: new Date(),
updatedAt: new Date()
}
let storedCheckoutSession: CheckoutSession | undefined = undefined
const createdCheckoutSession = await startCheckoutSessionFactory({
getWorkspacePlan: async () => ({ workspaceId, name: 'team', status: 'trial' }),
getWorkspaceCheckoutSession: async () => null,
countRole: async () => 1,
deleteCheckoutSession: () => {
expect.fail()
},
createCheckoutSession: async () => checkoutSession,
saveCheckoutSession: async ({ checkoutSession }) => {
storedCheckoutSession = checkoutSession
}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
expect(checkoutSession).deep.equal(storedCheckoutSession)
expect(checkoutSession).deep.equal(createdCheckoutSession)
})
it('creates and stores a checkout for TRIAL workspaces even if it has an old unpaid checkout session', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
const checkoutSession: CheckoutSession = {
id: cryptoRandomString({ length: 10 }),
workspaceId,
workspacePlan,
url: 'https://example.com',
billingInterval,
paymentStatus: 'unpaid',
createdAt: new Date(),
updatedAt: new Date()
}
let existingCheckoutSession: CheckoutSession | undefined = {
billingInterval,
id: cryptoRandomString({ length: 10 }),
createdAt: new Date(1990, 1, 12),
updatedAt: new Date(1990, 1, 12),
paymentStatus: 'unpaid',
url: 'https://example.com',
workspaceId,
workspacePlan
}
let storedCheckoutSession: CheckoutSession | undefined = undefined
const createdCheckoutSession = await startCheckoutSessionFactory({
getWorkspacePlan: async () => ({ workspaceId, name: 'team', status: 'trial' }),
getWorkspaceCheckoutSession: async () => existingCheckoutSession!,
countRole: async () => 1,
deleteCheckoutSession: async () => {
existingCheckoutSession = undefined
},
createCheckoutSession: async () => checkoutSession,
saveCheckoutSession: async ({ checkoutSession }) => {
storedCheckoutSession = checkoutSession
}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
expect(existingCheckoutSession).to.be.undefined
expect(checkoutSession).deep.equal(storedCheckoutSession)
expect(checkoutSession).deep.equal(createdCheckoutSession)
})
it('does not allow checkout for TRIAL workspaces if there is a paid checkout session', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
let existingCheckoutSession: CheckoutSession | undefined = {
billingInterval,
id: cryptoRandomString({ length: 10 }),
createdAt: new Date(1990, 1, 12),
updatedAt: new Date(1990, 1, 12),
paymentStatus: 'paid',
url: 'https://example.com',
workspaceId,
workspacePlan
}
const err = await expectToThrow(async () => {
await startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
workspaceId,
name: 'team',
status: 'trial'
}),
getWorkspaceCheckoutSession: async () => existingCheckoutSession!,
countRole: async () => 1,
deleteCheckoutSession: async () => {
existingCheckoutSession = undefined
},
createCheckoutSession: async () => {
expect.fail()
},
saveCheckoutSession: async () => {}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
})
expect(err.message).to.equal(new WorkspaceAlreadyPaidError().message)
})
it('does not allow checkout for TRIAL workspaces if there is a paid checkout session', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
let existingCheckoutSession: CheckoutSession | undefined = {
billingInterval,
id: cryptoRandomString({ length: 10 }),
createdAt: new Date(),
updatedAt: new Date(),
paymentStatus: 'unpaid',
url: 'https://example.com',
workspaceId,
workspacePlan
}
const err = await expectToThrow(async () => {
await startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
workspaceId,
name: 'team',
status: 'trial'
}),
getWorkspaceCheckoutSession: async () => existingCheckoutSession!,
countRole: async () => 1,
deleteCheckoutSession: async () => {
existingCheckoutSession = undefined
},
createCheckoutSession: async () => {
expect.fail()
},
saveCheckoutSession: async () => {}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
})
expect(err.message).to.equal(
new WorkspaceCheckoutSessionInProgressError().message
)
})
it('creates and stores a checkout for CANCELED workspaces', async () => {
const workspaceId = cryptoRandomString({ length: 10 })
const workspacePlan: PaidWorkspacePlans = 'pro'
const billingInterval: WorkspacePlanBillingIntervals = 'monthly'
const checkoutSession: CheckoutSession = {
id: cryptoRandomString({ length: 10 }),
workspaceId,
workspacePlan,
url: 'https://example.com',
billingInterval,
paymentStatus: 'unpaid',
createdAt: new Date(),
updatedAt: new Date()
}
let existingCheckoutSession: CheckoutSession | undefined = {
billingInterval: 'monthly',
id: cryptoRandomString({ length: 10 }),
paymentStatus: 'paid',
url: '',
workspaceId,
workspacePlan: 'business',
createdAt: new Date(),
updatedAt: new Date()
}
let storedCheckoutSession: CheckoutSession | undefined = undefined
const createdCheckoutSession = await startCheckoutSessionFactory({
getWorkspacePlan: async () => ({
name: 'pro',
workspaceId,
status: 'canceled'
}),
getWorkspaceCheckoutSession: async () => existingCheckoutSession!,
countRole: async () => 1,
deleteCheckoutSession: async () => {
existingCheckoutSession = undefined
},
createCheckoutSession: async () => checkoutSession,
saveCheckoutSession: async ({ checkoutSession }) => {
storedCheckoutSession = checkoutSession
}
})({
workspaceId,
billingInterval,
workspacePlan,
workspaceSlug: cryptoRandomString({ length: 10 })
})
expect(existingCheckoutSession).to.be.undefined
expect(checkoutSession).deep.equal(storedCheckoutSession)
expect(checkoutSession).deep.equal(createdCheckoutSession)
})
})
describe('completeCheckoutSessionFactory creates a function, that', () => {
it('throws a CheckoutSessionNotFound if the checkoutSession is null', async () => {
const sessionId = cryptoRandomString({ length: 10 })
const subscriptionId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(async () => {
await completeCheckoutSessionFactory({
getCheckoutSession: async () => null,
updateCheckoutSessionStatus: async () => {
expect.fail()
},
upsertPaidWorkspacePlan: async () => {
expect.fail()
},
getSubscriptionData: async () => {
expect.fail()
},
upsertWorkspaceSubscription: async () => {
expect.fail()
}
})({ sessionId, subscriptionId })
expect(err.message).to.equal(new CheckoutSessionNotFoundError().message)
})
})
it('throws for already paid checkout sessions', async () => {
const sessionId = cryptoRandomString({ length: 10 })
const subscriptionId = cryptoRandomString({ length: 10 })
const err = await expectToThrow(async () => {
await completeCheckoutSessionFactory({
getCheckoutSession: async () => ({
billingInterval: 'monthly',
id: sessionId,
paymentStatus: 'paid',
url: 'https://example.com',
workspaceId: cryptoRandomString({ length: 10 }),
workspacePlan: 'business',
createdAt: new Date(),
updatedAt: new Date()
}),
updateCheckoutSessionStatus: async () => {
expect.fail()
},
upsertPaidWorkspacePlan: async () => {
expect.fail()
},
getSubscriptionData: async () => {
expect.fail()
},
upsertWorkspaceSubscription: async () => {
expect.fail()
}
})({ sessionId, subscriptionId })
expect(err.message).to.equal(new WorkspaceAlreadyPaidError().message)
})
}),
(['monthly', 'yearly'] as const).forEach((billingInterval) => {
it(`sets the billingCycleEnd end for ${billingInterval} based on the checkoutSession.billingInterval`, async () => {
const sessionId = cryptoRandomString({ length: 10 })
const subscriptionId = cryptoRandomString({ length: 10 })
const workspaceId = cryptoRandomString({ length: 10 })
const storedCheckoutSession: CheckoutSession = {
billingInterval,
id: sessionId,
paymentStatus: 'unpaid',
url: 'https://example.com',
workspaceId,
workspacePlan: 'business',
createdAt: new Date(),
updatedAt: new Date()
}
let storedWorkspacePlan: PaidWorkspacePlan | undefined = undefined
const subscriptionData: SubscriptionData = {
customerId: cryptoRandomString({ length: 10 }),
subscriptionId: cryptoRandomString({ length: 10 }),
products: [
{
priceId: cryptoRandomString({ length: 10 }),
productId: cryptoRandomString({ length: 10 }),
quantity: 10,
subscriptionItemId: cryptoRandomString({ length: 10 })
}
],
status: 'active',
cancelAt: null
}
let storedWorkspaceSubscriptionData: WorkspaceSubscription | undefined =
undefined
await completeCheckoutSessionFactory({
getCheckoutSession: async () => storedCheckoutSession,
updateCheckoutSessionStatus: async ({ paymentStatus }) => {
storedCheckoutSession.paymentStatus = paymentStatus
},
upsertPaidWorkspacePlan: async ({ workspacePlan }) => {
storedWorkspacePlan = workspacePlan
},
getSubscriptionData: async () => subscriptionData,
upsertWorkspaceSubscription: async ({ workspaceSubscription }) => {
storedWorkspaceSubscriptionData = workspaceSubscription
}
})({ sessionId, subscriptionId })
expect(storedCheckoutSession.paymentStatus).to.equal('paid')
expect(storedWorkspacePlan).to.deep.equal({
workspaceId,
name: storedCheckoutSession.workspacePlan,
status: 'valid'
})
expect(storedWorkspaceSubscriptionData!.billingInterval).to.equal(
storedCheckoutSession.billingInterval
)
expect(storedWorkspaceSubscriptionData!.subscriptionData).to.equal(
subscriptionData
)
let billingCycleEndsIn: number
const expectedCycleLength = 1
switch (billingInterval) {
case 'monthly':
billingCycleEndsIn =
storedWorkspaceSubscriptionData!.currentBillingCycleEnd.getMonth() -
new Date().getMonth()
break
case 'yearly':
billingCycleEndsIn =
storedWorkspaceSubscriptionData!.currentBillingCycleEnd.getFullYear() -
new Date().getFullYear()
break
}
expect(billingCycleEndsIn).to.be.equal(expectedCycleLength)
})
})
})
})