Skip to content

Commit 7be2cae

Browse files
committed
Merge pull request #239 from stephenplusplus/spp--signed-url-test
storage: fix signed url tests. fixes #238
2 parents 50704b7 + 0ff03a1 commit 7be2cae

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/storage/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ Bucket.prototype.remove = function(name, callback) {
325325
*
326326
* {@link https://developers.google.com/storage/docs/accesscontrol#Signed-URLs}
327327
*
328+
* @throws {Error} if an expiration timestamp from the past is given.
329+
*
328330
* @param {object} options - Configuration object.
329331
* @param {string} options.action - "read", "write", or "delete"
330332
* @param {string=} options.contentMd5 - The MD5 digest value in base64. If you
@@ -346,6 +348,10 @@ Bucket.prototype.remove = function(name, callback) {
346348
* }, function(err, url) {});
347349
*/
348350
Bucket.prototype.getSignedUrl = function(options, callback) {
351+
if (options.expires < Math.floor(Date.now() / 1000)) {
352+
throw new Error('An expiration date cannot be in the past.');
353+
}
354+
349355
options.action = {
350356
read: 'GET',
351357
write: 'PUT',

regression/storage.js

-20
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,5 @@ describe('storage', function() {
224224
});
225225
});
226226
});
227-
228-
it('should allow control of expiration', function(done) {
229-
var offsetSeconds = 5;
230-
bucket.getSignedUrl({
231-
action: 'read',
232-
expires: Math.round(Date.now() / 1000) + offsetSeconds,
233-
resource: filename
234-
}, function(err, signedReadUrl) {
235-
assert.ifError(err);
236-
request.get(signedReadUrl, function(err, resp, body) {
237-
assert.equal(body, localFile);
238-
});
239-
setTimeout(function() {
240-
request.get(signedReadUrl, function(err, resp) {
241-
assert.equal(resp.statusCode, 400);
242-
bucket.remove(filename, done);
243-
});
244-
}, (offsetSeconds + 1) * 1000);
245-
});
246-
});
247227
});
248228
});

test/storage/index.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
var assert = require('assert');
2222
var gcloud = require('../../lib');
2323
var storage = require('../../lib/storage');
24+
var url = require('url');
2425

2526
var credentials = require('../testdata/privateKeyFile.json');
2627
var noop = function() {};
@@ -142,15 +143,46 @@ describe('Bucket', function() {
142143
bucket.remove('file-name');
143144
});
144145

145-
it('should create a signed url', function(done) {
146-
bucket.getSignedUrl({
146+
describe('getSignedUrl', function() {
147+
it('should create a signed url', function(done) {
148+
bucket.getSignedUrl({
147149
action: 'read',
148-
resource: 'filename',
149-
expires: Date.now() / 1000
150-
}, function(err, url) {
150+
expires: Math.round(Date.now() / 1000) + 5,
151+
resource: 'filename'
152+
}, function(err, signedUrl) {
151153
assert.ifError(err);
152-
assert.equal(typeof url, 'string');
154+
assert.equal(typeof signedUrl, 'string');
153155
done();
154156
});
157+
});
158+
159+
describe('expires', function() {
160+
var nowInSeconds = Math.floor(Date.now() / 1000);
161+
162+
it('should use the provided expiration date', function(done) {
163+
var expirationTimestamp = nowInSeconds + 60;
164+
bucket.getSignedUrl({
165+
action: 'read',
166+
resource: 'filename',
167+
expires: expirationTimestamp
168+
}, function(err, signedUrl) {
169+
assert.ifError(err);
170+
var expires = url.parse(signedUrl, true).query.Expires;
171+
assert.equal(expires, expirationTimestamp);
172+
done();
173+
});
174+
});
175+
176+
it('should throw if a date from the past is given', function() {
177+
var expirationTimestamp = nowInSeconds - 1;
178+
assert.throws(function() {
179+
bucket.getSignedUrl({
180+
action: 'read',
181+
resource: 'filename',
182+
expires: expirationTimestamp
183+
}, function() {});
184+
}, /cannot be in the past/);
185+
});
186+
});
155187
});
156188
});

0 commit comments

Comments
 (0)