/api/user/sessions (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.Sessions.get(req)Returns array
[
{
"sessionid": "sess_6c7eb3e8360aceea",
"object": "session",
"appid": "tests_1656038580",
"accountid": "acct_b3327ce0ecc1a9ae",
"duration": 1200,
"csrfToken": "39d04d40893eaf1f9935c00bfe419824c9714dd0b01a953c9331f10c45415bcfaf2a358549b830beff6d368a89bb58677beaf6f9ea9681fb1280e062c21d2691",
"expiresAt": "2022-06-24T03:03:00.000Z",
"lastVerifiedAt": "2022-06-24T02:43:00.000Z",
"ended": false,
"createdAt": "2022-06-24T02:43:00.490Z",
"updatedAt": "2022-06-24T02:43:00.490Z"
},
{
"sessionid": "sess_60c570c6525cfc59",
"object": "session",
"appid": "tests_1656038580",
"accountid": "acct_b3327ce0ecc1a9ae",
"duration": 1200,
"csrfToken": "b8b1a630b621c5f921f9877adafd69e4b228dd93f35db94524f290de5560222b805a3e08ba759ad827d097937266f8474f7a15235f283eae21bf606d3fa02350",
"expiresAt": "2022-06-24T03:03:00.000Z",
"lastVerifiedAt": "2022-06-24T02:43:00.000Z",
"ended": false,
"createdAt": "2022-06-24T02:43:00.481Z",
"updatedAt": "2022-06-24T02:43:00.481Z"
},
{
"sessionid": "sess_dac190d867d8c9f5",
"object": "session",
"appid": "tests_1656038580",
"accountid": "acct_b3327ce0ecc1a9ae",
"duration": 1200,
"csrfToken": "aad19fd88f99ea59a2b855ce01d4bf9ef59300810ac4d87b66b9ab5d501bab34bd813e769811476c52db7d8d49c5363905eada67654c342ebd35b7f90aeb8a48",
"expiresAt": "2022-06-24T03:03:00.000Z",
"lastVerifiedAt": "2022-06-24T02:43:00.000Z",
"ended": false,
"createdAt": "2022-06-24T02:43:00.472Z",
"updatedAt": "2022-06-24T02:43:00.473Z"
}
]
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
all | boolean | optional | URL |
limit | integer | optional | URL |
offset | integer | optional | URL |
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 |
NodeJS source (view on github)
const dashboard = require('../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-accountid')
}
let sessionids
if (req.query.all) {
sessionids = await dashboard.Storage.Session.findAll({
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
},
attributes: ['sessionid'],
order: [
['createdAt', 'DESC']
]
})
} else {
const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
sessionids = await dashboard.Storage.Session.findAll({
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
},
offset,
limit,
attributes: ['sessionid'],
order: [
['createdAt', 'DESC']
]
})
}
const sessions = []
for (const sessionData of sessionids) {
req.query.sessionid = sessionData.dataValues.sessionid
const session = await global.api.user.Session.get(req)
sessions.push(session)
}
return sessions
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/sessions', function () {
const cachedResponses = {}
const cachedSessions = []
before(async () => {
await TestHelper.setupBeforeEach()
const user = await TestHelper.createUser()
cachedSessions.unshift(user.session.sessionid)
for (let i = 0, len = global.pageSize + 1; i < len; i++) {
await TestHelper.createSession(user)
cachedSessions.unshift(user.session.sessionid)
}
const req1 = TestHelper.createRequest(`/api/user/sessions?accountid=${user.account.accountid}&offset=1`)
req1.account = user.account
req1.session = user.session
cachedResponses.offset = await req1.get()
const req2 = TestHelper.createRequest(`/api/user/sessions?accountid=${user.account.accountid}&limit=1`)
req2.account = user.account
req2.session = user.session
cachedResponses.limit = await req2.get()
const req3 = TestHelper.createRequest(`/api/user/sessions?accountid=${user.account.accountid}&all=true`)
req3.account = user.account
req3.session = user.session
cachedResponses.all = await req3.get()
const req4 = TestHelper.createRequest(`/api/user/sessions?accountid=${user.account.accountid}`)
req4.account = user.account
req4.session = user.session
req4.filename = __filename
req4.saveResponse = true
cachedResponses.returns = await req4.get()
global.pageSize = 3
cachedResponses.pageSize = await req4.get()
})
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/sessions')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} 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/sessions?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} 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/sessions?accountid=${user2.account.accountid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('receives', () => {
it('optional querystring offset (integer)', async () => {
const offset = 1
const sessionsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(sessionsNow[i].sessionid, cachedSessions[offset + i])
}
})
it('optional querystring limit (integer)', async () => {
const limit = 1
const sessionsNow = cachedResponses.limit
assert.strictEqual(sessionsNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const sessionsNow = cachedResponses.all
assert.strictEqual(sessionsNow.length, cachedSessions.length)
})
})
describe('returns', () => {
it('array', async () => {
const sessions = cachedResponses.returns
assert.strictEqual(sessions.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
global.pageSize = 3
const sessionsNow = cachedResponses.pageSize
assert.strictEqual(sessionsNow.length, global.pageSize)
})
})
})