Skip to content
Draft
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
28 changes: 28 additions & 0 deletions browser_tests/fixtures/utils/liveCloudBillingPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,31 @@ describe('Disposable payment permissions', () => {
).toBe(false)
})
})

describe('Subscription lifecycle permissions', () => {
it.for([
'/api/billing/subscription/cancel',
'/api/billing/subscription/resubscribe'
])('limits %s to opted-in sandbox tests', (path) => {
const url = new URL(path, config.PLAYWRIGHT_SETUP_API_URL)
expect(isLiveCloudMutationAllowed(url, 'POST', config)).toBe(false)
expect(
isLiveCloudMutationAllowed(url, 'POST', {
...config,
allowPayments: true
})
).toBe(true)
const production = {
...config,
PLAYWRIGHT_SETUP_API_URL: 'https://cloud.comfy.org',
allowPayments: true
}
expect(
isLiveCloudMutationAllowed(
new URL(path, production.PLAYWRIGHT_SETUP_API_URL),
'POST',
production
)
).toBe(false)
})
})
9 changes: 8 additions & 1 deletion browser_tests/fixtures/utils/liveCloudBillingPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ function isPaymentPostAllowed(url: URL, backend: string): boolean {
url.pathname === '/ajax/metrics_batch'
)
return true
if (url.origin === backend && url.pathname === '/api/billing/payment-portal')
if (
url.origin === backend &&
[
'/api/billing/payment-portal',
'/api/billing/subscription/cancel',
'/api/billing/subscription/resubscribe'
].includes(url.pathname)
)
return true
return (
url.origin === 'https://api.stripe.com' &&
Expand Down
27 changes: 27 additions & 0 deletions browser_tests/tests/liveCloud/disposable/cancelReactivate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { verifyCancellationRecovery } from '@e2e/tests/liveCloud/helpers/lifecycleScenarios'
import {
expect,
liveCloudDisposableBillingFixture as test
} from '@e2e/fixtures/liveCloudDisposableBillingFixture'

test.describe(
'Real Cloud subscription lifecycle',
{ tag: ['@cloud-live'] },
() => {
test.setTimeout(240_000)

test('cancels and reactivates without changing the balance', async ({
disposableCheckout
}, testInfo) => {
await disposableCheckout.checkout.completeCheckout(
disposableCheckout.billingSession,
testInfo
)
const recovery = await verifyCancellationRecovery(
disposableCheckout.billingSession,
testInfo
)
expect(recovery.balanceAfterCents).toBe(recovery.balanceBeforeCents)
})
}
)
24 changes: 24 additions & 0 deletions browser_tests/tests/liveCloud/disposable/planTransitions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { verifyPlanTransitions } from '@e2e/tests/liveCloud/helpers/lifecycleScenarios'
import {
expect,
liveCloudDisposableBillingFixture as test
} from '@e2e/fixtures/liveCloudDisposableBillingFixture'

test.describe('Real Cloud plan transitions', { tag: ['@cloud-live'] }, () => {
test.setTimeout(240_000)

test('upgrades now and schedules a downgrade', async ({
disposableCheckout
}, testInfo) => {
const completion = await disposableCheckout.checkout.completeCheckout(
disposableCheckout.billingSession,
testInfo
)
expect(completion.paymentMethodCount).toBe(1)
const transitions = await verifyPlanTransitions(
disposableCheckout.billingSession,
testInfo
)
expect(transitions.scheduledPlan).toBe('standard-monthly')
})
})
201 changes: 201 additions & 0 deletions browser_tests/tests/liveCloud/helpers/lifecycleScenarios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import {
zBillingBalanceResponse,
zBillingStatusResponse,
zBillingOpStatusResponse,
zCancelSubscriptionAcceptedResponse,
zCancelSubscriptionResponse,
zResubscribeResponse,
zPreviewSubscribeResponse,
zSubscribeResponse
} from '@comfyorg/ingest-types/zod'
import { z } from 'zod'
import { expect } from '@playwright/test'
import type { TestInfo } from '@playwright/test'
import type { LiveCloudBillingSession } from '@e2e/fixtures/helpers/LiveCloudBillingSession'
async function expectSucceeded(
session: LiveCloudBillingSession,
operationId: string
) {
await expect
.poll(
async () => {
const operation = await session.read(
`/api/billing/ops/${encodeURIComponent(operationId)}`,
zBillingOpStatusResponse
)
return operation.status
},
{ timeout: 60_000 }
)
.toBe('succeeded')
}
export async function verifyCancellationRecovery(
session: LiveCloudBillingSession,
testInfo: TestInfo
) {
const balanceBefore = await session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
const cancel = await session.post(
'/api/billing/subscription/cancel',
{ idempotency_key: crypto.randomUUID() },
z.union([zCancelSubscriptionResponse, zCancelSubscriptionAcceptedResponse]),
[200, 202]
)
await expectSucceeded(session, cancel.billing_op_id)
await expect
.poll(() => session.read('/api/billing/status', zBillingStatusResponse))
.toMatchObject({
plan_slug: 'creator-monthly',
subscription_tier: 'CREATOR'
})
const canceled = await session.read(
'/api/billing/status',
zBillingStatusResponse
)
expect(canceled.cancel_at).toBeTruthy()
const resubscribe = await session.post(
'/api/billing/subscription/resubscribe',
{ idempotency_key: crypto.randomUUID() },
zResubscribeResponse
)
if (resubscribe.status === 'pending') {
await expectSucceeded(session, resubscribe.billing_op_id)
}
await expect
.poll(async () => {
const status = await session.read(
'/api/billing/status',
zBillingStatusResponse
)
return {
cancelAt: status.cancel_at,
planSlug: status.plan_slug,
subscriptionStatus: status.subscription_status
}
})
.toMatchObject({
cancelAt: undefined,
planSlug: 'creator-monthly',
subscriptionStatus: 'active'
})
const balanceAfter = await session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
expect(balanceAfter.amount_micros).toBe(balanceBefore.amount_micros)
const result = {
cancelOperationId: cancel.billing_op_id,
reactivateOperationId: resubscribe.billing_op_id,
balanceBeforeCents: balanceBefore.amount_micros,
balanceAfterCents: balanceAfter.amount_micros
}
await testInfo.attach('cancellation-recovery.json', {
body: JSON.stringify(result),
contentType: 'application/json'
})
return result
}
export async function verifyPlanTransitions(
session: LiveCloudBillingSession,
testInfo: TestInfo
) {
const balanceBeforeUpgrade = await session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
const upgradePreview = await session.post(
'/api/billing/preview-subscribe',
{ plan_slug: 'pro-monthly' },
zPreviewSubscribeResponse
)
expect(upgradePreview).toMatchObject({
allowed: true,
transition_type: 'upgrade'
})
const upgradeRequest: Record<string, number | string> = {
plan_slug: 'pro-monthly'
}
if (upgradePreview.quote_id) {
upgradeRequest.quote_id = upgradePreview.quote_id
}
if (upgradePreview.quote_version !== undefined) {
upgradeRequest.quote_version = upgradePreview.quote_version
}
if (upgradePreview.proration_at) {
upgradeRequest.proration_at = upgradePreview.proration_at
}
const upgrade = await session.post(
'/api/billing/subscribe',
upgradeRequest,
zSubscribeResponse
)
await expectSucceeded(session, upgrade.billing_op_id)
await expect
.poll(() => session.read('/api/billing/status', zBillingStatusResponse))
.toMatchObject({
plan_slug: 'pro-monthly',
subscription_tier: 'PRO'
})
const expectedUpgradeBalance =
balanceBeforeUpgrade.amount_micros +
Number(upgradePreview.credits_today_cents)
await expect
.poll(
async () =>
(await session.read('/api/billing/balance', zBillingBalanceResponse))
.amount_micros
)
.toBe(expectedUpgradeBalance)
const downgradePreview = await session.post(
'/api/billing/preview-subscribe',
{ plan_slug: 'standard-monthly' },
zPreviewSubscribeResponse
)
expect(downgradePreview).toMatchObject({
allowed: true,
transition_type: 'downgrade'
})
const downgradeRequest: Record<string, number | string> = {
plan_slug: 'standard-monthly'
}
if (downgradePreview.quote_id) {
downgradeRequest.quote_id = downgradePreview.quote_id
}
if (downgradePreview.quote_version !== undefined) {
downgradeRequest.quote_version = downgradePreview.quote_version
}
if (downgradePreview.proration_at) {
downgradeRequest.proration_at = downgradePreview.proration_at
}
const downgrade = await session.post(
'/api/billing/subscribe',
downgradeRequest,
zSubscribeResponse
)
await expectSucceeded(session, downgrade.billing_op_id)
await expect
.poll(() => session.read('/api/billing/status', zBillingStatusResponse))
.toMatchObject({
plan_slug: 'pro-monthly',
scheduled_change: { plan_slug: 'standard-monthly' }
})
const balanceAfterDowngrade = await session.read(
'/api/billing/balance',
zBillingBalanceResponse
)
expect(balanceAfterDowngrade.amount_micros).toBe(expectedUpgradeBalance)
const result = {
upgradeOperationId: upgrade.billing_op_id,
downgradeOperationId: downgrade.billing_op_id,
balanceBeforeUpgradeCents: balanceBeforeUpgrade.amount_micros,
balanceAfterDowngradeCents: balanceAfterDowngrade.amount_micros,
scheduledPlan: 'standard-monthly'
}
await testInfo.attach('plan-transitions.json', {
body: JSON.stringify(result),
contentType: 'application/json'
})
return result
}
9 changes: 9 additions & 0 deletions docs/testing/cloud-billing-e2e.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ Traces start after permanent-account sign-in and contain authenticated network
traffic. Keep reports private. Disposable accounts require no reusable credentials.
Completed-payment assertions verify saved cards through the real hosted billing
portal, because Staging disables the embedded-checkout saved-card API.

## Subscription lifecycle

PR #17548 depends only on #17481. Run `--project=cloud-live-disposable` for
cancellation/reactivation and plan transitions. The shared setup provisions a
paid sandbox subscription; all lifecycle assertions and endpoint permissions live
in this PR. Cancellation/reactivation preserves balance, upgrade grants the quote,
and scheduled downgrade grants nothing. Paid setup verifies the saved card through
the hosted billing portal.
Loading