/api/user/subscriptions/delete-customer (DELETE)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.DeleteCustomer.delete(req)Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid-account | ineligible accessing account |
invalid-customerid | missing querystring customerid |
invalid querystring customerid |
NodeJS source (view on github)
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.customerid) {
throw new Error('invalid-customerid')
}
const customer = await global.api.user.subscriptions.Customer.get(req)
if (!customer) {
throw new Error('invalid-customerid')
}
await stripeCache.execute('customers', 'del', req.query.customerid, req.stripeKey)
await subscriptions.Storage.Customer.destroy({
where: {
customerid: req.query.customerid,
appid: req.appid || global.appid
}
})
return true
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/subscriptions/delete-customer', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-customerid', () => {
it('missing querystring customerid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/delete-customer')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
it('invalid querystring customerid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/delete-customer?customerid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-customerid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
country: 'US'
})
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/delete-customer?customerid=${user.customer.customerid}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('boolean', async () => {
const user = await TestHelper.createUser()
await TestHelper.createCustomer(user, {
email: user.profile.contactEmail,
country: 'US'
})
const req = TestHelper.createRequest(`/api/user/subscriptions/delete-customer?customerid=${user.customer.customerid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})