-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const validate = require('./validate'); | ||
|
||
module.exports = ({ instance, headers }) => { | ||
const url = `https://${instance}.zendesk.com`; | ||
|
||
return { | ||
list: (options = null) => { | ||
if (options) throw new Error('no options are allowed'); | ||
|
||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
show: (options = {}) => { | ||
const { error } = validate.show(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { id } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields/${id}.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
create: (options = {}) => { | ||
const { error } = validate.create(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { data } = options; | ||
return { | ||
method: 'POST', | ||
url: `${url}/api/v2/user_fields.json`, | ||
headers, | ||
data | ||
}; | ||
}, | ||
|
||
update: (options = {}) => { | ||
const { error } = validate.update(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { id, data } = options; | ||
return { | ||
method: 'PUT', | ||
url: `${url}/api/v2/user_fields/${id}.json`, | ||
headers, | ||
data | ||
}; | ||
}, | ||
|
||
delete: (options = {}) => { | ||
const { error } = validate.delete(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { id } = options; | ||
return { | ||
method: 'DELETE', | ||
url: `${url}/api/v2/user_fields/${id}.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
reorder: (options = {}) => { | ||
const { error } = validate.reorder(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { data } = options; | ||
return { | ||
method: 'PUT', | ||
url: `${url}/api/v2/user_fields/reorder.json`, | ||
headers, | ||
data | ||
}; | ||
}, | ||
|
||
listOptions: (options = {}) => { | ||
const { error } = validate.listOptions(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { field_id } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields/${field_id}/options.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
showOption: (options = {}) => { | ||
const { error } = validate.showOption(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { field_id, id } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
createOrUpdateOption: (options = {}) => { | ||
const { error } = validate.createOrUpdateOption(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { field_id, data } = options; | ||
return { | ||
method: 'POST', | ||
url: `${url}/api/v2/user_fields/${field_id}/options.json`, | ||
headers, | ||
data | ||
}; | ||
}, | ||
|
||
deleteOption: (options = {}) => { | ||
const { error } = validate.deleteOption(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { field_id, id } = options; | ||
return { | ||
method: 'DELETE', | ||
url: `${url}/api/v2/user_fields/${field_id}/options/${id}.json`, | ||
headers | ||
}; | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const Joi = require('@hapi/joi'); | ||
|
||
const id = Joi.number().min(1); | ||
const field_id = Joi.number().min(1); | ||
const data = Joi.object(); | ||
|
||
module.exports = { | ||
list: null, // no options | ||
show: options => Joi.validate(options, { id: id.required() }), | ||
create: options => Joi.validate(options, { data: data.required() }), | ||
update: options => | ||
Joi.validate(options, { id: id.required(), data: data.required() }), | ||
delete: options => Joi.validate(options, { id: id.required() }), | ||
reorder: options => Joi.validate(options, { data: data.required() }), | ||
listOptions: options => | ||
Joi.validate(options, { field_id: field_id.required() }), | ||
showOption: options => | ||
Joi.validate(options, { field_id: field_id.required(), id: id.required() }), | ||
createOrUpdateOption: options => | ||
Joi.validate(options, { | ||
field_id: field_id.required(), | ||
data: data.required() | ||
}), | ||
deleteOption: options => | ||
Joi.validate(options, { field_id: field_id.required(), id: id.required() }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
const endpoint = require('../../../../src/v2/support/user_fields'); | ||
|
||
const instance = 'instance'; | ||
const url = `https://${instance}.zendesk.com`; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
Authorization: 'Basic <64bit_encoded_credentials>' | ||
}; | ||
|
||
describe('ticket fields', () => { | ||
let user_fields; | ||
|
||
beforeEach(() => (user_fields = endpoint({ instance, headers }))); | ||
afterEach(() => (user_fields = null)); | ||
|
||
describe('list user fields', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.list('invalid')).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('show user field', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.show({ id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.show()).toThrowError(); | ||
expect(() => user_fields.show('invalid')).toThrowError(); | ||
expect(() => user_fields.show({})).toThrowError(); | ||
expect(() => user_fields.show({ id: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('create user fields', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.create({ data: {} })).toEqual({ | ||
method: 'POST', | ||
url: `${url}/api/v2/user_fields.json`, | ||
headers, | ||
data: {} | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.create()).toThrowError(); | ||
expect(() => user_fields.create('invalid')).toThrowError(); | ||
expect(() => user_fields.create({})).toThrowError(); | ||
expect(() => user_fields.create({ data: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('update user fields', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.update({ id: 123, data: {} })).toEqual({ | ||
method: 'PUT', | ||
url: `${url}/api/v2/user_fields/123.json`, | ||
headers, | ||
data: {} | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.update()).toThrowError(); | ||
expect(() => user_fields.update('invalid')).toThrowError(); | ||
expect(() => user_fields.update({})).toThrowError(); | ||
expect(() => user_fields.update({ id: 'invalid' })).toThrowError(); | ||
expect(() => | ||
user_fields.update({ id: 123, data: 'invalid' }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('delete user field', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.delete({ id: 123 })).toEqual({ | ||
method: 'DELETE', | ||
url: `${url}/api/v2/user_fields/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.delete()).toThrowError(); | ||
expect(() => user_fields.delete('invalid')).toThrowError(); | ||
expect(() => user_fields.delete({})).toThrowError(); | ||
expect(() => user_fields.delete({ id: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('reorder user field', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.reorder({ data: {} })).toEqual({ | ||
method: 'PUT', | ||
url: `${url}/api/v2/user_fields/reorder.json`, | ||
headers, | ||
data: {} | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.reorder()).toThrowError(); | ||
expect(() => user_fields.reorder('invalid')).toThrowError(); | ||
expect(() => user_fields.reorder({})).toThrowError(); | ||
expect(() => user_fields.reorder({ data: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('list user field options', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.listOptions({ field_id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/user_fields/123/options.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.listOptions()).toThrowError(); | ||
expect(() => user_fields.listOptions('invalid')).toThrowError(); | ||
expect(() => user_fields.listOptions({})).toThrowError(); | ||
expect(() => | ||
user_fields.listOptions({ field_id: 'invalid' }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('show a user field option', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.showOption({ field_id: 123, id: 456 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/ticket_fields/123/options/456.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.showOption()).toThrowError(); | ||
expect(() => user_fields.showOption('invalid')).toThrowError(); | ||
expect(() => user_fields.showOption({})).toThrowError(); | ||
expect(() => | ||
user_fields.showOption({ field_id: 'invalid' }) | ||
).toThrowError(); | ||
expect(() => | ||
user_fields.showOption({ field_id: 123, id: 'invalid' }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('create or update a user field option', () => { | ||
it('should process w/ valid input', () => { | ||
expect( | ||
user_fields.createOrUpdateOption({ field_id: 123, data: {} }) | ||
).toEqual({ | ||
method: 'POST', | ||
url: `${url}/api/v2/user_fields/123/options.json`, | ||
headers, | ||
data: {} | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.createOrUpdateOption()).toThrowError(); | ||
expect(() => user_fields.createOrUpdateOption('invalid')).toThrowError(); | ||
expect(() => user_fields.createOrUpdateOption({})).toThrowError(); | ||
expect(() => | ||
user_fields.createOrUpdateOption({ field_id: 'invalid' }) | ||
).toThrowError(); | ||
expect(() => | ||
user_fields.createOrUpdateOption({ field_id: 123, data: 'invalid' }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('delete user field option', () => { | ||
it('should process w/ valid input', () => { | ||
expect(user_fields.deleteOption({ field_id: 123, id: 456 })).toEqual({ | ||
method: 'DELETE', | ||
url: `${url}/api/v2/user_fields/123/options/456.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => user_fields.deleteOption()).toThrowError(); | ||
expect(() => user_fields.deleteOption('invalid')).toThrowError(); | ||
expect(() => user_fields.deleteOption({})).toThrowError(); | ||
expect(() => | ||
user_fields.deleteOption({ field_id: 'invalid' }) | ||
).toThrowError(); | ||
expect(() => | ||
user_fields.deleteOption({ field_id: 123, id: 'invalid' }) | ||
).toThrowError(); | ||
}); | ||
}); | ||
}); |
9e476c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolves #13