/api/administrator/reset-session-key (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.ResetSessionKey.patch(req)Returns object
{
"accountid": "acct_8485ad4a95d2eac8",
"object": "account",
"appid": "tests_1656038570",
"profileid": "prof_935527d4cb0ada24",
"sessionKeyNumber": 2,
"sessionKeyLastResetAt": "2022-06-24T02:42:51.000Z",
"lastSignedInAt": "2022-06-24T02:42:50.000Z",
"createdAt": "2022-06-24T02:42:50.974Z",
"updatedAt": "2022-06-24T02:42:51.008Z"
}
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 account is deleted |
invalid-accountid | missing querystring accountid |
invalid querystring accountid | |
requires | querystring accountid is not deleted |
NodeJS source (view on github)
const crypto = require('crypto')
const dashboard = require('../../../../index.js')
module.exports = {
/**
* End all of a user's sessions by generating a new
* session key that invalidates all previous sessions
*/
patch: 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')
}
if (account.deletedAt) {
throw new Error('invalid-account')
}
await dashboard.Storage.Account.update({
sessionKey: crypto.randomBytes(32).toString('hex'),
sessionKeyLastResetAt: new Date(),
sessionKeyNumber: account.sessionKeyNumber + 1
}, {
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.accountid)
return global.api.administrator.Account.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/administrator/reset-session-key', () => {
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/reset-session-key')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch()
} 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/reset-session-key?accountid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
})
describe('invalid-account', () => {
it('ineligible querystring account is deleted', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest(`/api/administrator/reset-session-key?accountid=${user.account.accountid}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('requires', () => {
it('querystring accountid is not deleted', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestHelper.createUser()
await TestHelper.setDeleted(user)
const req = TestHelper.createRequest(`/api/administrator/reset-session-key?accountid=${user.account.accountid}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/administrator/reset-session-key?accountid=${user.account.accountid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const accountNow = await req.patch()
assert.strictEqual(accountNow.object, 'account')
assert.notStrictEqual(accountNow.sessionKeyNumber, user.sessionKeyNumber)
assert.notStrictEqual(accountNow.sessionKeyLastResetAt, undefined)
assert.notStrictEqual(accountNow.sessionKeyLastResetAt, null)
})
})
})