-
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
2 changed files
with
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const Joi = require('@hapi/joi'); | ||
const { validate, prepare } = require('../../utils/options'); | ||
|
||
// Validation | ||
const _params = Joi.string().min(1); | ||
const _id = Joi.number().positive(); | ||
|
||
// Initialize Endpoint | ||
module.exports = (options = {}) => { | ||
const { error } = validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { url, headers } = prepare(options); | ||
|
||
return { | ||
/** | ||
* Listing Audit Logs | ||
* | ||
* GET /api/v2/audit_logs.json | ||
* https://developer.zendesk.com/rest_api/docs/support/audit_logs#listing-audit-logs | ||
*/ | ||
list: (options = {}) => { | ||
const { error } = Joi.object({ | ||
params: _params | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { params } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/audit_logs.json${params ? `?${params}` : ''}`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* Getting Audit Logs | ||
* | ||
* GET /api/v2/audit_logs/{id}.json | ||
* https://developer.zendesk.com/rest_api/docs/support/audit_logs#getting-audit-logs | ||
*/ | ||
get: (options = {}) => { | ||
const { error } = Joi.object({ | ||
id: _id.required() | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { id } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/audit_logs/${id}.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,71 @@ | ||
const endpoint = require('../../../../src/api/support/audit_logs'); | ||
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', () => { | ||
expect(endpoint(options)).toBeTruthy(); | ||
}); | ||
|
||
it('should fail with invalid input', () => { | ||
expect(() => endpoint()).toThrowError(); | ||
expect(() => endpoint({})).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('listing audit logs', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/audit_logs.json`, | ||
headers | ||
}); | ||
|
||
expect(endPoint.list({ params: 'filter[source_type]=user' })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/audit_logs.json?filter[source_type]=user`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.list('invalid')).toThrowError(); | ||
expect(() => endPoint.list({ params: 0 })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('getting audit logs', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.get({ id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/audit_logs/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.get()).toThrowError(); | ||
expect(() => endPoint.get({})).toThrowError(); | ||
expect(() => endPoint.get({ id: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
}); |