/api/user/set-account-deleted (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.SetAccountDeleted.patch(req)Returns object
{
"accountid": "acct_8714b157efacf9cd",
"object": "account",
"appid": "tests_1656038580",
"profileid": "prof_c020c8887729c7b0",
"sessionKeyNumber": 1,
"deletedAt": "2022-07-01T02:43:01.000Z",
"lastSignedInAt": "2022-06-24T02:43:01.000Z",
"owner": true,
"ownerSince": "2022-06-24T02:43:00.000Z",
"administrator": true,
"administratorSince": "2022-06-24T02:43:00.000Z",
"createdAt": "2022-06-24T02:43:00.990Z",
"updatedAt": "2022-06-24T02:43:01.022Z"
}
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-accountid | missing querystring accountid |
invalid querystring accountid | |
invalid-password | missing posted password |
invalid posted password |
NodeJS source (view on github)
const dashboard = require('../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
let accountInfo
try {
accountInfo = await dashboard.Storage.Account.findOne({
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
}
})
} catch (error) {
}
if (!accountInfo) {
throw new Error('invalid-accountid')
}
if (accountInfo.dataValues.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
let dashboardEncryptionKey = global.dashboardEncryptionKey
if (req.server) {
dashboardEncryptionKey = req.server.dashboardEncryptionKey || dashboardEncryptionKey
}
if (!req.body || !req.body.password || !req.body.password.length) {
throw new Error('invalid-password')
}
const validPassword = await dashboard.Hash.bcryptHashCompare(req.body.password, accountInfo.dataValues.passwordHash, dashboardEncryptionKey)
if (!validPassword) {
throw new Error('invalid-password')
}
const now = new Date()
let deletedAt
if (global.deleteDelay) {
deletedAt = new Date(now.getFullYear(), now.getMonth(), now.getDate() + global.deleteDelay, now.getHours(), now.getMinutes(), now.getSeconds())
} else {
deletedAt = now
}
const updateClause = {
deletedAt
}
await dashboard.Storage.Account.update(updateClause, {
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.accountid)
return global.api.user.Account.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/set-account-deleted', () => {
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-account-deleted')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-account-deleted?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-deleted?accountid=${user2.account.accountid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-password', () => {
it('missing posted password', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-deleted?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
password: ''
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-password')
})
it('invalid posted password', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-deleted?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
password: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-password')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-deleted?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
password: user.account.password
}
req.filename = __filename
req.saveResponse = true
const account = await req.patch()
assert.strictEqual(account.object, 'account')
})
})
describe('configuration', () => {
it('environment DELETE_DELAY', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-deleted?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
password: user.account.password
}
global.deleteDelay = 3
const accountNow = await req.patch()
const days = Math.floor((new Date(accountNow.deletedAt).getTime() - new Date().getTime()) / 1000 / 60 / 60 / 24)
const days2 = Math.ceil((new Date(accountNow.deletedAt).getTime() - new Date().getTime()) / 1000 / 60 / 60 / 24)
assert.strictEqual(days === 3 || days2 === 3, true)
})
})
})