/api/user/profile (GET)
await global.api.user.Profile.get(req) Located in Dashboard API
Returns object
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 querystring profileid | |
invalid-profileid | missing querystring profileid |
invalid-account | ineligible querystring profileid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.profileid) {
throw new Error('invalid-profileid')
}
const storage = req.storage || dashboard
let profile
if (req.cacheData && req.cacheData[req.query.profileid]) {
profile = req.cacheData[req.query.profileid]
} else {
profile = await storage.Storage.read(`${req.appid}/profile/${req.query.profileid}`)
}
if (!profile) {
throw new Error('invalid-profileid')
}
try {
profile = JSON.parse(profile)
} catch (error) {
}
if (!profile || profile.object !== 'profile') {
throw new Error('invalid-profileid')
}
if (profile.accountid !== req.account.accountid) {
throw new Error('invalid-account')
}
return profile
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/profile', () => {
describe('exceptions', () => {
describe('invalid-profileid', () => {
it('missing querystring profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/profile')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('invalid querystring profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/profile?profileid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
describe('invalid-account', () => {
it('ineligible querystring profileid', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/profile?profileid=${user2.account.profileid}`)
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('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/profile?profileid=${user.profile.profileid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const profile = await req.get()
assert.strictEqual(profile.object, 'profile')
})
})
})