diff --git a/packages/api/src/utils/billing.js b/packages/api/src/utils/billing.js index 84b35ec7bb..851ad061a4 100644 --- a/packages/api/src/utils/billing.js +++ b/packages/api/src/utils/billing.js @@ -91,12 +91,21 @@ export function randomString () { } /** - * @returns {import('src/utils/billing-types.js').BillingService & { paymentMethodSaves: Array<{ customerId: string, methodId: string }> }} + * @typedef {object} MockBillingService + * @property {Array<{ customerId: string, methodId: string }>} paymentMethodSaves + * @property {Array<{ customerId: string, storageSubscription: any }>} storageSubscriptionSaves + * @property {import('./billing-types').BillingService['getPaymentMethod']} getPaymentMethod + * @property {import('./billing-types').BillingService['savePaymentMethod']} savePaymentMethod + */ + +/** + * @returns {MockBillingService} */ export function createMockBillingService () { + const storageSubscriptionSaves = [] const paymentMethodSaves = [] const customerToPaymentMethod = new Map() - /** @type {import('src/utils/billing-types.js').BillingService & { paymentMethodSaves: Array<{ customerId: string, methodId: string }> }} */ + /** @type {MockBillingService} */ const billing = { async getPaymentMethod (customerId) { const pm = customerToPaymentMethod.get(customerId) @@ -108,7 +117,8 @@ export function createMockBillingService () { id: methodId }) }, - paymentMethodSaves + paymentMethodSaves, + storageSubscriptionSaves } return billing } diff --git a/packages/api/test/user-payment.spec.js b/packages/api/test/user-payment.spec.js index 00035b36b5..9d39ae6b2d 100644 --- a/packages/api/test/user-payment.spec.js +++ b/packages/api/test/user-payment.spec.js @@ -121,11 +121,7 @@ describe('userPaymentPut', () => { const desiredPaymentMethodId = `pm_${randomString()}` const paymentSettings = { method: { id: desiredPaymentMethodId } } const authorization = createBearerAuthorization(AuthorizationTestContext.use(this).createUserToken()) - const user = { _id: randomString(), issuer: randomString() } - const request = Object.assign( - createSaveUserPaymentSettingsRequest({ authorization, body: JSON.stringify(paymentSettings) }), - { auth: { user } } - ) + const request = createMockAuthenticatedRequest(createSaveUserPaymentSettingsRequest({ authorization, body: JSON.stringify(paymentSettings) })) const billing = createMockBillingService() const customers = createMockCustomerService() const env = { @@ -139,8 +135,37 @@ describe('userPaymentPut', () => { customers.mockCustomers.map(c => c.id).includes(billing.paymentMethodSaves[0].customerId), 'billing.paymentMethodSaves[0].customerId is in customers.mockCustomers') }) + it('saves storage subscription price', async function () { + const desiredPaymentSettings = { + method: { id: `pm_${randomString()}` }, + subscription: { storage: { price: `price_test_${randomString()}` } } + } + const request = ( + createMockAuthenticatedRequest( + createSaveUserPaymentSettingsRequest({ + authorization: createBearerAuthorization(AuthorizationTestContext.use(this).createUserToken()), + body: JSON.stringify(desiredPaymentSettings) + }))) + const env = { + billing: createMockBillingService(), + customers: createMockCustomerService() + } + const response = await userPaymentPut(request, env) + assert.equal(response.status, 202, 'response.status is 202') + // assert.equal(env.billing.storageSubscriptionSaves.length, 1, 'billing.storageSubscriptionSaves.length is 1') + // assert.ok( + // env.customers.mockCustomers.map(c => c.id).includes(env.billing.storageSubscriptionSaves[0].customerId), + // 'billing.storageSubscriptionSaves[0].customerId is in customers.mockCustomers') + }) }) +function createMockAuthenticatedRequest (_request) { + const request = Object.create(_request) + const user = { _id: randomString(), issuer: randomString() } + request.auth = { user } + return request +} + describe('/user/payment', () => { it('can userPaymentPut and then userPaymentGet', async function () { const env = {