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 a1478b0
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,11 @@ describe('storage', () => {
bucket = storageWithoutAuth.bucket('gcp-public-data-landsat');
});

it('should list and download a file', done => {
bucket.getFiles(
{
autoPaginate: false,
},
(err, files) => {
assert.ifError(err);
const file = files![0];
file.download(done);
}
);
it('should list and download a file', async () => {
const [files] = await bucket.getFiles({autoPaginate: false});
const file = files[0];
assert.strictEqual(await file.isPublic(), true);
assert.doesNotReject(file.download());
});
});

Expand All @@ -232,13 +226,13 @@ describe('storage', () => {
file = bucket.file(privateFile.id!);
});

it('should not download a file', done => {
file.download(err => {
assert(
err!.message.indexOf('does not have storage.objects.get') > -1
);
done();
});
it('should not download a file', async () => {
assert.strictEqual(await file.isPublic(), false);
assert.rejects(
file.download(),
(err: Error) =>
err.message.indexOf('does not have storage.objects.get') > -1
);
});

it('should not upload a file', done => {
Expand Down Expand Up @@ -387,7 +381,9 @@ 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 +413,9 @@ 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 +3136,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 a1478b0

Please sign in to comment.