Skip to content

Commit

Permalink
tests: storage: refactor + bump offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 29, 2014
1 parent c5a004e commit 3bec28d
Showing 1 changed file with 36 additions and 45 deletions.
81 changes: 36 additions & 45 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ var files = {
};

function setHash(obj, file, done) {
var md5sum = crypto.createHash('md5');
var hash = crypto.createHash('md5');
fs.createReadStream(obj[file].path)
.on('data', function(d) {
md5sum.update(d);
})
.on('data', hash.update.bind(hash))
.on('end', function() {
obj[file].hash = md5sum.digest('base64');
obj[file].hash = hash.digest('base64');
done();
});
}
Expand All @@ -57,7 +55,7 @@ describe('storage', function() {
var doneCalled = 0;
Object.keys(files).forEach(function(file) {
setHash(files, file, function() {
if (++doneCalled === 2) {
if (++doneCalled === Object.keys(files).length) {
done();
}
});
Expand All @@ -66,29 +64,27 @@ describe('storage', function() {

describe('stream write', function() {
it('should stream write, then remove large file (5mb)', function(done) {
var fileName = 'LargeFile';

var filename = 'LargeFile';
fs.createReadStream(files.big.path)
.pipe(bucket.createWriteStream(fileName))
.pipe(bucket.createWriteStream(filename))
.on('error', done)
.on('complete', function(fileObject) {
assert.equal(fileObject.md5Hash, files.big.hash);
bucket.remove(fileName, done);
bucket.remove(filename, done);
});
});

it('should write and read metadata', function(done) {
var fileName = 'CloudLogo';
var filename = 'CloudLogo';
var myMetadata = { contentType: 'image/png' };

fs.createReadStream(files.logo.path)
.pipe(bucket.createWriteStream(fileName, myMetadata))
.pipe(bucket.createWriteStream(filename, myMetadata))
.on('error', done)
.on('complete', function() {
bucket.stat(fileName, function(err, metadata) {
bucket.stat(filename, function(err, metadata) {
assert.ifError(err);
assert.equal(metadata.contentType, myMetadata.contentType);
bucket.remove(fileName, function(err) {
bucket.remove(filename, function(err) {
assert.ifError(err);
done();
});
Expand All @@ -97,19 +93,19 @@ describe('storage', function() {
});

it('should write/read/remove from a buffer', function(done) {
var fileName = 'MyBuffer';
var filename = 'MyBuffer';
var fileContent = 'Hello World';
tmp.setGracefulCleanup();
tmp.file(function _tempFileCreated(err, path) {
assert.ifError(err);
bucket.write(fileName, fileContent, function(err, fileObject) {
bucket.write(filename, fileContent, function(err, fileObject) {
assert.ifError(err);
assert(fileObject);
bucket.createReadStream(fileName)
bucket.createReadStream(filename)
.pipe(fs.createWriteStream(path))
.on('error', done)
.on('finish', function() {
bucket.remove(fileName, function(err) {
bucket.remove(filename, function(err) {
assert.ifError(err);
fs.readFile(path, function(err, data) {
assert.equal(data, fileContent);
Expand All @@ -123,17 +119,16 @@ describe('storage', function() {
});

it('should copy an existing file', function(done) {
var fileName = 'CloudLogo';
var filename = 'CloudLogo';
var copyName = 'CloudLogoCopy';

fs.createReadStream(files.logo.path)
.pipe(bucket.createWriteStream(fileName))
.pipe(bucket.createWriteStream(filename))
.on('error', done)
.on('complete', function() {
bucket.copy(fileName, { name: copyName }, function(err) {
bucket.copy(filename, { name: copyName }, function(err) {
assert.ifError(err);
async.parallel([
bucket.remove.bind(bucket, fileName),
bucket.remove.bind(bucket, filename),
bucket.remove.bind(bucket, copyName)
], done);
});
Expand All @@ -151,10 +146,7 @@ describe('storage', function() {
.on('complete', function() {
bucket.copy(filenames[0], { name: filenames[1] }, function(err) {
assert.ifError(err);
bucket.copy(filenames[0], { name: filenames[2] }, function(err) {
assert.ifError(err);
done();
});
bucket.copy(filenames[0], { name: filenames[2] }, done);
});
});
});
Expand Down Expand Up @@ -192,11 +184,11 @@ describe('storage', function() {
});

describe('sign urls', function() {
var fileName = 'LogoToSign.jpg';
var filename = 'LogoToSign.jpg';

beforeEach(function(done) {
fs.createReadStream(files.logo.path)
.pipe(bucket.createWriteStream(fileName))
.pipe(bucket.createWriteStream(filename))
.on('error', done)
.on('complete', done.bind(null, null));
});
Expand All @@ -205,47 +197,46 @@ describe('storage', function() {
var signedReadUrl = bucket.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + 5,
resource: fileName
resource: filename
});
var localFile = fs.readFileSync(files.logo.path);
request.get(signedReadUrl, function(err, resp, body) {
assert.equal(body, localFile);
bucket.remove(fileName, done);
bucket.remove(filename, done);
});
});

it('should create a signed delete url', function(done) {
var signedDeleteUrl = bucket.getSignedUrl({
action: 'delete',
expires: Math.round(Date.now() / 1000) + 5,
resource: fileName
resource: filename
});
request.del(signedDeleteUrl, function(err, resp) {
assert.equal(resp.statusCode, 204);
bucket.stat(fileName, function(err) {
bucket.stat(filename, function(err) {
assert.equal(err.code, 404);
done();
});
});
});

it('should allow control of expiration', function(done) {
var offsetSeconds = 5;
var OFFSET_SECONDS = 5;
var signedReadUrl = bucket.getSignedUrl({
action: 'read',
expires: Math.round(Date.now() / 1000) + offsetSeconds,
resource: fileName
expires: Math.round(Date.now() / 1000) + OFFSET_SECONDS,
resource: filename
});
var localFile = fs.readFileSync(files.logo.path);
request.get(signedReadUrl, function(err, resp, body) {
assert.equal(body, localFile);
assert.equal(body, fs.readFileSync(files.logo.path));
setTimeout(function() {
request.get(signedReadUrl, function(err, resp) {
assert.equal(resp.statusCode, 400);
bucket.remove(filename, done);
});
}, (OFFSET_SECONDS + 5) * 1000);
});
setTimeout(function() {
request.get(signedReadUrl, function(err, resp) {
assert.equal(resp.statusCode, 400);
bucket.remove(fileName, done);
});
}, (offsetSeconds + 1) * 1000);
});
});
});

0 comments on commit 3bec28d

Please sign in to comment.