Skip to content

Commit

Permalink
converted user_passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 24, 2019
1 parent e9d574d commit 1274369
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 68 deletions.
80 changes: 80 additions & 0 deletions src/api/support/user_passwords.js
Original file line number Diff line number Diff line change
@@ -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
};
}
};
};
45 changes: 0 additions & 45 deletions src/api/support/user_passwords/index.js

This file was deleted.

23 changes: 0 additions & 23 deletions src/api/support/user_passwords/validate.js

This file was deleted.

95 changes: 95 additions & 0 deletions tests/api/support/user_passwords.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 1274369

Please sign in to comment.