Skip to content

Commit

Permalink
Merge pull request #38 from PlayNetwork/v1.1.3
Browse files Browse the repository at this point in the history
adding key.getClient and unit tests
  • Loading branch information
brozeph committed Dec 16, 2016
2 parents 000e628 + a861fd6 commit 0c40964
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.1.3 - 2016/12/16

* Added `getClient` to `key` sub-module

# v1.1.2 - 2016/12/01

* Added `deleteSettings` to `settings` sub-module
Expand Down
33 changes: 31 additions & 2 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,36 @@ module.exports = function (keyOptions, clientId, secret, self) {
return validation.promiseOrCallback(exec, callback);
};

self.getClient = (clientId, options, callback) => {
// handle any non-specified input params
if (typeof options === 'function') {
callback = options;
options = undefined;
}

if (typeof clientId === 'function') {
callback = clientId;
clientId = undefined;
options = undefined;
}

let exec = co(function *() {
if (validation.isEmpty(clientId)) {
return yield Promise.reject(new Error('clientId is required'));
}

let headers = yield self.ensureAuthHeaders();

return yield req.get({
headers : headers,
pathname : `/v0/clients/${clientId}`,
query : options
});
});

return validation.promiseOrCallback(exec, callback);
};

self.getTokenCacheSize = () => (tokenMap.size);

self.grantClientAccess = (clientId, serviceId, callback) => {
Expand All @@ -247,8 +277,7 @@ module.exports = function (keyOptions, clientId, secret, self) {
return yield Promise.reject(new Error('serviceId is required'));
}

let
headers = yield self.ensureAuthHeaders();
let headers = yield self.ensureAuthHeaders();

return yield req.post({
headers : headers,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playnetwork-sdk",
"version": "1.1.2",
"version": "1.1.3",
"contributors": [
{
"name": "Joshua Thomas",
Expand Down
70 changes: 70 additions & 0 deletions test/lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,76 @@ describe('key', () => {
});
});

describe('#getClient', () => {
beforeEach(() => {
// override ensureAuthHeaders
key.ensureAuthHeaders = function () {
return new Promise((resolve, reject) => {
return resolve({
'x-client-id': 'test',
'x-authentication-token': 'test'
})
})
};
});

it('should require clientId (promise)', (done) => {
key.getClient()
.then(() => {
return done(new Error('should require stationId'));
})
.catch((err) => {
should.exist(err);
should.exist(err.message);
err.message.should.contain('clientId is required');

return done();
})
});

it('should require clientId (callback)', (done) => {
key.getClient(function (err, result) {
should.exist(err);
should.exist(err.message);
err.message.should.contain('clientId is required');
should.not.exist(result);

return done();
});
});

it('should properly retrieve client (promise)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.get('/v0/clients/test')
.reply(200, { total : 0 });

key.getClient('test')
.then((result) => {
should.exist(result);
should.exist(requestInfo);

return done();
})
.catch((err) => (done(err)));
});

it('should properly retrieve client (callback)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.get('/v0/clients/test')
.reply(200, { total : 0 });

key.getClient('test', function (err, result) {
should.not.exist(err);
should.exist(result);
should.exist(requestInfo);

return done();
});
});
});

describe('#getTokenCacheSize', () => {
it('should cache tokens when enabled', (done) => {
// intercept outbound request
Expand Down

0 comments on commit 0c40964

Please sign in to comment.