/api/administrator/delete-account (DELETE)
await global.api.administrator.DeleteAccount.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 accountid | |
invalid-accountid | missing querystring accountid |
receieves | querystring accountid |
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.accountid) {
throw new Error('invalid-accountid')
}
const account = await global.api.administrator.Account.get(req)
if (!account) {
throw new Error('invalid-accountid')
}
await dashboard.Storage.delete(`${req.appid}/account/${req.query.accountid}`)
await dashboard.StorageList.remove(`${req.appid}/accounts`, req.query.accountid)
if (account.administrator) {
await dashboard.StorageList.remove(`${req.appid}/administrator/accounts`, req.query.accountid)
}
if (account.deleted) {
await dashboard.StorageList.remove(`${req.appid}/deleted/accounts`, req.query.accountid)
}
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/administrator/delete-account', () => {
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/delete-account')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/delete-account?accountid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
})
describe('receieves', () => {
it('querystring accountid', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest(`/api/administrator/delete-account?accountid=${user.account.accountid}`)
req.account = administrator.account
req.session = administrator.session
await req.delete()
const req2 = TestHelper.createRequest(`/api/administrator/account?accountid=${user.account.accountid}`)
req2.account = administrator.account
req2.session = administrator.session
let errorMessage
try {
await req2.get(req2)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('returns', () => {
it('boolean', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest(`/api/administrator/delete-account?accountid=${user.account.accountid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})