Skip to content

Commit

Permalink
Merge pull request #74 from PlayNetwork/v1.4.1
Browse files Browse the repository at this point in the history
adding createClient route to the key for use by the background locati…
  • Loading branch information
brozeph committed Oct 24, 2018
2 parents 7a091f6 + b0a5fb2 commit a8911e4
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 2 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.4.1 - 2018/10/24

* Added additional support methods for keys

# v1.4.0 - 2018/10/15

* Http(s).request now uses a keep-alive agent by default
Expand Down
44 changes: 44 additions & 0 deletions lib/claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,50 @@ module.exports = function (claimOptions, ensureAuthHeaders, self) {
return validation.promiseOrCallback(exec, callback);
};

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

if (typeof serviceId === 'function') {
callback = serviceId;
serviceId = undefined;
claims = undefined;
}

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

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'));
}

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

let headers = yield ensureAuthHeaders();

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

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

self.settings = () => (settings);

self.version = (callback) => {
Expand Down
27 changes: 27 additions & 0 deletions lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ module.exports = function (keyOptions, clientId, secret, self) {
return validation.promiseOrCallback(exec, callback);
};

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

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

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

let headers = yield self.ensureAuthHeaders();

return yield req.post({
headers : headers,
pathname : '/v0/clients'
}, client);
});

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

self.disableClient = (clientId, callback) => {
// handle any non-specified input params
if (typeof clientId === 'function') {
Expand Down
96 changes: 94 additions & 2 deletions test/lib/claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('claim', () => {

it('should properly retrieve all claims for a given client and service (promise)', (done) => {
// intercept outbound request

nock('https://claim-api.apps.playnetwork.com')
.get(`/v0/clients/${mockClientId}/services/${mockServiceId}/claims`)
.reply(200, { total : 0 });
Expand All @@ -125,7 +125,7 @@ describe('claim', () => {

it('should properly retrieve all claims for a given client (promise)', (done) => {
// intercept outbound request

nock('https://claim-api.apps.playnetwork.com')
.get(`/v0/clients/${mockClientId}/claims`)
.reply(200, { total : 0 });
Expand All @@ -140,4 +140,96 @@ describe('claim', () => {
.catch((err) => (done(err)));
});
});

describe('#createClaims', () => {
const mockClaims = {
name : 'test claims'
};
const mockClientId = 'mock-client-id';
const mockServiceId = 'mock-service-id';

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

return done();
})
});

it('should require serviceId (promise)', (done) => {
claim.createClaims(mockClientId)
.then(() => {
return done(new Error('should require serviceId'));
})
.catch((err) => {
should.exist(err);
should.exist(err.message);
err.message.should.contain('serviceId is required');

return done();
})
});

it('should require claim details (promise)', (done) => {
claim.createClaims(mockClientId, mockServiceId)
.then(() => {
return done(new Error('should require claims'));
})
.catch((err) => {
should.exist(err);
should.exist(err.message);
err.message.should.contain('claims are required');

return done();
})
});

it('should require claim details (callback)', (done) => {
claim.createClaims(mockClientId, mockServiceId, function (err, result) {
should.exist(err);
should.exist(err.message);
err.message.should.contain('claims are required');
should.not.exist(result);

return done();
});
});

it('should properly create claims (promise)', (done) => {
// intercept outbound request
nock('https://claim-api.apps.playnetwork.com')
.post(`/v0/clients/${mockClientId}/services/${mockServiceId}/claims`)
.reply(201, mockClaims);

claim.createClaims(mockClientId, mockServiceId, mockClaims)
.then((result) => {
should.exist(result);
should.exist(requestInfo);

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

it('should properly create claims (callback)', (done) => {
// intercept outbound request
nock('https://claim-api.apps.playnetwork.com')
.post(`/v0/clients/${mockClientId}/services/${mockServiceId}/claims`)
.reply(201, mockClaims);

claim.createClaims(mockClientId, mockServiceId, mockClaims, function (err, result) {
should.not.exist(err);
should.exist(result);
should.exist(requestInfo);

return done();
});
});
});
});
85 changes: 85 additions & 0 deletions test/lib/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,91 @@ describe('key', () => {
});
});

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

let mockClient = {
name : 'test client'
};

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

return done();
})
});

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

return done();
});
});

it('should require name', (done) => {
key.createClient({ other : true }, function (err, result) {
should.not.exist(result);
should.exist(err);
should.exist(err.message);
err.message.should.contain('client name is required');

return done();
});
});

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

key.createClient(mockClient)
.then((result) => {
should.exist(result);
should.exist(requestInfo);

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

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

key.createClient(mockClient, function (err, result) {
should.not.exist(err);
should.exist(result);
should.exist(requestInfo);

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

describe('#disableClient', () => {
beforeEach(() => {
// override ensureAuthHeaders
Expand Down

0 comments on commit a8911e4

Please sign in to comment.