/api/user/profiles (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.Profiles.get(req)Returns array
[
{
"profileid": "prof_c29431d1fad71ea2",
"object": "profile",
"appid": "tests_1656038577",
"accountid": "acct_a2fa1b57b86de78d",
"companyName": null,
"fullName": "Essie Botsford",
"contactEmail": "Essie_Botsford@yahoo.com",
"displayEmail": null,
"displayName": null,
"phone": null,
"occupation": null,
"location": null,
"dob": null,
"website": null,
"fields": null,
"createdAt": "2022-06-24T02:42:57.995Z",
"updatedAt": "2022-06-24T02:42:57.995Z"
},
{
"profileid": "prof_da0376800822958a",
"object": "profile",
"appid": "tests_1656038577",
"accountid": "acct_a2fa1b57b86de78d",
"companyName": null,
"fullName": "Essie Botsford",
"contactEmail": "Essie_Botsford@yahoo.com",
"displayEmail": null,
"displayName": null,
"phone": null,
"occupation": null,
"location": null,
"dob": null,
"website": null,
"fields": null,
"createdAt": "2022-06-24T02:42:57.989Z",
"updatedAt": "2022-06-24T02:42:57.989Z"
},
{
"profileid": "prof_4c21b448469962cc",
"object": "profile",
"appid": "tests_1656038577",
"accountid": "acct_a2fa1b57b86de78d",
"companyName": null,
"fullName": "Essie Botsford",
"contactEmail": "Essie_Botsford@yahoo.com",
"displayEmail": null,
"displayName": null,
"phone": null,
"occupation": null,
"location": null,
"dob": null,
"website": null,
"fields": null,
"createdAt": "2022-06-24T02:42:57.983Z",
"updatedAt": "2022-06-24T02:42:57.983Z"
}
]
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 profileids
if (req.query.all) {
profileids = await dashboard.Storage.Profile.findAll({
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
},
attributes: ['profileid'],
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
profileids = await dashboard.Storage.Profile.findAll({
where: {
accountid: req.query.accountid,
appid: req.appid || global.appid
},
attributes: ['profileid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
const profiles = []
for (const profileData of profileids) {
req.query.profileid = profileData.dataValues.profileid
const profile = await global.api.user.Profile.get(req)
profiles.push(profile)
}
return profiles
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/profiles', function () {
const cachedResponses = {}
const cachedProfiles = []
before(async () => {
await TestHelper.setupBeforeEach()
const user = await TestHelper.createUser()
cachedProfiles.push(user.profile.profileid)
for (let i = 0, len = global.pageSize + 1; i < len; i++) {
await TestHelper.createProfile(user, {
'full-name': user.profile.fullName,
'contact-email': user.profile.contactEmail,
default: 'true'
})
cachedProfiles.unshift(user.profile.profileid)
}
const req1 = TestHelper.createRequest(`/api/user/profiles?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/profiles?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/profiles?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/profiles?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/profiles')
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/profiles?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/profiles?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 profilesNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(profilesNow[i].profileid, cachedProfiles[offset + i])
}
})
it('optional querystring limit (integer)', async () => {
const limit = 1
const profilesNow = cachedResponses.limit
assert.strictEqual(profilesNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const profilesNow = cachedResponses.all
assert.strictEqual(profilesNow.length, cachedProfiles.length)
})
})
describe('returns', () => {
it('array', async () => {
const profilesNow = cachedResponses.returns
assert.strictEqual(profilesNow.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
global.pageSize = 3
const profilesNow = cachedResponses.pageSize
assert.strictEqual(profilesNow.length, global.pageSize)
})
})
})