Skip to content

Commit

Permalink
Merge pull request #213 from stephenplusplus/gce-token
Browse files Browse the repository at this point in the history
fix(connection): set token expiration date for GCE
  • Loading branch information
silvolu committed Sep 23, 2014
2 parents 7be2cae + 2eb9903 commit 23abd11
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Connection.prototype.fetchToken = function(callback) {
callback(err);
return;
}
var exp = new Date(body.token_expires * 1000);
var exp = new Date(Date.now() + body.expires_in * 1000);
callback(null, new Token(body.access_token, exp));
});
return;
Expand Down
35 changes: 35 additions & 0 deletions test/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,41 @@ describe('Connection', function() {
var tokenNeverExpires = new connection.Token('token', new Date(3000, 0, 0));
var tokenExpired = new connection.Token('token', new Date(2011, 0, 0));

describe('GCE', function() {
var gceConn;
var metadataResponse = {
body: {
access_token: 'y.8',
expires_in: 60
}
};

beforeEach(function() {
gceConn = new connection.Connection();
});

it('should fetch a token from the metadata server', function() {
gceConn.requester = function(opts) {
assert.equal(opts.uri.indexOf('http://metadata/'), 0);
};
gceConn.fetchToken();
});

it('should build token from the metadata server response', function() {
gceConn.requester = function(opts, callback) {
callback(null, metadataResponse, metadataResponse.body);
};
gceConn.fetchToken(function(err, token) {
assert.ifError(err);
assert(token instanceof connection.Token);
assert.equal(token.accessToken, metadataResponse.body.access_token);
var addedMs = metadataResponse.body.expires_in * 1000;
var tokenDate = new Date(Date.now() + addedMs);
assert.equal(token.expiry.getTime(), tokenDate.getTime());
});
});
});

it('should fetch a new token if token expires', function(done) {
conn.token = tokenExpired;
conn.fetchToken = function() {
Expand Down

0 comments on commit 23abd11

Please sign in to comment.