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

execute callback with error if signing is not possible #857

Merged
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
execute callback with error if signing is not possible
  • Loading branch information
stephenplusplus committed Sep 9, 2015
commit 262a1da53e0dacf242dbfc9b1d0bd6ed8dabb3f7
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);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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