-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |