Skip to content

Commit

Permalink
built support:audit_logs
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Jan 23, 2020
1 parent 970a511 commit 9dae0c0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/api/support/audit_logs.js
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
};
}
};
};
71 changes: 71 additions & 0 deletions tests/src/api/support/audit_logs.test.js
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();
});
});
});

0 comments on commit 9dae0c0

Please sign in to comment.