From 12743697896cd781f8642cc0508b1c8fc5779f37 Mon Sep 17 00:00:00 2001 From: androozka Date: Tue, 24 Dec 2019 11:00:27 -0800 Subject: [PATCH] converted user_passwords --- src/api/support/user_passwords.js | 80 ++++++++++++++++++ src/api/support/user_passwords/index.js | 45 ---------- src/api/support/user_passwords/validate.js | 23 ------ tests/api/support/user_passwords.test.js | 95 ++++++++++++++++++++++ 4 files changed, 175 insertions(+), 68 deletions(-) create mode 100644 src/api/support/user_passwords.js delete mode 100644 src/api/support/user_passwords/index.js delete mode 100644 src/api/support/user_passwords/validate.js create mode 100644 tests/api/support/user_passwords.test.js diff --git a/src/api/support/user_passwords.js b/src/api/support/user_passwords.js new file mode 100644 index 0000000..e6537d3 --- /dev/null +++ b/src/api/support/user_passwords.js @@ -0,0 +1,80 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _user_id = Joi.number().min(1); +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 { + /** + * Set a User's Password + * + * POST /api/v2/users/{user_id}/password.json + * https://developer.zendesk.com/rest_api/docs/support/user_passwords#set-a-users-password + */ + set: (options = {}) => { + const { error } = Joi.object({ + user_id: _user_id.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, data } = options; + return { + method: 'POST', + url: `${url}/api/v2/users/${user_id}/password.json`, + headers, + data + }; + }, + + /** + * Change Your Password + * + * PUT /api/v2/users/{user_id}/password.json + * https://developer.zendesk.com/rest_api/docs/support/user_passwords#change-your-password + */ + change: (options = {}) => { + const { error } = Joi.object({ + user_id: _user_id.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { user_id, data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/users/${user_id}/password.json`, + headers, + data + }; + }, + + /** + * Get a list of password requirements + * + * GET /api/v2/users/{user_id}/password/requirements.json + * https://developer.zendesk.com/rest_api/docs/support/user_passwords#get-a-list-of-password-requirements + */ + requirements: (options = {}) => { + const { error } = Joi.object({ + user_id: _user_id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { user_id } = options; + return { + method: 'GET', + url: `${url}/api/v2/users/${user_id}/password/requirements.json`, + headers + }; + } + }; +}; diff --git a/src/api/support/user_passwords/index.js b/src/api/support/user_passwords/index.js deleted file mode 100644 index 983fcbe..0000000 --- a/src/api/support/user_passwords/index.js +++ /dev/null @@ -1,45 +0,0 @@ -const validate = require('./validate'); - -module.exports = ({ instance, headers }) => { - const url = `https://${instance}.zendesk.com`; - - return { - set: (options = {}) => { - const { error } = validate.set(options); - if (error) throw new Error(error.details[0].message); - - const { user_id, data } = options; - return { - method: 'POST', - url: `${url}/api/v2/users/${user_id}/password.json`, - headers, - data - }; - }, - - change: (options = {}) => { - const { error } = validate.change(options); - if (error) throw new Error(error.details[0].message); - - const { user_id, data } = options; - return { - method: 'PUT', - url: `${url}/api/v2/users/${user_id}/password.json`, - headers, - data - }; - }, - - requirements: (options = {}) => { - const { error } = validate.requirements(options); - if (error) throw new Error(error.details[0].message); - - const { user_id } = options; - return { - method: 'GET', - url: `${url}/api/v2/users/${user_id}/password/requirements.json`, - headers - }; - } - }; -}; diff --git a/src/api/support/user_passwords/validate.js b/src/api/support/user_passwords/validate.js deleted file mode 100644 index 8337d29..0000000 --- a/src/api/support/user_passwords/validate.js +++ /dev/null @@ -1,23 +0,0 @@ -const Joi = require('@hapi/joi'); - -const user_id = Joi.number().min(1); -const data = Joi.object(); - -module.exports = { - set: options => - Joi.object({ - user_id: user_id.required(), - data: data.required() - }).validate(options), - - change: options => - Joi.object({ - user_id: user_id.required(), - data: data.required() - }).validate(options), - - requirements: options => - Joi.object({ - user_id: user_id.required() - }).validate(options) -}; diff --git a/tests/api/support/user_passwords.test.js b/tests/api/support/user_passwords.test.js new file mode 100644 index 0000000..e731b54 --- /dev/null +++ b/tests/api/support/user_passwords.test.js @@ -0,0 +1,95 @@ +const endpoint = require('../../../src/api/support/user_passwords'); +const { prepare } = require('../../../src/utils/options'); + +describe('object types', () => { + 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', () => { + const ep = endpoint(options); + expect(ep).toBeTruthy(); + }); + + it('should fail with invalid input', () => { + expect(() => endpoint()).toThrowError(); + expect(() => endpoint({})).toThrowError(); + }); + }); + + describe("set a user's password", () => { + it('should process w/ valid input', () => { + expect(endPoint.set({ user_id: 123, data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/users/123/password.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.set()).toThrowError(); + expect(() => endPoint.set('invalid')).toThrowError(); + expect(() => endPoint.set({ user_id: 'invalid' })).toThrowError(); + expect(() => + endPoint.set({ user_id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('change your password', () => { + it('should process w/ valid input', () => { + expect(endPoint.change({ user_id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/users/123/password.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.change()).toThrowError(); + expect(() => endPoint.change({})).toThrowError(); + expect(() => endPoint.change('invalid')).toThrowError(); + expect(() => endPoint.change({ id: 'invalid' })).toThrowError(); + expect(() => + endPoint.change({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('get a list of password requirements', () => { + it('should process w/ valid input', () => { + expect(endPoint.requirements({ user_id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/users/123/password/requirements.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.requirements()).toThrowError(); + expect(() => endPoint.requirements({})).toThrowError(); + expect(() => endPoint.requirements('invalid')).toThrowError(); + expect(() => + endPoint.requirements({ user_id: 'invalid' }) + ).toThrowError(); + }); + }); +});