diff --git a/src/v2/support/index.js b/src/v2/support/index.js index 55ed4f0..ade6e21 100644 --- a/src/v2/support/index.js +++ b/src/v2/support/index.js @@ -1,30 +1,31 @@ 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 }), - organizations: require("./organizations")({ instance, headers }), - organization_fields: require("./organization_fields")({ instance, headers }), - organization_memberships: require("./organization_memberships")({ + 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 }), + organizations: require('./organizations')({ instance, headers }), + organization_fields: require('./organization_fields')({ instance, headers }), + organization_memberships: require('./organization_memberships')({ instance, headers }), - organization_subscriptions: require("./organization_subscriptions")({ + organization_subscriptions: require('./organization_subscriptions')({ instance, headers }), - search: require("./search")({ instance, headers }), - suspended_tickets: require("./suspended_tickets")({ instance, headers }), - tags: require("./tags")({ instance, headers }), - ticket_activities: require("./ticket_activities")({ instance, headers }), - ticket_comments: require("./ticket_comments")({ instance, headers }), - ticket_fields: require("./ticket_fields")({ instance, headers }), - ticket_forms: require("./ticket_forms")({ instance, headers }), - ticket_import: require("./ticket_import")({ instance, headers }), - ticket_metrics: require("./ticket_metrics")({ instance, headers }), - tickets: require("./tickets")({ instance, headers }), - user_fields: require("./user_fields")({ instance, headers }), - user_passwords: require("./user_passwords")({ instance, headers }), - users: require("./users")({ instance, headers }), - views: require("./views")({ instance, headers }) + search: require('./search')({ instance, headers }), + suspended_tickets: require('./suspended_tickets')({ instance, headers }), + tags: require('./tags')({ instance, headers }), + ticket_activities: require('./ticket_activities')({ instance, headers }), + ticket_comments: require('./ticket_comments')({ instance, headers }), + ticket_fields: require('./ticket_fields')({ instance, headers }), + ticket_forms: require('./ticket_forms')({ instance, headers }), + ticket_import: require('./ticket_import')({ instance, headers }), + ticket_metrics: require('./ticket_metrics')({ instance, headers }), + tickets: require('./tickets')({ instance, headers }), + user_fields: require('./user_fields')({ instance, headers }), + user_identities: require('./user_identities')({ instance, headers }), + user_passwords: require('./user_passwords')({ instance, headers }), + users: require('./users')({ instance, headers }), + views: require('./views')({ instance, headers }) }); diff --git a/src/v2/support/user_identities/index.js b/src/v2/support/user_identities/index.js new file mode 100644 index 0000000..8136391 --- /dev/null +++ b/src/v2/support/user_identities/index.js @@ -0,0 +1,120 @@ +const validate = require('./validate'); + +module.exports = ({ instance, headers }) => { + const url = `https://${instance}.zendesk.com`; + + return { + list: (options = {}) => { + const { error } = validate.list(options); + if (error) throw new Error(error.details[0].message); + + const { user_id } = options; + + return { + method: 'GET', + url: `${url}/api/v2/users/${user_id}/identities.json`, + headers + }; + }, + + show: (options = {}) => { + const { error } = validate.show(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id } = options; + + return { + method: 'GET', + url: `${url}/api/v2/users/${user_id}/identities/${id}.json`, + headers + }; + }, + + create: (options = {}) => { + const { error } = validate.create(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, end_users = false, data } = options; + + return { + method: 'POST', + url: end_users + ? `${url}/api/v2/end_users/${user_id}/identities.json` + : `${url}/api/v2/users/${user_id}/identities.json`, + headers, + data + }; + }, + + update: (options = {}) => { + const { error } = validate.update(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id, data } = options; + + return { + method: 'PUT', + url: `${url}/api/v2/users/${user_id}/identities/${id}.json`, + headers, + data + }; + }, + + make_primary: (options = {}) => { + const { error } = validate.make_primary(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id, end_users = false, data = {} } = options; + + return { + method: 'PUT', + url: end_users + ? `${url}/api/v2/end_users/${user_id}/identities/${id}/make_primary` + : `${url}/api/v2/users/${user_id}/identities/${id}/make_primary`, + headers, + data + }; + }, + + verify: (options = {}) => { + const { error } = validate.verify(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id, data = {} } = options; + + return { + method: 'PUT', + url: `${url}/api/v2/users/${user_id}/identities/${id}/verify`, + headers, + data + }; + }, + + request_verification: (options = {}) => { + const { error } = validate.request_verification(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id, data = {} } = options; + + return { + method: 'PUT', + url: `${url}/api/v2/users/${user_id}/identities/${id}/request_verification.json`, + headers, + data + }; + }, + + delete: (options = {}) => { + const { error } = validate.delete(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, id } = options; + + return { + method: 'DELETE', + url: `${url}/api/v2/users/${user_id}/identities/${id}.json`, + headers + }; + } + }; +}; diff --git a/src/v2/support/user_identities/validate.js b/src/v2/support/user_identities/validate.js new file mode 100644 index 0000000..71246a8 --- /dev/null +++ b/src/v2/support/user_identities/validate.js @@ -0,0 +1,61 @@ +const Joi = require('@hapi/joi'); + +const id = Joi.number().min(1); +const user_id = Joi.number().min(1); +const end_users = Joi.boolean(); +const data = Joi.object(); + +module.exports = { + list: options => + Joi.object({ + user_id: user_id.required() + }).validate(options), + + show: options => + Joi.object({ + user_id: user_id.required(), + id: id.required() + }).validate(options), + + create: options => + Joi.object({ + user_id: user_id.required(), + end_users, + data: data.required() + }).validate(options), + + update: options => + Joi.object({ + user_id: user_id.required(), + id: id.required(), + data: data.required() + }).validate(options), + + make_primary: options => + Joi.object({ + user_id: user_id.required(), + id: id.required(), + end_users, + data + }).validate(options), + + verify: options => + Joi.object({ + user_id: user_id.required(), + id: id.required(), + data + }).validate(options), + + request_verification: options => + Joi.object({ + user_id: user_id.required(), + id: id.required(), + data + }).validate(options), + + delete: options => + Joi.object({ + user_id: user_id.required(), + id: id.required() + }).validate(options) +}; diff --git a/tests/src/v2/support.test.js b/tests/src/v2/support.test.js index 8c740e3..70ff5b2 100644 --- a/tests/src/v2/support.test.js +++ b/tests/src/v2/support.test.js @@ -1,38 +1,39 @@ -const endpoint = require("../../../src/v2/support"); +const endpoint = require('../../../src/v2/support'); -const instance = "instance"; +const instance = 'instance'; const headers = {}; -const check = (type, compare = "object") => expect(typeof type).toBe(compare); +const check = (type, compare = 'object') => expect(typeof type).toBe(compare); -describe("support api", () => { +describe('support api', () => { let support; 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)); - test("organizations", () => check(support.organizations)); - test("organization_fields", () => check(support.organization_fields)); - test("organization_subscriptions", () => + 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)); + test('organizations', () => check(support.organizations)); + test('organization_fields', () => check(support.organization_fields)); + test('organization_subscriptions', () => check(support.organization_subscriptions)); - test("organization_memberships", () => + test('organization_memberships', () => check(support.organization_memberships)); - test("search", () => check(support.search, "function")); - test("suspended_tickets", () => check(support.suspended_tickets)); - test("tags", () => check(support.tags)); - test("ticket_activities", () => check(support.ticket_activities)); - test("ticket_comments", () => check(support.ticket_comments)); - test("ticket_fields", () => check(support.ticket_fields)); - test("ticket_forms", () => check(support.ticket_forms)); - test("ticket_import", () => check(support.ticket_import)); - test("ticket_metrics", () => check(support.ticket_metrics)); - test("tickets", () => check(support.tickets)); - test("user_fields", () => check(support.user_fields)); - test("user_passwords", () => check(support.user_passwords)); - test("users", () => check(support.users)); - test("views", () => check(support.views)); + test('search', () => check(support.search, 'function')); + test('suspended_tickets', () => check(support.suspended_tickets)); + test('tags', () => check(support.tags)); + test('ticket_activities', () => check(support.ticket_activities)); + test('ticket_comments', () => check(support.ticket_comments)); + test('ticket_fields', () => check(support.ticket_fields)); + test('ticket_forms', () => check(support.ticket_forms)); + test('ticket_import', () => check(support.ticket_import)); + test('ticket_metrics', () => check(support.ticket_metrics)); + test('tickets', () => check(support.tickets)); + test('user_fields', () => check(support.user_fields)); + test('user_identities', () => check(support.user_identities)); + test('user_passwords', () => check(support.user_passwords)); + test('users', () => check(support.users)); + test('views', () => check(support.views)); }); diff --git a/tests/src/v2/support/user_identities.test.js b/tests/src/v2/support/user_identities.test.js new file mode 100644 index 0000000..1ac27f8 --- /dev/null +++ b/tests/src/v2/support/user_identities.test.js @@ -0,0 +1,299 @@ +const endpoint = require('../../../../src/v2/support/user_identities'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('user identities', () => { + let user_identities; + + beforeEach(() => (user_identities = endpoint({ instance, headers }))); + afterEach(() => (user_identities = null)); + + describe('list identities', () => { + it('should process w/ valid input', () => { + expect(user_identities.list({ user_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/identities.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => user_identities.list()).toThrowError(); + expect(() => user_identities.list({})).toThrowError(); + expect(() => user_identities.list('invalid')).toThrowError(); + expect(() => user_identities.list({ user_id: 'invalid' })).toThrowError(); + }); + }); + + describe('show identity', () => { + test('should process w/ valid input', () => { + expect(user_identities.show({ user_id: 123, id: 456 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/identities/456.json`, + headers + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.show()).toThrowError(); + expect(() => user_identities.show({})).toThrowError(); + expect(() => user_identities.show({ user_id: 'invalid' })).toThrowError(); + expect(() => + user_identities.show({ user_id: 0, id: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('create identity', () => { + test('should process w/ valid input', () => { + expect(user_identities.create({ user_id: 123, data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/users/123/identities.json`, + headers, + data: {} + }); + + expect( + user_identities.create({ user_id: 123, end_users: false, data: {} }) + ).toEqual({ + method: 'POST', + url: `${url}/api/v2/users/123/identities.json`, + headers, + data: {} + }); + + expect( + user_identities.create({ user_id: 123, end_users: true, data: {} }) + ).toEqual({ + method: 'POST', + url: `${url}/api/v2/end_users/123/identities.json`, + headers, + data: {} + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.create()).toThrowError(); + expect(() => user_identities.create({})).toThrowError(); + expect(() => user_identities.create('invalid')).toThrowError(); + expect(() => + user_identities.create({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.create({ user_id: 123, end_users: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.create({ user_id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('update identity', () => { + test('should process w/ valid input', () => { + expect( + user_identities.update({ user_id: 123, id: 456, data: {} }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456.json`, + headers, + data: {} + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.update()).toThrowError(); + expect(() => user_identities.update({})).toThrowError(); + expect(() => + user_identities.update({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.update({ user_id: 123, id: 'invalid', data: {} }) + ).toThrowError(); + expect(() => + user_identities.update({ user_id: 123, id: 456 }) + ).toThrowError(); + expect(() => + user_identities.update({ user_id: 123, id: 456, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('make identity primary', () => { + test('should process w/ valid input', () => { + expect(user_identities.make_primary({ user_id: 123, id: 456 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/make_primary`, + headers, + data: {} + }); + + expect( + user_identities.make_primary({ user_id: 123, id: 456, data: {} }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/make_primary`, + headers, + data: {} + }); + + expect( + user_identities.make_primary({ + user_id: 123, + id: 456, + end_users: false, + data: {} + }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/make_primary`, + headers, + data: {} + }); + + expect( + user_identities.make_primary({ + user_id: 123, + id: 456, + end_users: true, + data: {} + }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/end_users/123/identities/456/make_primary`, + headers, + data: {} + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.make_primary()).toThrowError(); + expect(() => user_identities.make_primary({})).toThrowError(); + expect(() => user_identities.make_primary('invalid')).toThrowError(); + expect(() => + user_identities.make_primary({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.make_primary({ user_id: 123, id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.make_primary({ + user_id: 123, + id: 456, + end_users: 'invalid' + }) + ).toThrowError(); + expect(() => + user_identities.make_primary({ user_id: 123, id: 456, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('verify identity', () => { + test('should process w/ valid input', () => { + expect(user_identities.verify({ user_id: 123, id: 456 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/verify`, + headers, + data: {} + }); + + expect( + user_identities.verify({ user_id: 123, id: 456, data: {} }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/verify`, + headers, + data: {} + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.verify()).toThrowError(); + expect(() => user_identities.verify({})).toThrowError(); + expect(() => user_identities.verify('invalid')).toThrowError(); + expect(() => + user_identities.verify({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.verify({ user_id: 123, id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.verify({ user_id: 123, id: 456, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('request user verification', () => { + test('should process w/ valid input', () => { + expect( + user_identities.request_verification({ user_id: 123, id: 456 }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/request_verification.json`, + headers, + data: {} + }); + + expect( + user_identities.request_verification({ + user_id: 123, + id: 456, + data: {} + }) + ).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/identities/456/request_verification.json`, + headers, + data: {} + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.request_verification()).toThrowError(); + expect(() => user_identities.request_verification({})).toThrowError(); + expect(() => + user_identities.request_verification('invalid') + ).toThrowError(); + expect(() => + user_identities.request_verification({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.request_verification({ user_id: 123, id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.request_verification({ + user_id: 123, + id: 456, + data: 'invalid' + }) + ).toThrowError(); + }); + }); + + describe('delete identity', () => { + test('should process w/ valid input', () => { + expect(user_identities.delete({ user_id: 123, id: 456 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/users/123/identities/456.json`, + headers + }); + }); + + test('should throw error w/ invalid input', () => { + expect(() => user_identities.delete()).toThrowError(); + expect(() => user_identities.delete({})).toThrowError(); + expect(() => user_identities.delete('invalid')).toThrowError(); + expect(() => + user_identities.delete({ user_id: 'invalid' }) + ).toThrowError(); + expect(() => + user_identities.delete({ user_id: 123, id: 'invalid' }) + ).toThrowError(); + }); + }); +});