From bf05fc7a42b0cb28e6a5d9d819d6d3d09a8bab55 Mon Sep 17 00:00:00 2001 From: androozka Date: Mon, 9 Sep 2019 20:14:25 -0700 Subject: [PATCH] built support:custom_agent_roles closes #20 --- src/v2/support/custom_agent_roles/index.js | 67 ++++++++++++ src/v2/support/custom_agent_roles/validate.js | 13 +++ src/v2/support/index.js | 1 + tests/src/v2/support.test.js | 1 + .../src/v2/support/custom_agent_roles.test.js | 103 ++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 src/v2/support/custom_agent_roles/index.js create mode 100644 src/v2/support/custom_agent_roles/validate.js create mode 100644 tests/src/v2/support/custom_agent_roles.test.js diff --git a/src/v2/support/custom_agent_roles/index.js b/src/v2/support/custom_agent_roles/index.js new file mode 100644 index 0000000..1b2b16b --- /dev/null +++ b/src/v2/support/custom_agent_roles/index.js @@ -0,0 +1,67 @@ +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/custom_roles.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/custom_roles/${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/custom_roles.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/custom_roles/${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/custom_roles/${id}.json`, + headers + }; + } + }; +}; diff --git a/src/v2/support/custom_agent_roles/validate.js b/src/v2/support/custom_agent_roles/validate.js new file mode 100644 index 0000000..5d7adc1 --- /dev/null +++ b/src/v2/support/custom_agent_roles/validate.js @@ -0,0 +1,13 @@ +const Joi = require('@hapi/joi'); + +const 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() }) +}; diff --git a/src/v2/support/index.js b/src/v2/support/index.js index b061e64..f2c28d1 100644 --- a/src/v2/support/index.js +++ b/src/v2/support/index.js @@ -1,4 +1,5 @@ module.exports = ({ instance, headers }) => ({ + custom_agent_roles: require('./custom_agent_roles')({ instance, headers }), end_users: require('./end_users')({ instance, headers }), group_memberships: require('./group_memberships')({ instance, headers }), groups: require('./groups')({ instance, headers }), diff --git a/tests/src/v2/support.test.js b/tests/src/v2/support.test.js index 2ca91be..690ef49 100644 --- a/tests/src/v2/support.test.js +++ b/tests/src/v2/support.test.js @@ -11,6 +11,7 @@ describe('support api', () => { beforeEach(() => (support = endpoint({ instance, headers }))); afterEach(() => (support = null)); + test('custom_agent_roles', () => check(support.custom_agent_roles)); test('end_users', () => check(support.end_users)); test('group_memberships', () => check(support.group_memberships)); test('groups', () => check(support.groups)); diff --git a/tests/src/v2/support/custom_agent_roles.test.js b/tests/src/v2/support/custom_agent_roles.test.js new file mode 100644 index 0000000..a934760 --- /dev/null +++ b/tests/src/v2/support/custom_agent_roles.test.js @@ -0,0 +1,103 @@ +const endpoint = require('../../../../src/v2/support/custom_agent_roles'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('custom agent roles', () => { + let customAgentRoles; + + beforeEach(() => (customAgentRoles = endpoint({ instance, headers }))); + afterEach(() => (customAgentRoles = null)); + + describe('list custom roles', () => { + it('should process w/ valid input', () => { + expect(customAgentRoles.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/custom_roles.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => customAgentRoles.list('invalid')).toThrowError(); + }); + }); + + describe('show custom role', () => { + it('should process w/ valid input', () => { + expect(customAgentRoles.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/custom_roles/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => customAgentRoles.show()).toThrowError(); + expect(() => customAgentRoles.show({})).toThrowError(); + expect(() => customAgentRoles.show('invalid')).toThrowError(); + expect(() => customAgentRoles.show({ id: 'invalid' })).toThrowError(); + }); + }); + + describe('create custom role', () => { + it('should process w/ valid input', () => { + expect(customAgentRoles.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/custom_roles.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => customAgentRoles.create()).toThrowError(); + expect(() => customAgentRoles.create({})).toThrowError(); + expect(() => customAgentRoles.create('invalid')).toThrowError(); + expect(() => customAgentRoles.create({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('update custom role', () => { + it('should process w/ valid input', () => { + expect(customAgentRoles.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/custom_roles/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => customAgentRoles.update()).toThrowError(); + expect(() => customAgentRoles.update({})).toThrowError(); + expect(() => customAgentRoles.update('invalid')).toThrowError(); + expect(() => customAgentRoles.update({ id: 0 })).toThrowError(); + expect(() => customAgentRoles.update({ id: 'invalid' })).toThrowError(); + expect(() => + customAgentRoles.update({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('delete custom role', () => { + it('should process w/ valid input', () => { + expect(customAgentRoles.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/custom_roles/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => customAgentRoles.delete()).toThrowError(); + expect(() => customAgentRoles.delete({})).toThrowError(); + expect(() => customAgentRoles.delete('invalid')).toThrowError(); + expect(() => customAgentRoles.delete({ id: 'invalid' })).toThrowError(); + }); + }); +});