/api/user/delete-profile (DELETE)
await global.api.user.DeleteProfile.delete(req) Located in Dashboard API
Returns boolean
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 querystring profileid | |
invalid-profileid | missing querystring profileid |
invalid-profile | querystring profileid is default contact profile |
invalid-account | ineligible querystring profileid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request 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.delete(`${req.appid}/profile/${req.query.profileid}`)
await dashboard.StorageObject.setProperty(`${req.appid}/account/${req.account.accountid}`, 'profileLastDeleted', dashboard.Timestamp.now)
await dashboard.StorageList.remove(`${req.appid}/profiles`, req.query.profileid)
await dashboard.StorageList.remove(`${req.appid}/account/profiles/${req.account.accountid}`, req.query.profileid)
return true
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* 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, {
'first-name': user.profile.firstName,
'last-name': user.profile.lastName,
'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)
})
})
})