-
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.
Merge pull request #77 from androozka/support_user-passwords
built support:user-passwords
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 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
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,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 | ||
}; | ||
} | ||
}; | ||
}; |
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,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() }) | ||
}; |
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
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,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(); | ||
}); | ||
}); | ||
}); |