/api/user/update-profile (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.UpdateProfile.patch(req)Returns object
{
"profileid": "prof_c4c0e5583ee14f61",
"object": "profile",
"appid": "tests_1656038584",
"accountid": "acct_aee5d69d29756d89",
"companyName": null,
"fullName": "Test Person",
"contactEmail": "Stacy.Heidenreich@yahoo.com",
"displayEmail": null,
"displayName": null,
"phone": null,
"occupation": null,
"location": null,
"dob": null,
"website": null,
"fields": null,
"createdAt": "2022-06-24T02:43:04.292Z",
"updatedAt": "2022-06-24T02:43:04.292Z"
}
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
company-name | string | configurable as required | POST |
contact-email | string | configurable as required | POST |
display-email | string | configurable as required | POST |
display-name | string | configurable as required | POST |
dob | string | configurable as required | POST |
full-name | string | configurable as required | POST |
location | string | configurable as required | POST |
occupation | string | configurable as required | POST |
phone | string | configurable as required | POST |
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-company-name | missing posted company-name |
invalid-company-name-length | posted company-name too short |
posted company-name too long | |
invalid-contact-email | missing posted contact-email |
invalid-display-email | missing posted display-email |
invalid-display-name | missing posted display-name |
invalid-display-name-length | posted display-name too short |
posted display-name too long | |
invalid-dob | missing posted dob |
invalid-full-name | missing posted full-name |
invalid-full-name-length | posted full-name too short |
posted full-name too long | |
invalid-location | missing posted location |
invalid-occupation | missing posted occupation |
invalid-phone | missing posted phone |
invalid-profileid | missing querystring profileid |
invalid querystring profileid | |
ineligible querystring profileid |
NodeJS source (view on github)
const dashboard = require('../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.profileid) {
throw new Error('invalid-profileid')
}
const profile = await global.api.user.Profile.get(req)
if (!profile) {
throw new Error('invalid-profileid')
}
req.body = req.body || {}
const profileInfo = {}
const profileFields = req.userProfileFields || global.userProfileFields
for (const field of profileFields) {
const displayName = global.profileFieldMap[field]
switch (field) {
case 'full-name':
if (!req.body['full-name'] || !req.body['full-name'].length) {
throw new Error('invalid-full-name')
}
if (global.minimumProfileFullNameLength > req.body['full-name'].length ||
global.maximumProfileFullNameLength < req.body['full-name'].length) {
throw new Error('invalid-full-name-length')
}
profileInfo.fullName = req.body['full-name']
continue
case 'contact-email':
if (!req.body[field] || req.body[field].indexOf('@') < 1) {
throw new Error(`invalid-${field}`)
}
profileInfo.contactEmail = req.body[field]
continue
case 'display-email':
if (!req.body[field] || req.body[field].indexOf('@') < 1) {
throw new Error(`invalid-${field}`)
}
profileInfo.displayEmail = req.body[field]
continue
case 'display-name':
if (!req.body[field] || !req.body[field].length) {
throw new Error(`invalid-${field}`)
}
if (global.minimumProfileDisplayNameLength > req.body[field].length ||
global.maximumProfileDisplayNameLength < req.body[field].length) {
throw new Error('invalid-display-name-length')
}
profileInfo.displayName = req.body[field]
continue
case 'company-name':
if (!req.body[field] || !req.body[field].length) {
throw new Error(`invalid-${field}`)
}
if (global.minimumProfileCompanyNameLength > req.body[field].length ||
global.maximumProfileCompanyNameLength < req.body[field].length) {
throw new Error('invalid-company-name-length')
}
profileInfo.companyName = req.body[field]
continue
case 'dob':
if (!req.body[field] || !req.body[field].length) {
throw new Error(`invalid-${field}`)
}
try {
const date = dashboard.Format.parseDate(req.body[field])
if (!date || !date.getFullYear) {
throw new Error(`invalid-${field}`)
}
profileInfo.dob = dashboard.Format.date(date)
} catch (error) {
throw new Error(`invalid-${field}`)
}
continue
default:
if (!req.body[field]) {
throw new Error(`invalid-${field}`)
}
profileInfo[displayName] = req.body[field]
continue
}
}
await dashboard.Storage.Profile.update(profileInfo, {
where: {
profileid: req.query.profileid,
appid: req.appid || global.appid
}
})
for (const field in profileInfo) {
profile[field] = profileInfo[field]
}
await dashboard.StorageCache.remove(req.query.profileid)
return profile
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')
describe('/api/user/update-profile', () => {
describe('exceptions', () => {
describe('invalid-profileid', () => {
it('missing querystring profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/update-profile')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} 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/update-profile?profileid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
describe('invalid-profileid', () => {
it('ineligible querystring profileid', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user2.account.profileid}`)
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-full-name', () => {
it('missing posted full-name', async () => {
global.requireProfile = true
global.userProfileFields = ['full-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-full-name')
})
})
describe('invalid-full-name-length', () => {
it('posted full-name too short', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['full-name']
global.minimumProfileFullNameLength = 100
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-full-name-length')
})
it('posted full-name too long', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['full-name']
global.maximumProfileFullNameLength = 1
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-full-name-length')
})
})
describe('invalid-display-name', () => {
it('missing posted display-name', async () => {
global.requireProfile = true
global.userProfileFields = ['display-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-name')
})
})
describe('invalid-display-name-length', () => {
it('posted display-name too short', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['display-name']
global.minimumProfileDisplayNameLength = 100
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-name-length')
})
it('posted display-name too long', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['display-name']
global.maximumProfileDisplayNameLength = 1
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-name-length')
})
})
describe('invalid-company-name', () => {
it('missing posted company-name', async () => {
global.requireProfile = true
global.userProfileFields = ['company-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-company-name')
})
})
describe('invalid-company-name-length', () => {
it('posted company-name too short', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/create-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
'company-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['company-name']
global.minimumProfileCompanyNameLength = 100
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-company-name-length')
})
it('posted company-name too long', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/create-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
'company-name': 'Test'
}
global.requireProfile = true
global.userProfileFields = ['company-name']
global.maximumProfileCompanyNameLength = 1
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-company-name-length')
})
})
describe('invalid-contact-email', () => {
it('missing posted contact-email', async () => {
global.requireProfile = true
global.userProfileFields = ['contact-email']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-contact-email')
})
})
describe('invalid-display-email', () => {
it('missing posted display-email', async () => {
global.requireProfile = true
global.userProfileFields = ['display-email']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-email')
})
})
describe('invalid-location', () => {
it('missing posted location', async () => {
global.requireProfile = true
global.userProfileFields = ['location']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-location')
})
})
describe('invalid-occupation', () => {
it('missing posted occupation', async () => {
global.requireProfile = true
global.userProfileFields = ['occupation']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-occupation')
})
})
describe('invalid-phone', () => {
it('missing posted phone', async () => {
global.requireProfile = true
global.userProfileFields = ['phone']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-phone')
})
})
describe('invalid-dob', () => {
it('missing posted dob', async () => {
global.requireProfile = true
global.userProfileFields = ['dob']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-dob')
})
})
})
describe('receives', () => {
it('optionally-required posted full-name', async () => {
global.requireProfile = true
global.userProfileFields = ['full-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Testing Person'
}
const profile = await req.patch()
assert.strictEqual(profile.fullName, 'Testing Person')
})
it('optionally-required posted display-name', async () => {
global.requireProfile = true
global.userProfileFields = ['display-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-name': 'Testing'
}
const profile = await req.patch()
assert.strictEqual(profile.displayName, 'Testing')
})
it('optionally-required posted company-name', async () => {
global.requireProfile = true
global.userProfileFields = ['company-name']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'company-name': 'Testing'
}
const profile = await req.patch()
assert.strictEqual(profile.companyName, 'Testing')
})
it('optionally-required posted contact-email', async () => {
global.requireProfile = true
global.userProfileFields = ['contact-email']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'contact-email': 'test@test.com'
}
const profile = await req.patch()
assert.strictEqual(profile.contactEmail, 'test@test.com')
})
it('optionally-required posted display-email', async () => {
global.requireProfile = true
global.userProfileFields = ['display-email']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-email': 'test@test.com'
}
const profile = await req.patch()
assert.strictEqual(profile.displayEmail, 'test@test.com')
})
it('optionally-required posted location', async () => {
global.requireProfile = true
global.userProfileFields = ['location']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
location: 'testing'
}
const profile = await req.patch()
assert.strictEqual(profile.location, 'testing')
})
it('optionally-required posted occupation', async () => {
global.requireProfile = true
global.userProfileFields = ['occupation']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
occupation: 'Testing'
}
const profile = await req.patch()
assert.strictEqual(profile.occupation, 'Testing')
})
it('optionally-required posted phone', async () => {
global.requireProfile = true
global.userProfileFields = ['phone']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
phone: '456-789-0123'
}
const profile = await req.patch()
assert.strictEqual(profile.phone, '456-789-0123')
})
it('optionally-required posted dob', async () => {
global.requireProfile = true
global.userProfileFields = ['dob']
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
dob: '1950-01-01'
}
const profile = await req.patch()
assert.strictEqual(profile.dob, '1950-01-01')
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Test Person'
}
req.filename = __filename
req.saveResponse = true
global.userProfileFields = ['full-name']
const profile = await req.patch()
assert.strictEqual(profile.object, 'profile')
})
})
describe('configuration', () => {
it('environment USER_PROFILE_FIELDS', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
const fields = ['full-name', 'display-name', 'contact-email', 'display-email', 'dob', 'phone', 'occupation', 'location', 'company-name', 'website']
const body = {
'full-name': 'Test Person',
'contact-email': 'test1@test.com',
'display-email': 'test2@test.com',
dob: '2000-01-01',
'display-name': 'tester',
phone: '456-789-0123',
occupation: 'Programmer',
location: 'USA',
'company-name': 'Test company',
website: 'https://example.com'
}
for (const field of fields) {
req.body = {
confirm: 'password1234',
[field]: body[field]
}
global.userProfileFields = [field]
const displayName = global.profileFieldMap[field]
const account = await req.patch()
assert.strictEqual(account[displayName], body[field])
}
})
it('environment MINIMUM_PROFILE_FULL_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Test'
}
global.userProfileFields = ['full-name']
global.minimumProfileFullNameLength = 100
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-full-name-length')
})
it('environment MAXIMUM_PROFILE_FULL_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'full-name': 'Test'
}
global.userProfileFields = ['full-name']
global.maximumProfileFullNameLength = 1
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-full-name-length')
})
it('environment MINIMUM_PROFILE_DISPLAY_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-name': '1'
}
global.userProfileFields = ['display-name']
global.minimumProfileDisplayNameLength = 100
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-name-length')
})
it('environment MAXIMUM_PROFILE_DISPLAY_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'display-name': 'meeeee123'
}
global.userProfileFields = ['display-name']
global.maximumProfileDisplayNameLength = 1
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-display-name-length')
})
it('environment MINIMUM_PROFILE_COMPANY_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'company-name': '1'
}
global.userProfileFields = ['company-name']
global.minimumProfileCompanyNameLength = 100
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-company-name-length')
})
it('environment MAXIMUM_PROFILE_COMPANY_NAME_LENGTH', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
req.body = {
'company-name': 'acme inc'
}
global.userProfileFields = ['company-name']
global.maximumProfileCompanyNameLength = 1
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-company-name-length')
})
it('override req.userProfileFields', async () => {
const user = await TestHelper.createUser()
global.userProfileFields = ['full-name', 'display-name', 'contact-email', 'display-email', 'dob', 'phone', 'occupation', 'location', 'company-name', 'website']
const req = TestHelper.createRequest(`/api/user/update-profile?profileid=${user.account.profileid}`)
req.account = user.account
req.session = user.session
const body = {
'full-name': 'Test Person',
'contact-email': 'test1@test.com',
'display-email': 'test2@test.com',
dob: '2000-01-01',
'display-name': 'tester',
phone: '456-789-0123',
occupation: 'Programmer',
location: 'USA',
'company-name': 'Test company',
website: 'https://example.com'
}
for (const field of global.userProfileFields) {
req.userProfileFields = [field]
req.body = {
[field]: body[field]
}
const displayName = global.profileFieldMap[field]
const account = await req.route.api.patch(req)
assert.strictEqual(account[displayName], body[field])
}
})
})
})