Skip to content

Commit

Permalink
built support:brands
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Jan 30, 2020
1 parent 23851e9 commit 16ed065
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 0 deletions.
157 changes: 157 additions & 0 deletions src/api/support/brands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _id = Joi.number().min(1);
const _host_mapping = Joi.string().min(1);
const _subdomain = Joi.string().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 {
/**
* List Brands
*
* GET /api/v2/brands.json
* https://developer.zendesk.com/rest_api/docs/support/brands#list-brands
*/
list: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/v2/brands.json`,
headers
};
},

/**
* Show a Brand
*
* GET /api/v2/brands/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/brands#show-a-brand
*/
show: (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/brands/${id}.json`,
headers
};
},

/**
* Create Brand
*
* POST /api/v2/brands.json
* https://developer.zendesk.com/rest_api/docs/support/brands#create-brand
*/
create: (options = {}) => {
const { error } = Joi.object({
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { data } = options;
return {
method: 'POST',
url: `${url}/api/v2/brands.json`,
headers,
data
};
},

/**
* Update a Brand
*
* PUT /api/v2/brands/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/brands#update-a-brand
*/
update: (options = {}) => {
const { error } = Joi.object({
id: _id.required(),
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id, data } = options;
return {
method: 'PUT',
url: `${url}/api/v2/brands/${id}.json`,
headers,
data
};
},

/**
* Delete a Brand
*
* DELETE /api/v2/tickets/{id}.json
* https://developer.zendesk.com/rest_api/docs/support/brands#delete-a-brand
*/
delete: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'DELETE',
url: `${url}/api/v2/brands/${id}.json`,
headers
};
},

/**
* Check host mapping validity
*
* GET /api/v2/brands/check_host_mapping.json?host_mapping={host_mapping}&subdomain={subdomain}
* https://developer.zendesk.com/rest_api/docs/support/brands#check-host-mapping-validity
*/
check: (options = {}) => {
const { error } = Joi.object({
host_mapping: _host_mapping.required(),
subdomain: _subdomain.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { host_mapping, subdomain } = options;
return {
method: 'GET',
url: `${url}/api/v2/brands/check_host_mapping.json?host_mapping=${host_mapping}&subdomain=${subdomain}`,
headers
};
},

/**
* Check host mapping validity for an existing brand
*
* GET /api/v2/brands/{id}/check_host_mapping.json
* https://developer.zendesk.com/rest_api/docs/support/brands#check-host-mapping-validity-for-an-existing-brand
*/
check_existing: (options = {}) => {
const { error } = Joi.object({
id: _id.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id } = options;
return {
method: 'PUT',
url: `${url}/api/v2/brands/${id}/check_host_mapping.json`,
headers
};
}
};
};
155 changes: 155 additions & 0 deletions tests/src/api/support/brands.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
const endpoint = require('../../../../src/api/support/brands');
const { prepare } = require('../../../../src/utils/options');

describe('Brands', () => {
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('List Brands', () => {
it('should process w/ valid input', () => {
expect(endPoint.list()).toEqual({
method: 'GET',
url: `${url}/api/v2/brands.json`,
headers
});
});
});

describe('Show a Brand', () => {
it('should process w/ valid input', () => {
expect(endPoint.show({ id: 123 })).toEqual({
method: 'GET',
url: `${url}/api/v2/brands/123.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.show()).toThrowError();
expect(() => endPoint.show({})).toThrowError();
expect(() => endPoint.show({ id: 'invalid' })).toThrowError();
});
});

describe('Create Brand', () => {
it('should process w/ valid input', () => {
expect(endPoint.create({ data: {} })).toEqual({
method: 'POST',
url: `${url}/api/v2/brands.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.create()).toThrowError();
expect(() => endPoint.create('invalid')).toThrowError();
expect(() => endPoint.create({})).toThrowError();
expect(() => endPoint.create({ data: 'invalid' })).toThrowError();
});
});

describe('Update a Brand', () => {
it('should process w/ valid input', () => {
expect(endPoint.update({ id: 123, data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/v2/brands/123.json`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.update()).toThrowError();
expect(() => endPoint.update({})).toThrowError();
expect(() => endPoint.update({ data: {} })).toThrowError();
expect(() => endPoint.update({ id: 'invalid', data: {} })).toThrowError();
expect(() =>
endPoint.update({ id: 123, data: 'invalid' })
).toThrowError();
});
});

describe('Delete a Brand', () => {
it('should process w/ valid input', () => {
expect(endPoint.delete({ id: 123 })).toEqual({
method: 'DELETE',
url: `${url}/api/v2/brands/123.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.delete()).toThrowError();
expect(() => endPoint.delete('invalid')).toThrowError();
expect(() => endPoint.delete({})).toThrowError();
expect(() => endPoint.delete({ id: 'invalid' })).toThrowError();
});
});

describe('Check host mapping validity', () => {
it('should process w/ valid input', () => {
expect(
endPoint.check({ host_mapping: 'host_mapping', subdomain: 'subdomain' })
).toEqual({
method: 'GET',
url: `${url}/api/v2/brands/check_host_mapping.json?host_mapping=host_mapping&subdomain=subdomain`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.check()).toThrowError();
expect(() => endPoint.check('invalid')).toThrowError();
expect(() => endPoint.check({})).toThrowError();
expect(() => endPoint.check({ host_mapping: 0 })).toThrowError();
expect(() =>
endPoint.check({ host_mapping: 'valid', subdomain: 0 })
).toThrowError();
});
});

describe('Check host mapping validity for an existing brand', () => {
it('should process w/ valid input', () => {
expect(endPoint.check_existing({ id: 123 })).toEqual({
method: 'PUT',
url: `${url}/api/v2/brands/123/check_host_mapping.json`,
headers
});
});

it('should throw error w/ invalid input', () => {
expect(() => endPoint.check_existing()).toThrowError();
expect(() => endPoint.check_existing('invalid')).toThrowError();
expect(() => endPoint.check_existing({})).toThrowError();
expect(() => endPoint.check_existing({ id: 0 })).toThrowError();
});
});
});

0 comments on commit 16ed065

Please sign in to comment.