Skip to content

Commit

Permalink
feat: CMS-2558 clients resource
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Feb 13, 2017
1 parent aebbade commit 2ac4fbc
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/Accounts.js
Expand Up @@ -10,6 +10,8 @@ import {
} from './helper';
import AccountList from './resources/AccountList';
import AccountResource from './resources/AccountResource';
import ClientList from './resources/ClientList';
import ClientResource from './resources/ClientResource';
import InvitesResource from './resources/InvitesResource';
import TokenStoreFactory from './TokenStore';

Expand Down Expand Up @@ -341,4 +343,33 @@ export default class Accounts extends Core {
.then(([invites, traversal]) => new InvitesResource(invites, this.environment, traversal));
});
}

clientList(options) {
return Promise.resolve()
.then(() => {
if (options && Object.keys(options).length === 1 && 'clientID' in options) {
throw new Error('Providing only an clientID in ClientList filter will result in single resource response. Please use Accounts#client');
}

const request = this.newRequest()
.follow('ec:acc/clients/options')
.withTemplateParameters(optionsToQuery(options));
return get(this.environment, request);
})
.then(([res, traversal]) => new ClientList(res, this.environment, traversal));
}

client(clientID) {
return Promise.resolve()
.then(() => {
if (!clientID) {
throw new Error('accountID must be defined');
}
const request = this.newRequest()
.follow('ec:acc/clients/options')
.withTemplateParameters({ clientid: clientID });
return get(this.environment, request);
})
.then(([res, traversal]) => new ClientResource(res, this.environment, traversal));
}
}
22 changes: 22 additions & 0 deletions src/resources/ClientList.js
@@ -0,0 +1,22 @@
import ListResource from './ListResource';
import ClientResource from './ClientResource';

/**
* Client list class
*
* @class
*/
export default class ClientList extends ListResource {
/**
* Creates a new {@link ClientList}.
*
* @param {object} resource resource loaded from the API.
* @param {string} environment the environment this resource is associated to.
* @param {?object} traversal traversal from which traverson can continue.
*/
constructor(resource, environment, traversal) {
super(resource, environment, 'ec:acc/client', traversal);
this.ListClass = ClientList;
this.ItemClass = ClientResource;
}
}
34 changes: 34 additions & 0 deletions src/resources/ClientResource.js
@@ -0,0 +1,34 @@
import Resource from './Resource';

/**
* ClientResource class
*
* @class
*/
export default class ClientResource extends Resource {
getClientID() {
return this.getProperty('clientID');
}

getCallbackURL() {
return this.getProperty('callbackURL');
}

getConfig() {
return this.getProperty('config');
}

setCallbackURL(value) {
if (!value) {
throw new Error('callbackURL must be defined.');
}
return this.setProperty('callbackURL', value);
}

setConfig(value) {
if (!value) {
throw new Error('config must be defined.');
}
return this.setProperty('config', value);
}
}
39 changes: 39 additions & 0 deletions test/Account.test.js
Expand Up @@ -11,6 +11,8 @@ const Accounts = require('../lib/Accounts').default;
const ListResource = require('../lib/resources/ListResource').default;
const AccountList = require('../lib/resources/AccountList').default;
const AccountResource = require('../lib/resources/AccountResource').default;
const ClientList = require('../lib/resources/ClientList').default;
const ClientResource = require('../lib/resources/ClientResource').default;
const InvitesResource = require('../lib/resources/InvitesResource').default;
const Resource = require('../lib/resources/Resource').default;

Expand Down Expand Up @@ -293,6 +295,43 @@ describe('Accounts class', () => {
throw err;
});
});
it('should return list on clientList', () => {
const accounts = new Accounts('live');
const stub = sinon.stub(helper, 'get');
stub.returns(resolver('client-list.json'));

return accounts.clientList()
.then((list) => {
list.should.be.instanceof(ClientList);
stub.restore();
})
.catch((err) => {
stub.restore();
throw err;
});
});
it('should be rejected on clientList only with clientID', () => {
return new Accounts().clientList({ clientID: 'id' }).should.be.rejectedWith(Error);
});
it('should return resource on client', () => {
const accounts = new Accounts('live');
const stub = sinon.stub(helper, 'get');
stub.returns(resolver('account-list.json'));

return accounts.client('aID')
.then((resource) => {
resource.should.be.instanceof(ClientResource);
stub.restore();
})
.catch((err) => {
stub.restore();
throw err;
});
});
it('should be rejected on client with undefiend id', () => {
return new Accounts().client().should.be.rejectedWith(Error);
});

});

describe('Account ListResource', () => {
Expand Down
116 changes: 116 additions & 0 deletions test/ClientResource.test.js
@@ -0,0 +1,116 @@
/* eslint no-unused-expressions:0 */

const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const fs = require('fs');

const ClientResource = require('../lib/resources/ClientResource').default;
const ClientList = require('../lib/resources/ClientList').default;
const Resource = require('../lib/resources/Resource').default;
const ListResource = require('../lib/resources/ListResource').default;

const should = chai.should();
chai.use(sinonChai);

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

describe('Client ListResource', () => {
let listJson;
let list;
before(() => {
return new Promise((resolve, reject) => {
fs.readFile(`${__dirname}/mocks/client-list.json`, 'utf-8', (err, res) => {
if (err) {
return reject(err);
}
return resolve(JSON.parse(res));
});
})
.then((json) => {
listJson = json;
});
});
beforeEach(() => {
list = new ClientList(listJson);
});
afterEach(() => {
list = null;
});
it('should be instance of ListResource', () => {
list.should.be.instanceOf(ListResource);
});
it('should be instance of ClientList', () => {
list.should.be.instanceOf(ClientList);
});
it('should have TokenResource items', () => {
list.getAllItems().forEach(item => item.should.be.instanceOf(ClientResource));
});
});

describe('Token Resource', () => {
let resourceJson;
let resource;
before(() => {
return new Promise((resolve, reject) => {
fs.readFile(`${__dirname}/mocks/client-single.json`, 'utf-8', (err, res) => {
if (err) {
return reject(err);
}
return resolve(JSON.parse(res));
});
})
.then((json) => {
resourceJson = json;
});
});
beforeEach(() => {
resource = new ClientResource(resourceJson);
});
afterEach(() => {
resource = null;
});
it('should be instance of Resource', () => {
resource.should.be.instanceOf(Resource);
});
it('should be instance of ClientResource', () => {
resource.should.be.instanceOf(ClientResource);
});

const getter = [
'clientID', 'callbackURL', 'config',
];
getter.forEach((name) => {
it(`should call resource.getProperty with ${name}`, () => {
const spy = sinon.spy(resource, 'getProperty');

const property = resource[`get${capitalizeFirstLetter(name)}`]();
spy.should.have.been.called.once;
spy.should.have.been.calledWith(name);
property.should.be.equal(resource.getProperty(name));

spy.restore();
});
});

const setter = [
'callbackURL', 'config',
];
setter.forEach((name) => {
it(`should call resource.setProperty with ${name}`, () => {
const spy = sinon.spy(resource, 'setProperty');

resource[`set${capitalizeFirstLetter(name)}`](resource.getProperty(name));
spy.should.have.been.called.once;
spy.should.have.been.calledWith(name, resource.getProperty(name));

spy.restore();
});
it(`should throw on set${capitalizeFirstLetter(name)} with undefined value`, () => {
const throws = () => resource[`set${capitalizeFirstLetter(name)}`]();
throws.should.throw(Error);
});
});
});
2 changes: 1 addition & 1 deletion test/TokenResource.test.js
Expand Up @@ -42,7 +42,7 @@ describe('Token ListResource', () => {
it('should be instance of ListResource', () => {
list.should.be.instanceOf(ListResource);
});
it('should be instance of TokenLisst', () => {
it('should be instance of TokenList', () => {
list.should.be.instanceOf(TokenList);
});
it('should have TokenResource items', () => {
Expand Down

0 comments on commit 2ac4fbc

Please sign in to comment.