/api/user/delete-profile (DELETE)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.DeleteProfile.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 querystring profileid |
invalid-profile | querystring profileid is default contact profile |
invalid-profileid | missing querystring profileid |
invalid querystring profileid |
NodeJS source (view on github)
const dashboard = require('../../../../index.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.profileid) {
throw new Error('invalid-profileid')
}
if (req.query.profileid === req.account.profileid) {
throw new Error('invalid-profile')
}
const profile = await global.api.user.Profile.get(req)
if (!profile) {
throw new Error('invalid-profileid')
}
await dashboard.Storage.Profile.destroy({
where: {
profileid: req.query.profileid,
appid: req.appid || global.appid
}
})
await dashboard.Storage.Account.update({
profileLastDeleted: new Date()
}, {
where: {
accountid: profile.accountid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(profile.accountid)
await dashboard.StorageCache.remove(profile.profileid)
return true
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/delete-profile', () => {
describe('exceptions', () => {
describe('invalid-profileid', () => {
it('missing querystring profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/delete-profile')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('invalid querystring profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/delete-profile?profileid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
describe('invalid-profile', () => {
it('querystring profileid is default contact profile', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/delete-profile?profileid=${user.profile.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profile')
})
})
describe('invalid-account', () => {
it('ineligible querystring profileid', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/delete-profile?profileid=${user2.account.profileid}`)
req.account = user.account
req.session = user.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()
const profile1 = user.profile
await TestHelper.createProfile(user, {
'full-name': user.profile.fullName,
'contact-email': user.profile.contactEmail,
default: 'true'
})
const req = TestHelper.createRequest(`/api/user/delete-profile?profileid=${profile1.profileid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})