Skip to content

Commit

Permalink
Merge pull request #76 from androozka/support_custom-agent-roles
Browse files Browse the repository at this point in the history
built support:custom_agent_roles
  • Loading branch information
androozka committed Sep 10, 2019
2 parents aa60b6b + bf05fc7 commit 2f4b014
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/v2/support/custom_agent_roles/index.js
Original file line number Diff line number Diff line change
@@ -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
};
}
};
};
13 changes: 13 additions & 0 deletions src/v2/support/custom_agent_roles/validate.js
Original file line number Diff line number Diff line change
@@ -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() })
};
1 change: 1 addition & 0 deletions src/v2/support/index.js
Original file line number Diff line number Diff line change
@@ -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 }),
Expand Down
1 change: 1 addition & 0 deletions tests/src/v2/support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
103 changes: 103 additions & 0 deletions tests/src/v2/support/custom_agent_roles.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 2f4b014

Please sign in to comment.