/api/user/set-account-profile (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.SetAccountProfile.patch(req)Returns object
{
"accountid": "acct_69ac375d7b5fe599",
"object": "account",
"appid": "tests_1656038581",
"profileid": "prof_de53dbe1f5c61b24",
"sessionKeyNumber": 1,
"lastSignedInAt": "2022-06-24T02:43:01.000Z",
"owner": true,
"ownerSince": "2022-06-24T02:43:01.000Z",
"administrator": true,
"administratorSince": "2022-06-24T02:43:01.000Z",
"createdAt": "2022-06-24T02:43:01.814Z",
"updatedAt": "2022-06-24T02:43:01.852Z"
}
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-profileid | missing posted profileid |
invalid posted profileid |
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')
}
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-accountid')
}
if (!req.body || !req.body.profileid) {
throw new Error('invalid-profileid')
}
req.query.profileid = req.body.profileid
const profile = await global.api.user.Profile.get(req)
if (!profile) {
throw new Error('invalid-profileid')
}
await dashboard.Storage.Account.update({
profileid: req.query.profileid
}, {
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-profile', () => {
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-account-profile')
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-profile?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-profile?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-profileid', () => {
it('missing posted profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('invalid posted profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: 'invalid'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const profile1 = user.profile
await TestHelper.createProfile(user, {
'full-name': user.profile.fullName,
'contact-email': user.profile.contactEmail,
default: 'true'
})
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: profile1.profileid
}
req.filename = __filename
req.saveResponse = true
const account = await req.patch()
assert.strictEqual(account.object, 'account')
})
})
})