Skip to content

Commit

Permalink
add test that userPaymentPut can handle requests that include a subsc…
Browse files Browse the repository at this point in the history
…ription storage price
  • Loading branch information
gobengo committed Sep 10, 2022
1 parent a99ae8b commit 8bfd82e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
16 changes: 13 additions & 3 deletions packages/api/src/utils/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -108,7 +117,8 @@ export function createMockBillingService () {
id: methodId
})
},
paymentMethodSaves
paymentMethodSaves,
storageSubscriptionSaves
}
return billing
}
Expand Down
35 changes: 30 additions & 5 deletions packages/api/test/user-payment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down

0 comments on commit 8bfd82e

Please sign in to comment.