Skip to content

Commit

Permalink
alphabetically ordered functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PN-mruiz committed Oct 27, 2016
1 parent cfa2259 commit 602397c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 93 deletions.
44 changes: 22 additions & 22 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ module.exports = function (keyOptions, clientId, secret, self) {

let tokenMap = new Map();

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

if (validation.isEmpty(serviceId)) {
return yield Promise.reject(new Error('serviceId is required'));
}

let
headers = yield self.ensureAuthHeaders();

return yield req.post({
headers : headers,
pathname : `/v0/clients/${clientId}/services/${serviceId}`
});
});

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

self.allClients = (options, callback) => {
// handle any non-specified input params
if (typeof options === 'function') {
Expand Down Expand Up @@ -259,6 +237,28 @@ module.exports = function (keyOptions, clientId, secret, self) {

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

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

if (validation.isEmpty(serviceId)) {
return yield Promise.reject(new Error('serviceId is required'));
}

let
headers = yield self.ensureAuthHeaders();

return yield req.post({
headers : headers,
pathname : `/v0/clients/${clientId}/services/${serviceId}`
});
});

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

self.purgeTokenCache = (clientId) => {
if (validation.isEmpty(clientId)) {
let success = tokenMap.size > 0;
Expand Down
142 changes: 71 additions & 71 deletions test/lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,77 +88,6 @@ describe('key', () => {
});
});

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

it('should detect missing clientId', (done) => {
key.grantClientAccess()
.then(() => (done('clientId is required')))
.catch((err) => {
should.exist(err);
should.exist(err.message);
err.message.should.equal('clientId is required');

return done();
});
});

it('should detect missing serviceId', (done) => {
key.grantClientAccess('clientId', undefined, function (err, token) {
should.exist(err);
should.exist(err.message);
err.message.should.equal('serviceId is required');
should.not.exist(token);

return done();
});
});

it('should properly validate client (promise)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.post('/v0/clients/clientId/services/serviceId')
.reply(201, {});

key.grantClientAccess('clientId', 'serviceId')
.then((client) => {
should.exist(client);

return done();
})
.catch(done);
});

it('should properly validate client (callback)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.post('/v0/clients/clientId/services/serviceId')
.reply(201, {});

key.grantClientAccess('clientId', 'serviceId',
(err, client) => {
if (err) {
return done(err);
}

should.exist(client);

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

describe('#allClients', () => {
beforeEach(() => {
// override ensureAuthHeaders
Expand Down Expand Up @@ -501,6 +430,77 @@ describe('key', () => {
});
});

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

it('should detect missing clientId', (done) => {
key.grantClientAccess()
.then(() => (done('clientId is required')))
.catch((err) => {
should.exist(err);
should.exist(err.message);
err.message.should.equal('clientId is required');

return done();
});
});

it('should detect missing serviceId', (done) => {
key.grantClientAccess('clientId', undefined, function (err, token) {
should.exist(err);
should.exist(err.message);
err.message.should.equal('serviceId is required');
should.not.exist(token);

return done();
});
});

it('should properly validate client (promise)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.post('/v0/clients/clientId/services/serviceId')
.reply(201, {});

key.grantClientAccess('clientId', 'serviceId')
.then((client) => {
should.exist(client);

return done();
})
.catch(done);
});

it('should properly validate client (callback)', (done) => {
// intercept outbound request
nock('https://key-api.apps.playnetwork.com')
.post('/v0/clients/clientId/services/serviceId')
.reply(201, {});

key.grantClientAccess('clientId', 'serviceId',
(err, client) => {
if (err) {
return done(err);
}

should.exist(client);

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

describe('#purgeTokenCache', () => {
it('should return false when nothing is purgeable', () => {
key.getTokenCacheSize().should.equal(0);
Expand Down

0 comments on commit 602397c

Please sign in to comment.