Skip to content

Commit

Permalink
added sunshine.object_types
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 12, 2019
1 parent be0f8b0 commit 008c302
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v2/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = ({ instance, headers }) => ({
sunshine: require('./sunshine')({ instance, headers }),
support: require('./support')({ instance, headers })
});
3 changes: 3 additions & 0 deletions src/v2/sunshine/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = ({ instance, headers }) => ({
object_types: require('./object_types')({ instance, headers })
});
87 changes: 87 additions & 0 deletions src/v2/sunshine/object_types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const Joi = require('@hapi/joi');

const _key = Joi.string()
.min(2)
.max(32);
const _data = Joi.object({
key: _key,
schema: Joi.object({
properties: Joi.object(),
required: Joi.array()
}),
end_users_can_read: Joi.bool()
});

module.exports = ({ instance, headers }) => {
const url = `https://${instance}.zendesk.com`;

return {
list: () => {
// Ignore any options
return {
method: 'GET',
url: `${url}/api/sunshine/objects/types`,
headers
};
},

show: (options = {}) => {
const { error } = Joi.object({
key: _key.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { key } = options;
return {
method: 'GET',
url: `${url}/api/sunshine/objects/types/${key}`,
headers
};
},

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/sunshine/objects/types`,
headers,
data
};
},

update: (options = {}) => {
const { error } = Joi.object({
key: _key.required(),
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { key, data } = options;
return {
method: 'PUT',
url: `${url}/api/sunshine/objects/types/${key}`,
headers,
data
};
},

delete: (options = {}) => {
const { error } = Joi.object({
key: _key.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { key } = options;
return {
method: 'DELETE',
url: `${url}/api/sunshine/objects/types/${key}`,
headers
};
}
};
};
107 changes: 107 additions & 0 deletions tests/src/v2/sunshine/object_types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const endpoint = require('../../../../src/v2/sunshine/object_types');

const instance = 'instance';
const url = `https://${instance}.zendesk.com`;
const headers = {
'Content-Type': 'application/json',
Authorization: 'Basic <64bit_encoded_credentials>'
};

describe('object types', () => {
let objectTypes;

beforeEach(() => (objectTypes = endpoint({ instance, headers })));
afterEach(() => (objectTypes = null));

describe('list object types', () => {
it('should process, ignores additional input', () => {
expect(objectTypes.list()).toEqual({
method: 'GET',
url: `${url}/api/sunshine/objects/types`,
headers
});
});
});

describe('show object type', () => {
it('should process w/ valid input', () => {
expect(objectTypes.show({ key: 'valid' })).toEqual({
method: 'GET',
url: `${url}/api/sunshine/objects/types/valid`,
headers
});
});

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

describe('create object type', () => {
it('should process w/ valid input', () => {
const data = {
key: 'valid',
schema: {
properties: {},
required: ['valid']
},
end_users_can_read: false
};

expect(objectTypes.create({ data })).toEqual({
method: 'POST',
url: `${url}/api/sunshine/objects/types`,
headers,
data
});
});

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

describe('update custom role', () => {
it('should process w/ valid input', () => {
expect(objectTypes.update({ key: 'valid', data: {} })).toEqual({
method: 'PUT',
url: `${url}/api/sunshine/objects/types/valid`,
headers,
data: {}
});
});

it('should throw error w/ invalid input', () => {
expect(() => objectTypes.update()).toThrowError();
expect(() => objectTypes.update({})).toThrowError();
expect(() => objectTypes.update('invalid')).toThrowError();
expect(() => objectTypes.update({ key: 0 })).toThrowError();
expect(() =>
objectTypes.update({ key: 'valid', data: 'invalid' })
).toThrowError();
});
});

describe('delete custom role', () => {
it('should process w/ valid input', () => {
expect(objectTypes.delete({ key: 'valid' })).toEqual({
method: 'DELETE',
url: `${url}/api/sunshine/objects/types/valid`,
headers
});
});

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

0 comments on commit 008c302

Please sign in to comment.