/api/user/set-session-ended (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.SetSessionEnded.patch(req)Returns object
{
"sessionid": "sess_b0b52a2d89a5669d",
"object": "session",
"appid": "tests_1656038582",
"accountid": "acct_b9d3263d58091030",
"duration": 1200,
"csrfToken": "2f91c29829e4bea756a85778f23bccc9ac499e80bfe101351523c6064194291d9afc3e7930fb80f7cd33d1327566e707ab39ca96cec430096aa85a048e220916",
"expiresAt": "2022-06-24T03:03:02.000Z",
"lastVerifiedAt": "2022-06-24T02:43:02.000Z",
"endedAt": "2022-06-24T02:43:02.000Z",
"ended": true,
"createdAt": "2022-06-24T02:43:02.624Z",
"updatedAt": "2022-06-24T02:43:02.645Z"
}
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 sessionid |
invalid-session | querystring sessionid is already ended |
invalid-sessionid | missing querystring sessionid |
invalid querystring sessionid |
NodeJS source (view on github)
const dashboard = require('../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.sessionid) {
throw new Error('invalid-sessionid')
}
const session = await global.api.user.Session.get(req)
if (!session) {
throw new Error('invalid-sessionid')
}
if (session.ended) {
throw new Error('invalid-session')
}
if (session.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
await dashboard.Storage.Session.update({
endedAt: new Date()
}, {
where: {
sessionid: req.query.sessionid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.sessionid)
return global.api.user.Session.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
/* eslint-env mocha */
describe('/api/user/set-session-ended', () => {
describe('exceptions', () => {
describe('invalid-sessionid', () => {
it('missing querystring sessionid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-session-ended')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-sessionid')
})
it('invalid querystring sessionid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-session-ended?sessionid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-sessionid')
})
})
describe('invalid-session', () => {
it('querystring sessionid is already ended', async () => {
const user = await TestHelper.createUser()
const session1 = user.session
await TestHelper.endSession(user)
await TestHelper.createSession(user)
const req = TestHelper.createRequest(`/api/user/set-session-ended?sessionid=${session1.sessionid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-session')
})
})
describe('invalid-account', () => {
it('ineligible querystring sessionid', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-session-ended?sessionid=${user2.session.sessionid}`)
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('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-session-ended?sessionid=${user.session.sessionid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const sessionNow = await req.patch()
assert.strictEqual(sessionNow.object, 'session')
})
})
})