Skip to content

Commit

Permalink
Merge pull request #239 from stephenplusplus/spp--signed-url-test
Browse files Browse the repository at this point in the history
storage: fix signed url tests. fixes #238
  • Loading branch information
silvolu committed Sep 23, 2014
2 parents 50704b7 + 0ff03a1 commit 7be2cae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
6 changes: 6 additions & 0 deletions lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ Bucket.prototype.remove = function(name, callback) {
*
* {@link https://developers.google.com/storage/docs/accesscontrol#Signed-URLs}
*
* @throws {Error} if an expiration timestamp from the past is given.
*
* @param {object} options - Configuration object.
* @param {string} options.action - "read", "write", or "delete"
* @param {string=} options.contentMd5 - The MD5 digest value in base64. If you
Expand All @@ -346,6 +348,10 @@ Bucket.prototype.remove = function(name, callback) {
* }, function(err, url) {});
*/
Bucket.prototype.getSignedUrl = function(options, callback) {
if (options.expires < Math.floor(Date.now() / 1000)) {
throw new Error('An expiration date cannot be in the past.');
}

options.action = {
read: 'GET',
write: 'PUT',
Expand Down
20 changes: 0 additions & 20 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,25 +224,5 @@ describe('storage', function() {
});
});
});

it('should allow control of expiration', function(done) {
var offsetSeconds = 5;
bucket.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + offsetSeconds,
resource: filename
}, function(err, signedReadUrl) {
assert.ifError(err);
request.get(signedReadUrl, function(err, resp, body) {
assert.equal(body, localFile);
});
setTimeout(function() {
request.get(signedReadUrl, function(err, resp) {
assert.equal(resp.statusCode, 400);
bucket.remove(filename, done);
});
}, (offsetSeconds + 1) * 1000);
});
});
});
});
44 changes: 38 additions & 6 deletions test/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
var assert = require('assert');
var gcloud = require('../../lib');
var storage = require('../../lib/storage');
var url = require('url');

var credentials = require('../testdata/privateKeyFile.json');
var noop = function() {};
Expand Down Expand Up @@ -142,15 +143,46 @@ describe('Bucket', function() {
bucket.remove('file-name');
});

it('should create a signed url', function(done) {
bucket.getSignedUrl({
describe('getSignedUrl', function() {
it('should create a signed url', function(done) {
bucket.getSignedUrl({
action: 'read',
resource: 'filename',
expires: Date.now() / 1000
}, function(err, url) {
expires: Math.round(Date.now() / 1000) + 5,
resource: 'filename'
}, function(err, signedUrl) {
assert.ifError(err);
assert.equal(typeof url, 'string');
assert.equal(typeof signedUrl, 'string');
done();
});
});

describe('expires', function() {
var nowInSeconds = Math.floor(Date.now() / 1000);

it('should use the provided expiration date', function(done) {
var expirationTimestamp = nowInSeconds + 60;
bucket.getSignedUrl({
action: 'read',
resource: 'filename',
expires: expirationTimestamp
}, function(err, signedUrl) {
assert.ifError(err);
var expires = url.parse(signedUrl, true).query.Expires;
assert.equal(expires, expirationTimestamp);
done();
});
});

it('should throw if a date from the past is given', function() {
var expirationTimestamp = nowInSeconds - 1;
assert.throws(function() {
bucket.getSignedUrl({
action: 'read',
resource: 'filename',
expires: expirationTimestamp
}, function() {});
}, /cannot be in the past/);
});
});
});
});

0 comments on commit 7be2cae

Please sign in to comment.