/api/user/reset-account-deleted (PATCH)
await global.api.user.ResetAccountDeleted.patch(req) Located in Dashboard API
Returns object
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 posted username | |
invalid-username | missing posted username |
invalid posted password | |
invalid-password | missing posted password |
invalid-account | credentialed account is not scheduled for deletion |
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 = {
auth: false,
patch: async (req) => {
if (!req.body) {
throw new Error('invalid-username')
}
if (!req.body.username) {
throw new Error('invalid-username')
}
if (!req.body.username || !req.body.username.length) {
throw new Error('invalid-username')
}
if (global.minimumUsernameLength > req.body.username.length) {
throw new Error('invalid-username-length')
}
if (!req.body.password || !req.body.password.length) {
throw new Error('invalid-password')
}
if (global.minimumPasswordLength > req.body.password.length) {
throw new Error('invalid-password-length')
}
let dashboardEncryptionKey = global.dashboardEncryptionKey
if (req.server) {
dashboardEncryptionKey = req.server.dashboardEncryptionKey || dashboardEncryptionKey
}
const usernameHash = await dashboard.Hash.sha512Hash(req.body.username, dashboardEncryptionKey)
const accountid = await dashboard.Storage.read(`${req.appid}/map/usernames/${usernameHash}`)
if (!accountid) {
throw new Error('invalid-username')
}
const passwordHash = await dashboard.StorageObject.getProperty(`${req.appid}/account/${accountid}`, 'passwordHash')
const validPassword = await dashboard.Hash.bcryptHashCompare(req.body.password, passwordHash, dashboardEncryptionKey)
if (!validPassword) {
throw new Error('invalid-password')
}
const query = req.query
req.query = { accountid }
const account = await global.api.administrator.Account.get(req)
if (!account) {
throw new Error('invalid-account')
}
if (!account.deleted) {
throw new Error('invalid-account')
}
if (account.deleted < dashboard.Timestamp.now) {
throw new Error('invalid-account')
}
await dashboard.StorageObject.removeProperty(`${req.appid}/account/${account.accountid}`, 'deleted')
await dashboard.StorageList.remove(`${req.appid}/deleted/accounts`, account.accountid)
req.account = account
const accountNow = await global.api.user.Account.get(req)
req.query = query
return accountNow
}
}
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/reset-account-deleted', () => {
describe('exceptions', () => {
describe('invalid-username', () => {
it('missing posted username', async () => {
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.body = {
username: '',
password: 'password'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-username')
})
it('invalid posted username', async () => {
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.body = {
username: 'invalid',
password: 'password'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-username')
})
})
describe('invalid-password', () => {
it('missing posted password', async () => {
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.body = {
username: 'username',
password: ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-password')
})
it('invalid posted password', async () => {
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.body = {
username: user.account.username,
password: 'invalid'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-password')
})
})
describe('invalid-account', () => {
it('credentialed account is not scheduled for deletion', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.account = user.account
req.session = user.session
req.body = {
username: user.account.username,
password: user.account.password
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest('/api/user/reset-account-deleted')
req.body = {
username: user.account.username,
password: user.account.password
}
req.filename = __filename
req.saveResponse = true
const accountNow = await req.patch()
assert.strictEqual(undefined, accountNow.deleted)
})
})
})