Skip to content

Commit

Permalink
add tmp GET /user/payment?mockSubscription=true
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Sep 10, 2022
1 parent 8bfd82e commit e121a6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/api/src/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,21 @@ const notifySlack = async (
* @param {Pick<import('./env').Env, 'billing'|'customers'>} env
*/
export async function userPaymentGet (request, env) {
let subscription = null
const { searchParams } = new URL(request.url)
if (searchParams.get('mockSubscription')) {
subscription = {
storage: {
price: 'price_mock_userPaymentGet_mockSubscription'
}
}
}
const userPaymentSettings = await getPaymentSettings({
billing: env.billing,
customers: env.customers,
user: { id: request.auth.user._id }
})
return new JSONResponse(userPaymentSettings)
return new JSONResponse({ ...userPaymentSettings, subscription })
}

/**
Expand Down
19 changes: 18 additions & 1 deletion packages/api/test/user-payment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function createBearerAuthorization (bearerToken) {
* @param {object} arg
* @param {string} [arg.method] - method of request
* @param {string} [arg.authorization] - authorization header value
* @param {Record<string,string>} [arg.searchParams]
*/
function createUserPaymentRequest (arg = {}) {
const { path, baseUrl, authorization, accept, method } = {
Expand All @@ -25,8 +26,12 @@ function createUserPaymentRequest (arg = {}) {
method: 'get',
...arg
}
const url = new URL(path, baseUrl)
for (const [key, value] of Object.entries(arg.searchParams || {})) {
url.searchParams.set(key, value)
}
return new Request(
new URL(path, baseUrl),
url,
{
headers: {
accept,
Expand Down Expand Up @@ -58,6 +63,18 @@ describe('GET /user/payment', () => {
assert.equal(typeof userPaymentSettings, 'object')
assert.ok(!userPaymentSettings.method, 'userPaymentSettings.method is falsy')
})
it('supports ?mockSubscription=true', async function () {
const request = createUserPaymentRequest({
authorization: createBearerAuthorization(AuthorizationTestContext.use(this).createUserToken()),
searchParams: {
mockSubscription: 'true'
}
})
const response = await fetch(request)
assert.equal(response.status, 200, 'response.status is 200')
const paymentSettings = await response.json()
assert.equal(typeof paymentSettings?.subscription?.storage?.price, 'string', 'paymentSettings.subscription.storage.price is a string')
})
})

/**
Expand Down

0 comments on commit e121a6b

Please sign in to comment.