diff --git a/lib/storage/file.js b/lib/storage/file.js index dfeb29fc17c..c1bd470cd21 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -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; } @@ -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; } diff --git a/test/storage/file.js b/test/storage/file.js index 3511d554f31..b8bf82348a8 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -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 @@ -1637,7 +1674,7 @@ 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'); @@ -1645,6 +1682,45 @@ 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.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',