Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: add test for connection with keyFilePath. #1

Merged
merged 1 commit into from
Aug 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
var fs = require('fs');
var GAPIToken = require('gapitoken');
var req = require('request');
var path = require('path');
var pkg = require('../../package.json');
var util = require('./util');

Expand Down Expand Up @@ -67,7 +66,6 @@ function Connection(opts) {

/**
* Retrieves a token to authorize the requests.

* @param {Function} callback Callback.
*/
Connection.prototype.connect = function(callback) {
Expand All @@ -93,8 +91,7 @@ Connection.prototype.connect = function(callback) {
Connection.prototype.fetchToken = function(callback) {
var that = this;
if (!this.opts.keyFilename) {
// We should be on GCE, try to retrieve token from
// the metadata from server.
// We should be on GCE, try to retrieve token from the metadata server.
req({
method: 'get',
uri: METADATA_TOKEN_URL,
Expand All @@ -113,10 +110,10 @@ Connection.prototype.fetchToken = function(callback) {
return;
}
if (!this.credentials) {
// read key file for once and cache the contents.
// read key file once and cache the contents.
fs.readFile(this.opts.keyFilename, function(err, data) {
if (err) {
callback(err); return;
return callback(err);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just switched this style to match the style used throughout the rest of this file. Though, in a future PR, I wouldn't mind doing a sweep to use the following style:

callback(err);
return;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to be consistent across the repo.

}
that.credentials = JSON.parse(data);
that.fetchServiceAccountToken_(callback);
Expand Down
130 changes: 78 additions & 52 deletions test/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,95 @@

var assert = require('assert');
var async = require('async');
var conn = require('../../lib/common/connection.js');
var fs = require('fs');
var path = require('path');

var connection = require('../../lib/common/connection.js');

describe('Connection', function() {
var tokenNeverExpires = new conn.Token('token', new Date(3000, 0, 0));
var tokenExpired = new conn.Token('token', new Date(2011, 0, 0));
it('should fetch a new token if token expires', function(done) {
var c = new conn.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
var conn;

beforeEach(function() {
conn = new connection.Connection({
keyFilename: path.join(__dirname, '../testdata/privateKeyFile.json')
});
c.token = tokenExpired;
c.fetchToken = function() {
done();
};
c.requester = function(opts, callback) {
callback(null);
};
c.req({ uri: 'https://someuri' }, function(){});
});

it('should make other requests wait while connecting', function(done) {
var numTokenFetches = 0;
var c = new conn.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
});
c.fetchToken = function(cb) {
numTokenFetches++;
setTimeout(function() {
cb(null, tokenNeverExpires);
}, 100);
};
c.requester = function(opts, callback) {
it('should use a private key json file', function(done) {
var privateKeyFileJson = require('../testdata/privateKeyFile.json');
conn.fetchServiceAccountToken_ = function(callback) {
callback(null);
};

async.parallel([
function(done) { c.req({ uri: 'https://someuri' }, done); },
function(done) { c.req({ uri: 'https://someuri' }, done); },
function(done) { c.req({ uri: 'https://someuri' }, done); }
], function(err) {
assert.equal(err, null);
assert.equal(numTokenFetches, 1);
assert.equal(c.token, tokenNeverExpires);
conn.fetchToken(function(err) {
assert.ifError(err);
assert.deepEqual(conn.credentials, privateKeyFileJson);
done();
});
});

it('should fetch a new token if token is invalid', function(done) {
var c = new conn.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
describe('Token', function() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I did was move the other tests that were previously in this file to the Token describe block, as they all were testing Token-y functionality. No changes to the code, other than using connection.Connection, instead of conn.Connection.

var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));

it('should fetch a new token if token expires', function(done) {
var c = new connection.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
});
c.token = tokenExpired;
c.fetchToken = function() {
done();
};
c.requester = function(opts, callback) {
callback(null);
};
c.req({ uri: 'https://someuri' }, function(){});
});

it('should make other requests wait while connecting', function(done) {
var numTokenFetches = 0;
var c = new connection.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
});
c.fetchToken = function(cb) {
numTokenFetches++;
setTimeout(function() {
cb(null, tokenNeverExpires);
}, 100);
};
c.requester = function(opts, callback) {
callback(null);
};

async.parallel([
function(done) { c.req({ uri: 'https://someuri' }, done); },
function(done) { c.req({ uri: 'https://someuri' }, done); },
function(done) { c.req({ uri: 'https://someuri' }, done); }
], function(err) {
assert.equal(err, null);
assert.equal(numTokenFetches, 1);
assert.equal(c.token, tokenNeverExpires);
done();
});
});

it('should fetch a new token if token is invalid', function(done) {
var c = new connection.Connection({
email: 'x@provider',
privateKey: '/some/path',
scopes: ['scope1', 'scope2']
});
c.token = new connection.Token();
c.fetchToken = function() {
done();
};
c.requester = function(opts, callback) {
callback(null);
};
c.req({ uri: 'https://someuri' }, function(){});
});
c.token = new conn.Token();
c.fetchToken = function() {
done();
};
c.requester = function(opts, callback) {
callback(null);
};
c.req({ uri: 'https://someuri' }, function(){});
});
});
7 changes: 7 additions & 0 deletions test/testdata/privateKeyFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private_key_id": "7",
"private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "8",
"type": "service_account"
}