Skip to content

Commit

Permalink
Merge pull request #3 from RallyTools/api_key_support
Browse files Browse the repository at this point in the history
Add api key support
  • Loading branch information
krmorse committed Apr 21, 2014
2 parents 5c5223e + 8365d6f commit 6780806
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
7 changes: 7 additions & 0 deletions lib/request.js
Expand Up @@ -6,9 +6,16 @@ var Q = require('q'),
function Request(options) {
this.wsapiUrl = format('%s/slm/webservice/%s', options.server, options.apiVersion);
this.httpRequest = request.defaults(options.requestOptions);
this._hasKey = options.requestOptions &&
options.requestOptions.headers &&
options.requestOptions.headers.zsessionid;
}

Request.prototype.doSecuredRequest = function(method, options, callback) {
if(this._hasKey) {
return this.doRequest(method, options, callback);
}

var self = this,
deferred = Q.defer();

Expand Down
31 changes: 24 additions & 7 deletions lib/restapi.js
Expand Up @@ -19,8 +19,9 @@ var _ = require('lodash'),
@param {object} options (optional) - optional config for the REST client
- @member {string} server - server for the Rally API (default: https://rally1.rallydev.com)
- @member {string} apiVersion - the Rally REST API version to use for requests (default: v2.0)
- @member {string} userName||user - the username to use for requests (default: RALLY_USERNAME env variable)
- @member {string} password||pass - the password to use for requests (default: RALLY_PASSWORD env variable)
- @member {string} userName||user - the username to use for requests (default: RALLY_USERNAME env variable) (@deprecated in favor of apiKey)
- @member {string} password||pass - the password to use for requests (default: RALLY_PASSWORD env variable) (@deprecated in favor of apiKey)
- @member {string} apiKey - the api key to use for requests (default: RALLY_API_KEY env variable)
- @member {object} requestOptions - default options for the request: https://github.com/mikeal/request
*/
function RestApi(options) {
Expand All @@ -30,11 +31,6 @@ function RestApi(options) {
requestOptions: {
jar: true,
json: true,
auth: {
user: (options && (options.user || options.userName)) || process.env.RALLY_USERNAME,
pass: (options && (options.pass || options.password)) || process.env.RALLY_PASSWORD,
sendImmediately: false
},
headers: {
'X-RallyIntegrationLibrary': format('%s v%s', pkgInfo.description, pkgInfo.version),
'X-RallyIntegrationName': pkgInfo.description,
Expand All @@ -44,6 +40,27 @@ function RestApi(options) {
}
}, options);

var apiKey = (options && options.apiKey) || process.env.RALLY_API_KEY;
if(apiKey) {
options = _.merge({
requestOptions: {
headers: {
zsessionid: apiKey
}
}
}, options);
} else {
options = _.merge({
requestOptions: {
auth: {
user: (options && (options.user || options.userName)) || process.env.RALLY_USERNAME,
pass: (options && (options.pass || options.password)) || process.env.RALLY_PASSWORD,
sendImmediately: false
}
}
}, options);
}

this.request = require('./request').init(options);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "rally",
"version": "0.1.2",
"version": "0.1.3",
"description": "Rally REST Toolkit for Node.js",
"contributors": [
{
Expand Down
12 changes: 12 additions & 0 deletions spec/request.spec.js
Expand Up @@ -176,6 +176,18 @@ describe('Request', function() {

describe('#doSecuredRequest', function() {

it('does not request a security token if api key specified', function() {
var rr = createRequest({
requestOptions: {
headers: {
zsessionid: '!#$%!@#$@!#'
}
}
});
rr.doSecuredRequest('put', {foo: 'bar'});
this.get.callCount.should.eql(0);
});

it('requests a security token', function() {
var rr = createRequest();
rr.doSecuredRequest('put', {foo: 'bar'});
Expand Down
33 changes: 25 additions & 8 deletions spec/restapi.spec.js
Expand Up @@ -58,36 +58,53 @@ describe('RestApi', function() {
requestOptions.jar.should.eql(true);
requestOptions.auth.user.should.eql('user');
requestOptions.auth.pass.should.eql('pass');
requestOptions.auth.sendImmediately.should.eql(false);
restApi.request.should.be.exactly(request.init.firstCall.returnValue);
});

it('initializes request with specified auth options', function() {
var restApi = new RestApi({
requestOptions: {
auth: {
user: 'user1',
pass: 'pass1'
}
}
user: 'user1',
pass: 'pass1'
});
var requestOptions = request.init.firstCall.args[0].requestOptions;
requestOptions.jar.should.eql(true);
requestOptions.auth.user.should.eql('user1');
requestOptions.auth.pass.should.eql('pass1');
requestOptions.auth.sendImmediately.should.eql(false);
restApi.request.should.be.exactly(request.init.firstCall.returnValue);
});

it('initializes request with correct integration headers', function() {
var restApi = new RestApi();
var initArgs = request.init.firstCall.args[0];
initArgs.requestOptions.headers.should.eql({
'X-RallyIntegrationLibrary': 'Rally REST Toolkit for Node.js v0.1.2',
'X-RallyIntegrationLibrary': 'Rally REST Toolkit for Node.js v0.1.3',
'X-RallyIntegrationName': 'Rally REST Toolkit for Node.js',
'X-RallyIntegrationVendor': 'Rally Software, Inc.',
'X-RallyIntegrationVersion': '0.1.2'
'X-RallyIntegrationVersion': '0.1.3'
});
restApi.request.should.be.exactly(request.init.firstCall.returnValue);
});

it('initializes request with default api key options', function() {
var key = '!#$!@#%161345!%1346@#$^#$';
process.env.RALLY_API_KEY = key;
var restApi = new RestApi();
var requestOptions = request.init.firstCall.args[0].requestOptions;
requestOptions.headers.zsessionid.should.eql(key);
restApi.request.should.be.exactly(request.init.firstCall.returnValue);
});

it('initializes request with specified api key options', function() {
var key = '!#$!@#%161345!%1346@#$^#$';
var restApi = new RestApi({
apiKey: key
});
var requestOptions = request.init.firstCall.args[0].requestOptions;
requestOptions.headers.zsessionid.should.eql(key);
restApi.request.should.be.exactly(request.init.firstCall.returnValue);
});
});

describe('#create', function() {
Expand Down

0 comments on commit 6780806

Please sign in to comment.