Skip to content

Commit

Permalink
test file.isPublic() via object without credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman committed May 14, 2019
1 parent 49949d0 commit 33cf2c1
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ describe('storage', () => {
(err, files) => {
assert.ifError(err);
const file = files![0];
assert.strictEqual(file.isPublic(), true);
file.download(done);
}
);
Expand All @@ -233,6 +234,7 @@ describe('storage', () => {
});

it('should not download a file', done => {
assert.strictEqual(file.isPublic(), false);
file.download(err => {
assert(
err!.message.indexOf('does not have storage.objects.get') > -1
Expand Down Expand Up @@ -387,7 +389,7 @@ describe('storage', () => {

await bucket.makePublic({includeFiles: true});
const [files] = await bucket.getFiles();
const [resps] = await Promise.all(files.map(file => file.isPublic()));
const resps = await Promise.all(files.map(file => isFilePublicAsync(file)));
resps.forEach(resp => assert.strictEqual(resp, true));
await Promise.all([
bucket.acl.default.delete({entity: 'allUsers'}),
Expand Down Expand Up @@ -417,7 +419,7 @@ describe('storage', () => {

await bucket.makePrivate({includeFiles: true});
const [files] = await bucket.getFiles();
const [resps] = await Promise.all(files.map(file => file.isPublic()));
const resps = await Promise.all(files.map(file => isFilePublicAsync(file)));
resps.forEach(resp => {
assert.strictEqual(resp, false);
});
Expand Down Expand Up @@ -3138,6 +3140,26 @@ describe('storage', () => {
);
}

async function isFilePublicAsync(file: File) {
try {
const [aclObject] = await file.acl.get({entity: 'allUsers'});
if (
(aclObject as AccessControlObject).entity === 'allUsers' &&
(aclObject as AccessControlObject).role === 'READER'
) {
return true;
} else {
return false;
}
} catch (error) {
if (error.code === 404) {
return false;
} else {
throw error;
}
}
}

// tslint:disable-next-line no-any
function createFileAsync(fileObject: any) {
return fileObject.file.save(fileObject.contents);
Expand Down

0 comments on commit 33cf2c1

Please sign in to comment.