Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Aug 23, 2017
1 parent 84e7aa5 commit d31902d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
21 changes: 4 additions & 17 deletions packages/error-reporting/src/google-apis/auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ var API = 'https://clouderrorreporting.googleapis.com/v1beta1';
* @property {Object} _logger - the instance-cached logger instance
*/
class RequestHandler extends common.Service {
/**
* Returns a query-string request object if a string key is given, otherwise
* will return null.
* @param {String|Null} [key] - the API key used to authenticate against the
* service in place of application default credentials.
* @returns {Object|Null} api key query string object for use with request or
* null in case no api key is given
* @static
*/
static manufactureQueryString(key) {
if (isString(key)) {
return {key: key};
}
return null;
}
/**
* No-operation stub function for user callback substitution
* @param {Error|Null} err - the error
Expand Down Expand Up @@ -113,7 +98,7 @@ class RequestHandler extends common.Service {
}
});
} else {
that._logger.info('API key provided; skipping OAuth2 token generation.');
that._logger.info('API key provided; skipping OAuth2 token request.');
}
}
/**
Expand All @@ -134,7 +119,9 @@ class RequestHandler extends common.Service {
if (this._config.getShouldReportErrorsToAPI()) {
this.request({
uri: 'events:report',
qs: RequestHandler.manufactureQueryString(this._config.getKey()),
qs: {
key: this._config.getKey()
},
method: 'POST',
json: errorMessage
}, (err, body, response) => {
Expand Down
25 changes: 18 additions & 7 deletions packages/error-reporting/test/unit/google-apis/auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var proxyquire = require('proxyquire');

var Configuration = require('../../../src/configuration.js');

function verifyReportedMessage(errToReturn, expectedMessage) {
function verifyReportedMessage(config, errToReturn, expectedMessage) {
class ServiceStub {
constructor() {
this.authClient = {
Expand All @@ -41,24 +41,35 @@ function verifyReportedMessage(errToReturn, expectedMessage) {
var logger = {
error: function(text) {
message += text;
}
},
info: function() {}
};
var config = new Configuration({ ignoreEnvironmentCheck: true }, logger);
var config = new Configuration(config, logger);
new RequestHandler(config, logger);
assert.strictEqual(message, expectedMessage);
}

describe('RequestHandler', function() {
it('should not request OAuth2 token if key is provided', function() {
var config = {
ignoreEnvironmentCheck: true,
key: 'key'
};
var message = 'Made OAuth2 Token Request';
verifyReportedMessage(config, new Error(message), '');
});

it('should issue a warning if it cannot communicate with the API', function() {
var config = { ignoreEnvironmentCheck: true };
var message = 'Test Error';
verifyReportedMessage(new Error(message),
verifyReportedMessage(config, new Error(message),
'Unable to find credential information on instance. This library ' +
'will be unable to communicate with the Stackdriver API to save ' +
'errors. Message: ' + message);
});

it('should not issue a warning if it can communicate with the API', function() {
verifyReportedMessage(null, '');
verifyReportedMessage(undefined, '');
var config = { ignoreEnvironmentCheck: true };
verifyReportedMessage(config, null, '');
verifyReportedMessage(config, undefined, '');
});
});

0 comments on commit d31902d

Please sign in to comment.