From 23851e9e94c623884aab386922046fac7bbe9789 Mon Sep 17 00:00:00 2001 From: androozka Date: Tue, 28 Jan 2020 19:58:18 -0800 Subject: [PATCH] built support:account_settings --- src/api/support/account_settings.js | 51 ++++++++++++++++ .../src/api/support/account_settings.test.js | 61 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/api/support/account_settings.js create mode 100644 tests/src/api/support/account_settings.test.js diff --git a/src/api/support/account_settings.js b/src/api/support/account_settings.js new file mode 100644 index 0000000..3dbd1dd --- /dev/null +++ b/src/api/support/account_settings.js @@ -0,0 +1,51 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _data = Joi.object(); + +// Initialize Endpoint +module.exports = (options = {}) => { + const { error } = validate(options); + if (error) throw new Error(error.details[0].message); + + const { url, headers } = prepare(options); + + return { + /** + * Show Settings + * + * GET /api/v2/account/settings.json + * https://developer.zendesk.com/rest_api/docs/support/account_settings#show-settings + */ + show: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/account/settings.json`, + headers + }; + }, + + /** + * Update Account Settings + * + * PUT /api/v2/account/settings.json + * https://developer.zendesk.com/rest_api/docs/support/account_settings#update-account-settings + */ + update: (options = {}) => { + const { error } = Joi.object({ + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/account/settings.json`, + headers, + data + }; + } + }; +}; diff --git a/tests/src/api/support/account_settings.test.js b/tests/src/api/support/account_settings.test.js new file mode 100644 index 0000000..dde1839 --- /dev/null +++ b/tests/src/api/support/account_settings.test.js @@ -0,0 +1,61 @@ +const endpoint = require('../../../../src/api/support/account_settings'); +const { prepare } = require('../../../../src/utils/options'); + +describe('Account Settings', () => { + let endPoint, options, url, headers; + + beforeEach(() => { + options = { + instance: 'instance', + email: 'user@email.com', + token: 'token' + }; + endPoint = endpoint(options); + ({ url, headers } = prepare(options)); + }); + + afterEach(() => { + options = null; + endPoint = null; + url = null; + headers = null; + }); + + describe('init', () => { + it('should setup endpoint object', () => { + expect(endpoint(options)).toBeTruthy(); + }); + + it('should fail with invalid input', () => { + expect(() => endpoint()).toThrowError(); + expect(() => endpoint({})).toThrowError(); + }); + }); + + describe('Show Settings', () => { + it('should process w/ valid input', () => { + expect(endPoint.show()).toEqual({ + method: 'GET', + url: `${url}/api/v2/account/settings.json`, + headers + }); + }); + }); + + describe('Update Account Settings', () => { + it('should process w/ valid input', () => { + expect(endPoint.update({ data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/account/settings.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update()).toThrowError(); + expect(() => endPoint.update({})).toThrowError(); + expect(() => endPoint.update({ data: 'invalid' })).toThrowError(); + }); + }); +});