Skip to content

Commit

Permalink
add regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Mar 23, 2015
1 parent b5d81b7 commit caea1d6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
10 changes: 1 addition & 9 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,7 @@ Bucket.prototype.makePublic = function(options, callback) {
options = options || {};
options.public = true;

async.parallel([
setPredefinedAcl,
addAclPermissions,
makeFilesPublic
], callback);

function setPredefinedAcl(done) {
self.setPredefinedAcl_(options, done);
}
async.parallel([addAclPermissions, makeFilesPublic], callback);

function addAclPermissions(done) {
// Allow reading bucket contents while preserving original permissions.
Expand Down
91 changes: 91 additions & 0 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,58 @@ describe('storage', function() {
});
});

it('should make files public', function(done) {
async.each(['a', 'b', 'c'], createFileWithContent, function(err) {
assert.ifError(err);

bucket.makePublic({ includeFiles: true }, function(err) {
assert.ifError(err);

bucket.getFiles(function(err, files) {
assert.ifError(err);

async.each(files, isFilePublic, function(err) {
assert.ifError(err);

async.parallel([
function(next) {
bucket.acl.default.delete({ entity: 'allUsers' }, next);
},
function(next) {
deleteFiles(bucket, next);
}
], done);
});
});
});
});

function createFileWithContent(content, callback) {
bucket.file(uuid() + '.txt').createWriteStream()
.on('error', callback)
.on('complete', function() {
callback();
})
.end(content);
}

function isFilePublic(file, callback) {
file.acl.get({ entity: 'allUsers' }, function(err, aclObject) {
if (err) {
callback(err);
return;
}

if (aclObject.entity === 'allUsers' &&
aclObject.role === 'READER') {
callback();
} else {
callback(new Error('File is not public.'));
}
});
}
});

it('should be made private', function(done) {
bucket.makePublic(function(err) {
assert.ifError(err);
Expand All @@ -201,6 +253,45 @@ describe('storage', function() {
});
});
});

it('should make files private', function(done) {
async.each(['a', 'b', 'c'], createFileWithContent, function(err) {
assert.ifError(err);

bucket.makePrivate({ includeFiles: true }, function(err) {
assert.ifError(err);

bucket.getFiles(function(err, files) {
assert.ifError(err);

async.each(files, isFilePrivate, function(err) {
assert.ifError(err);

deleteFiles(bucket, done);
});
});
});
});

function createFileWithContent(content, callback) {
bucket.file(uuid() + '.txt').createWriteStream()
.on('error', callback)
.on('complete', function() {
callback();
})
.end(content);
}

function isFilePrivate(file, callback) {
file.acl.get({ entity: 'allUsers' }, function(err) {
if (err && err.code === 404) {
callback();
} else {
callback(new Error('File is not private.'));
}
});
}
});
});

describe('files', function() {
Expand Down
12 changes: 1 addition & 11 deletions test/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,6 @@ describe('Bucket', function() {
});

it('should set predefined & default ACL, publicize files', function(done) {
bucket.setPredefinedAcl = function(opts, callback) {
assert.strictEqual(options.public, true);
assert.strictEqual(options.force, true);
callback();
};

bucket.acl.default.add = function(opts, callback) {
assert.equal(opts.entity, 'allUsers');
assert.equal(opts.role, 'READER');
Expand All @@ -472,10 +466,6 @@ describe('Bucket', function() {
});

it('should not make files public by default', function(done) {
bucket.setPredefinedAcl = function(opts, callback) {
callback();
};

bucket.acl.default.add = function(opts, callback) {
callback();
};
Expand All @@ -490,7 +480,7 @@ describe('Bucket', function() {
it('should execute callback with error', function(done) {
var error = new Error('Error.');

bucket.setPredefinedAcl_ = function(opts, callback) {
bucket.acl.default.add = function(opts, callback) {
callback(error);
};

Expand Down

0 comments on commit caea1d6

Please sign in to comment.