Skip to content

Commit

Permalink
execute callback with error if signing is not possible
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 9, 2015
1 parent bcf5b79 commit 262a1da
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
28 changes: 26 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,19 @@ File.prototype.getSignedPolicy = function(options, callback) {

makeAuthorizedRequest_.getCredentials(function(err, credentials) {
if (err) {
callback(err);
var signingError = new Error('Signing failed. See `error` property.');
signingError.error = err;
callback(signingError);
return;
}

if (!credentials.private_key) {
var errorMessage = [
'Signing failed. Could not find a `private_key`.',
'Please verify you are authorized with this property available.'
].join(' ');

callback(new Error(errorMessage));
return;
}

Expand Down Expand Up @@ -1180,7 +1192,19 @@ File.prototype.getSignedUrl = function(options, callback) {

makeAuthorizedRequest_.getCredentials(function(err, credentials) {
if (err) {
callback(err);
var signingError = new Error('Signing failed. See `error` property.');
signingError.error = err;
callback(signingError);
return;
}

if (!credentials.private_key || !credentials.client_email) {
var errorMessage = [
'Signing failed. Could not find a `private_key` or `client_email`.',
'Please verify you are authorized with these credentials available.'
].join(' ');

callback(new Error(errorMessage));
return;
}

Expand Down
78 changes: 77 additions & 1 deletion test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,43 @@ describe('File', function() {
});
});

it('should return an error if getCredentials errors', function(done) {
var error = new Error('Error.');

var storage = bucket.storage;
storage.makeAuthorizedRequest_.getCredentials = function(callback) {
callback(error);
};

file.getSignedPolicy({
expires: Date.now() + 5
}, function(err) {
var errorMessage = 'Signing failed. See `error` property.';
assert.strictEqual(err.message, errorMessage);
assert.strictEqual(err.error, error);
done();
});
});

it('should return an error if credentials are not present', function(done) {
var storage = bucket.storage;
storage.makeAuthorizedRequest_.getCredentials = function(callback) {
callback(null, {});
};

file.getSignedPolicy({
expires: Date.now() + 5
}, function(err) {
var errorMessage = [
'Signing failed. Could not find a `private_key`.',
'Please verify you are authorized with this property available.'
].join(' ');

assert.strictEqual(err.message, errorMessage);
done();
});
});

it('should add key equality condition', function(done) {
file.getSignedPolicy({
expires: Date.now() + 5
Expand Down Expand Up @@ -1637,14 +1674,53 @@ describe('File', function() {
it('should create a signed url', function(done) {
file.getSignedUrl({
action: 'read',
expires: Date.now() + 5,
expires: Date.now() + 5
}, function(err, signedUrl) {
assert.ifError(err);
assert.equal(typeof signedUrl, 'string');
done();
});
});

it('should return an error if getCredentials errors', function(done) {
var error = new Error('Error.');

var storage = bucket.storage;
storage.makeAuthorizedRequest_.getCredentials = function(callback) {
callback(error);
};

file.getSignedUrl({
action: 'read',
expires: Date.now() + 5
}, function(err) {
var errorMessage = 'Signing failed. See `error` property.';
assert.strictEqual(err.message, errorMessage);
assert.strictEqual(err.error, error);
done();
});
});

it('should return an error if credentials are not present', function(done) {
var storage = bucket.storage;
storage.makeAuthorizedRequest_.getCredentials = function(callback) {
callback(null, {});
};

file.getSignedUrl({
action: 'read',
expires: Date.now() + 5
}, function(err) {
var errorMessage = [
'Signing failed. Could not find a `private_key` or `client_email`.',
'Please verify you are authorized with these credentials available.'
].join(' ');

assert.strictEqual(err.message, errorMessage);
done();
});
});

it('should URI encode file names', function(done) {
directoryFile.getSignedUrl({
action: 'read',
Expand Down

0 comments on commit 262a1da

Please sign in to comment.