Skip to content

Commit

Permalink
built support:user-passwords
Browse files Browse the repository at this point in the history
closes #17
  • Loading branch information
androozka committed Sep 10, 2019
1 parent 2f4b014 commit 1e4b3a8
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v2/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = ({ 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 })
});
45 changes: 45 additions & 0 deletions src/v2/support/user_passwords/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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
};
}
};
};
19 changes: 19 additions & 0 deletions src/v2/support/user_passwords/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Joi = require('@hapi/joi');

const user_id = Joi.number().min(1);
const data = Joi.object();

module.exports = {
set: options =>
Joi.validate(options, {
user_id: user_id.required(),
data: data.required()
}),
change: options =>
Joi.validate(options, {
user_id: user_id.required(),
data: data.required()
}),
requirements: options =>
Joi.validate(options, { user_id: user_id.required() })
};
1 change: 1 addition & 0 deletions tests/src/v2/support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('support api', () => {
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));
});
75 changes: 75 additions & 0 deletions tests/src/v2/support/user_passwords.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const endpoint = require('../../../../src/v2/support/user_passwords');

const instance = 'instance';
const url = `https://${instance}.zendesk.com`;
const headers = {
'Content-Type': 'application/json',
Authorization: 'Basic <64bit_encoded_credentials>'
};

describe('user passwords', () => {
let userPasswords;

beforeEach(() => (userPasswords = endpoint({ instance, headers })));
afterEach(() => (userPasswords = null));

describe("set a user's password", () => {
it('should process w/ valid input', () => {
expect(userPasswords.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(() => userPasswords.set()).toThrowError();
expect(() => userPasswords.set('invalid')).toThrowError();
expect(() => userPasswords.set({ user_id: 'invalid' })).toThrowError();
expect(() =>
userPasswords.set({ user_id: 123, data: 'invalid' })
).toThrowError();
});
});

describe('change your password', () => {
it('should process w/ valid input', () => {
expect(userPasswords.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(() => userPasswords.change()).toThrowError();
expect(() => userPasswords.change({})).toThrowError();
expect(() => userPasswords.change('invalid')).toThrowError();
expect(() => userPasswords.change({ id: 'invalid' })).toThrowError();
expect(() =>
userPasswords.change({ id: 123, data: 'invalid' })
).toThrowError();
});
});

describe('get a list of password requirements', () => {
it('should process w/ valid input', () => {
expect(userPasswords.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(() => userPasswords.requirements()).toThrowError();
expect(() => userPasswords.requirements({})).toThrowError();
expect(() => userPasswords.requirements('invalid')).toThrowError();
expect(() =>
userPasswords.requirements({ user_id: 'invalid' })
).toThrowError();
});
});
});

0 comments on commit 1e4b3a8

Please sign in to comment.