From 67715b1895eaefc7b4c94588374a224249698592 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 16:33:14 +0000 Subject: [PATCH 01/13] refactor: system tests should use await --- system-test/storage.ts | 89 ++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 6904081dc..194c2acfd 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -220,12 +220,15 @@ describe('storage', () => { describe('acls', () => { describe('buckets', () => { - it('should get access controls', done => { - bucket.acl.get((err, accessControls) => { - assert.ifError(err); + it('should get access controls', async () => { + try { + const accessControls = await bucket.acl.get(); + console.log(accessControls); assert(Array.isArray(accessControls)); - done(); - }); + } + catch (e) { + assert.ifError(e); + } }); it('should add entity to default access controls', done => { @@ -332,18 +335,19 @@ describe('storage', () => { ); }); - it('should make a bucket public', done => { - bucket.makePublic(err => { - assert.ifError(err); - bucket.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.ifError(err); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - bucket.acl.delete({entity: 'allUsers'}, done); + it('should make a bucket public', async () => { + try { + await bucket.makePublic(); + const [aclObject] = await bucket.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', }); - }); + await bucket.acl.delete({entity: 'allUsers'}); + } + catch (e) { + assert.ifError(e); + } }); it('should make files public', async () => { @@ -363,22 +367,30 @@ describe('storage', () => { ]); }); - it('should make a bucket private', done => { - bucket.makePublic(err => { - assert.ifError(err); - bucket.makePrivate(err => { - assert.ifError(err); - bucket.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.strictEqual((err as ApiError).code, 404); - assert.strictEqual( - (err as ApiError).errors![0].reason, - 'notFound' - ); - assert.strictEqual(aclObject, null); - done(); - }); - }); - }); + it('should make a bucket private', async () => { + try { + await bucket.makePublic(); + await bucket.makePrivate(); + } + catch (e) { + assert.ifError(e); + } + + const validateMakeBucketPrivateRejects = ( + err: ApiError + ) => { + assert.strictEqual(err.code, 404); + assert.strictEqual( + (err as ApiError).errors![0].reason, + 'notFound' + ); + return true; + }; + + await assert.rejects( + bucket.acl.get({entity: 'allUsers'}), + validateMakeBucketPrivateRejects + ); }); it('should make files private', async () => { @@ -417,13 +429,14 @@ describe('storage', () => { file.delete(done); }); - it('should get access controls', done => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - file.acl.get(done as any, (err, accessControls) => { - assert.ifError(err); + it('should get access controls', async () => { + try { + const accessControls = file.acl.get(); assert(Array.isArray(accessControls)); - done(); - }); + } + catch (e) { + assert.ifError(e); + } }); it('should not expose default api', () => { From 52d2a01ddaf5b8a77c19a1b2db7753f8366f7ae3 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 16:37:15 +0000 Subject: [PATCH 02/13] linted files --- system-test/storage.ts | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 194c2acfd..fe4a330f9 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -225,8 +225,7 @@ describe('storage', () => { const accessControls = await bucket.acl.get(); console.log(accessControls); assert(Array.isArray(accessControls)); - } - catch (e) { + } catch (e) { assert.ifError(e); } }); @@ -344,8 +343,7 @@ describe('storage', () => { role: 'READER', }); await bucket.acl.delete({entity: 'allUsers'}); - } - catch (e) { + } catch (e) { assert.ifError(e); } }); @@ -371,19 +369,13 @@ describe('storage', () => { try { await bucket.makePublic(); await bucket.makePrivate(); - } - catch (e) { + } catch (e) { assert.ifError(e); } - const validateMakeBucketPrivateRejects = ( - err: ApiError - ) => { + const validateMakeBucketPrivateRejects = (err: ApiError) => { assert.strictEqual(err.code, 404); - assert.strictEqual( - (err as ApiError).errors![0].reason, - 'notFound' - ); + assert.strictEqual((err as ApiError).errors![0].reason, 'notFound'); return true; }; @@ -433,8 +425,7 @@ describe('storage', () => { try { const accessControls = file.acl.get(); assert(Array.isArray(accessControls)); - } - catch (e) { + } catch (e) { assert.ifError(e); } }); From e811d2c60c04da68eb8c5f3825c8ed66d01dd70e Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 17:02:52 +0000 Subject: [PATCH 03/13] refactored more tests --- system-test/storage.ts | 141 +++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 89 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index fe4a330f9..fb9cd76ac 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -223,115 +223,78 @@ describe('storage', () => { it('should get access controls', async () => { try { const accessControls = await bucket.acl.get(); - console.log(accessControls); assert(Array.isArray(accessControls)); } catch (e) { assert.ifError(e); } }); - it('should add entity to default access controls', done => { - bucket.acl.default.add( - { + it('should add entity to default access controls', async () => { + try { + const [accessControl] = await bucket.acl.default.add({ entity: USER_ACCOUNT, role: storage.acl.OWNER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - - bucket.acl.default.get( - { - entity: USER_ACCOUNT, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual( - (accessControl as AccessControlObject)!.role, - storage.acl.OWNER_ROLE - ); + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - bucket.acl.default.update( - { - entity: USER_ACCOUNT, - role: storage.acl.READER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual( - (accessControl as AccessControlObject).role, - storage.acl.READER_ROLE - ); - - bucket.acl.default.delete({entity: USER_ACCOUNT}, done); - } - ); - } - ); - } - ); + const [updatedAccessControl] = await bucket.acl.default.update({ + entity: USER_ACCOUNT, + role: storage.acl.READER_ROLE, + }); + assert.strictEqual( + updatedAccessControl.role, + storage.acl.READER_ROLE + ); + await bucket.acl.default.delete({entity: USER_ACCOUNT}); + } catch (e) { + assert.ifError(e); + } }); - it('should get default access controls', done => { - bucket.acl.default.get((err, accessControls) => { - assert.ifError(err); + it('should get default access controls', async () => { + try { + const accessControls = await bucket.acl.default.get(); assert(Array.isArray(accessControls)); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should grant an account access', done => { - bucket.acl.add( - { + it('should grant an account access', async () => { + try { + const [accessControl] = await bucket.acl.add({ entity: USER_ACCOUNT, role: storage.acl.OWNER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - - const opts = {entity: USER_ACCOUNT}; - - bucket.acl.get(opts, (err, accessControl) => { - assert.ifError(err); - assert.strictEqual( - (accessControl as AccessControlObject).role, - storage.acl.OWNER_ROLE - ); - - bucket.acl.delete(opts, done); - }); - } - ); + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const opts = {entity: USER_ACCOUNT}; + const [accessControlGet] = await bucket.acl.get(opts); + assert.strictEqual( + (accessControlGet as AccessControlObject).role, + storage.acl.OWNER_ROLE + ); + bucket.acl.delete(opts); + } catch (e) { + assert.ifError(e); + } }); - it('should update an account', done => { - bucket.acl.add( - { + it('should update an account', async () => { + try { + const [accessControl] = await bucket.acl.add({ entity: USER_ACCOUNT, role: storage.acl.OWNER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - - bucket.acl.update( - { - entity: USER_ACCOUNT, - role: storage.acl.WRITER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual( - accessControl!.role, - storage.acl.WRITER_ROLE - ); - - bucket.acl.delete({entity: USER_ACCOUNT}, done); - } - ); - } - ); + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [updatedAcl] = await bucket.acl.update({ + entity: USER_ACCOUNT, + role: storage.acl.WRITER_ROLE, + }); + assert.strictEqual(updatedAcl!.role, storage.acl.WRITER_ROLE); + await bucket.acl.delete({entity: USER_ACCOUNT}); + } catch (e) { + assert.ifError(e); + } }); it('should make a bucket public', async () => { From b7ccb079b2c48bffca854fa83a8064cf552b8a3b Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 17:27:37 +0000 Subject: [PATCH 04/13] more tests --- system-test/storage.ts | 136 ++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 82 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index fb9cd76ac..48707aaba 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -273,7 +273,7 @@ describe('storage', () => { (accessControlGet as AccessControlObject).role, storage.acl.OWNER_ROLE ); - bucket.acl.delete(opts); + await bucket.acl.delete(opts); } catch (e) { assert.ifError(e); } @@ -329,19 +329,13 @@ describe('storage', () => { }); it('should make a bucket private', async () => { - try { - await bucket.makePublic(); - await bucket.makePrivate(); - } catch (e) { - assert.ifError(e); - } - + assert.doesNotReject(bucket.makePublic()); + assert.doesNotReject(bucket.makePrivate()); const validateMakeBucketPrivateRejects = (err: ApiError) => { assert.strictEqual(err.code, 404); assert.strictEqual((err as ApiError).errors![0].reason, 'notFound'); return true; }; - await assert.rejects( bucket.acl.get({entity: 'allUsers'}), validateMakeBucketPrivateRejects @@ -386,7 +380,7 @@ describe('storage', () => { it('should get access controls', async () => { try { - const accessControls = file.acl.get(); + const [accessControls] = await file.acl.get(); assert(Array.isArray(accessControls)); } catch (e) { assert.ifError(e); @@ -398,89 +392,67 @@ describe('storage', () => { assert.strictEqual(typeof (file as any).default, 'undefined'); }); - it('should grant an account access', done => { - file.acl.add( - { + it('should grant an account access', async () => { + try { + const [accessControl] = await file.acl.add({ entity: USER_ACCOUNT, role: storage.acl.OWNER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - - file.acl.get({entity: USER_ACCOUNT}, (err, accessControl) => { - assert.ifError(err); - assert.strictEqual( - (accessControl as AccessControlObject).role, - storage.acl.OWNER_ROLE - ); - - file.acl.delete({entity: USER_ACCOUNT}, done); - }); - } - ); + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [accessControlGet] = await file.acl.get({entity: USER_ACCOUNT}); + assert.strictEqual( + (accessControlGet as AccessControlObject).role, + storage.acl.OWNER_ROLE + ); + await file.acl.delete({entity: USER_ACCOUNT}); + } catch (e) { + assert.ifError(e); + } }); - it('should update an account', done => { - file.acl.add( - { + it('should update an account', async () => { + try { + const [accessControl] = await file.acl.add({ entity: USER_ACCOUNT, role: storage.acl.OWNER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - - file.acl.update( - { - entity: USER_ACCOUNT, - role: storage.acl.READER_ROLE, - }, - (err, accessControl) => { - assert.ifError(err); - - assert.strictEqual( - accessControl!.role, - storage.acl.READER_ROLE - ); - - file.acl.delete({entity: USER_ACCOUNT}, done); - } - ); - } - ); + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [accessControlUpdate] = await file.acl.update({ + entity: USER_ACCOUNT, + role: storage.acl.READER_ROLE, + }); + assert.strictEqual( + accessControlUpdate!.role, + storage.acl.READER_ROLE + ); + await file.acl.delete({entity: USER_ACCOUNT}); + } catch (e) { + assert.ifError(e); + } }); - it('should make a file public', done => { - file.makePublic(err => { - assert.ifError(err); - file.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.ifError(err); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - file.acl.delete({entity: 'allUsers'}, done); + it('should make a file public', async () => { + try { + await file.makePublic(); + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', }); - }); + await file.acl.delete({entity: 'allUsers'}); + } catch (e) { + assert.ifError(e); + } }); - it('should make a file private', done => { - file.makePublic(err => { - assert.ifError(err); - file.makePrivate(err => { - assert.ifError(err); - file.acl.get( - {entity: 'allUsers'}, - (err: ApiError | null, aclObject) => { - assert.strictEqual(err!.code, 404); - assert.strictEqual(err!.errors![0].reason, 'notFound'); - assert.strictEqual(aclObject, null); - done(); - } - ); - }); - }); + it('should make a file private', async () => { + const validateMakeFilePrivateRejects = (err: ApiError) => { + assert.strictEqual(err.code, 404); + assert.strictEqual(err!.errors![0].reason, 'notFound'); + return true; + }; + assert.doesNotReject(file.makePublic()); + assert.rejects(file.makePrivate(), validateMakeFilePrivateRejects); }); it('should set custom encryption during the upload', done => { From 55cafb1866a0e86cc153fbd6df40c372e7d1d4b1 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 17:54:36 +0000 Subject: [PATCH 05/13] more tests --- system-test/storage.ts | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 48707aaba..f3c7833b0 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -329,8 +329,12 @@ describe('storage', () => { }); it('should make a bucket private', async () => { - assert.doesNotReject(bucket.makePublic()); - assert.doesNotReject(bucket.makePrivate()); + try { + await bucket.makePublic(); + await bucket.makePrivate(); + } catch (e) { + assert.ifError(e); + } const validateMakeBucketPrivateRejects = (err: ApiError) => { assert.strictEqual(err.code, 404); assert.strictEqual((err as ApiError).errors![0].reason, 'notFound'); @@ -452,30 +456,28 @@ describe('storage', () => { return true; }; assert.doesNotReject(file.makePublic()); - assert.rejects(file.makePrivate(), validateMakeFilePrivateRejects); + assert.doesNotReject(file.makePrivate()); + assert.rejects( + file.acl.get({entity: 'allUsers'}), + validateMakeFilePrivateRejects + ); }); - it('should set custom encryption during the upload', done => { + it('should set custom encryption during the upload', async () => { const key = '12345678901234567890123456789012'; - bucket.upload( - FILES.big.path, - { + try { + const [file] = await bucket.upload(FILES.big.path, { encryptionKey: key, resumable: false, - }, - (err, file) => { - assert.ifError(err); - - file!.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - assert.strictEqual( - metadata.customerEncryption.encryptionAlgorithm, - 'AES256' - ); - done(); - }); - } - ); + }); + const [metadata] = await file.getMetadata(); + assert.strictEqual( + metadata.customerEncryption.encryptionAlgorithm, + 'AES256' + ); + } catch (e) { + assert.ifError(e); + } }); it('should set custom encryption in a resumable upload', done => { From a77b3adf273ebf87dca9353594e8d92a5852e087 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 18:38:02 +0000 Subject: [PATCH 06/13] finished acl tests --- system-test/storage.ts | 121 +++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 72 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index f3c7833b0..e9e9088e4 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -480,94 +480,71 @@ describe('storage', () => { } }); - it('should set custom encryption in a resumable upload', done => { + it('should set custom encryption in a resumable upload', async () => { const key = crypto.randomBytes(32); - - bucket.upload( - FILES.big.path, - { + try { + const [file] = await bucket.upload(FILES.big.path, { encryptionKey: key, resumable: true, - }, - (err, file) => { - assert.ifError(err); - - file!.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - assert.strictEqual( - metadata.customerEncryption.encryptionAlgorithm, - 'AES256' - ); - done(); - }); - } - ); + }); + const [metadata] = await file.getMetadata(); + assert.strictEqual( + metadata.customerEncryption.encryptionAlgorithm, + 'AES256' + ); + } catch (e) { + assert.ifError(e); + } }); - it('should make a file public during the upload', done => { - bucket.upload( - FILES.big.path, - { + it('should make a file public during the upload', async () => { + try { + const [file] = await bucket.upload(FILES.big.path, { resumable: false, public: true, - }, - (err, file) => { - assert.ifError(err); + }); - file!.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.ifError(err); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - done(); - }); - } - ); + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); + } catch (e) { + assert.ifError(e); + } }); - it('should make a file public from a resumable upload', done => { - bucket.upload( - FILES.big.path, - { + it('should make a file public from a resumable upload', async () => { + try { + const [file] = await bucket.upload(FILES.big.path, { resumable: true, public: true, - }, - (err, file) => { - assert.ifError(err); - - file!.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.ifError(err); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - done(); - }); - } - ); + }); + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); + } catch (e) { + assert.ifError(e); + } }); - it('should make a file private from a resumable upload', done => { - bucket.upload( - FILES.big.path, - { + it('should make a file private from a resumable upload', async () => { + const validateMakeFilePrivateRejects = (err: ApiError) => { + assert.strictEqual((err as ApiError)!.code, 404); + assert.strictEqual((err as ApiError).errors![0].reason, 'notFound'); + return true; + }; + assert.doesNotReject( + bucket.upload(FILES.big.path, { resumable: true, private: true, - }, - (err, file) => { - assert.ifError(err); - - file!.acl.get({entity: 'allUsers'}, (err, aclObject) => { - assert.strictEqual((err as ApiError)!.code, 404); - assert.strictEqual( - (err as ApiError).errors![0].reason, - 'notFound' - ); - assert.strictEqual(aclObject, null); - done(); - }); - } + }) + ); + assert.rejects( + file.acl.get({entity: 'allUsers'}), + validateMakeFilePrivateRejects ); }); }); From d1b5db8e6dcfbc4357f169ec5fd4e3d18846c751 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 18:59:37 +0000 Subject: [PATCH 07/13] more tests --- system-test/storage.ts | 177 ++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 101 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index e9e9088e4..66bd09795 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -572,9 +572,9 @@ describe('storage', () => { return bucket.create(); }); - it('should get a policy', done => { - bucket.iam.getPolicy((err, policy) => { - assert.ifError(err); + it('should get a policy', async () => { + try { + const [policy] = await bucket.iam.getPolicy(); assert.deepStrictEqual(policy!.bindings, [ { members: [ @@ -588,33 +588,28 @@ describe('storage', () => { role: 'roles/storage.legacyBucketReader', }, ]); - - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should set a policy', done => { - bucket.iam.getPolicy((err, policy) => { - assert.ifError(err); + it('should set a policy', async () => { + try { + const [policy] = await bucket.iam.getPolicy(); policy!.bindings.push({ role: 'roles/storage.legacyBucketReader', members: ['allUsers'], }); - - bucket.iam.setPolicy(policy!, (err, newPolicy) => { - assert.ifError(err); - - const legacyBucketReaderBinding = newPolicy!.bindings.filter( - binding => { - return binding.role === 'roles/storage.legacyBucketReader'; - } - )[0]; - - assert(legacyBucketReaderBinding.members.includes('allUsers')); - - done(); - }); - }); + const [newPolicy] = await bucket.iam.setPolicy(policy); + const legacyBucketReaderBinding = newPolicy!.bindings.filter( + binding => { + return binding.role === 'roles/storage.legacyBucketReader'; + } + )[0]; + assert(legacyBucketReaderBinding.members.includes('allUsers')); + } catch (e) { + assert.ifError(e); + } }); it('should get-modify-set a conditional policy', async () => { @@ -652,22 +647,22 @@ describe('storage', () => { assert.deepStrictEqual(newPolicy.bindings, policy.bindings); }); - it('should test the iam permissions', done => { + it('should test the iam permissions', async () => { const testPermissions = [ 'storage.buckets.get', 'storage.buckets.getIamPolicy', ]; - - bucket.iam.testPermissions(testPermissions, (err, permissions) => { - assert.ifError(err); - + try { + const [permissions] = await bucket.iam.testPermissions( + testPermissions + ); assert.deepStrictEqual(permissions, { 'storage.buckets.get': true, 'storage.buckets.getIamPolicy': true, }); - - done(); - }); + } catch (e) { + assert.ifError(e); + } }); }); }); @@ -1095,15 +1090,16 @@ describe('storage', () => { ); }); - it('should get buckets', done => { - storage.getBuckets((err, buckets) => { + it('should get buckets', async () => { + try { + const [buckets] = await storage.getBuckets(); const createdBuckets = buckets.filter(bucket => { return bucketsToCreate.indexOf(bucket.name) > -1; }); - assert.strictEqual(createdBuckets.length, bucketsToCreate.length); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); it('should get buckets as a stream', done => { @@ -1123,7 +1119,7 @@ describe('storage', () => { }); describe('bucket metadata', () => { - it('should allow setting metadata on a bucket', done => { + it('should allow setting metadata on a bucket', async () => { const metadata = { website: { mainPageSuffix: 'http://fakeuri', @@ -1131,11 +1127,12 @@ describe('storage', () => { }, }; - bucket.setMetadata(metadata, (err: ApiError | null, meta: Metadata) => { - assert.ifError(err); + try { + const [meta] = await bucket.setMetadata(metadata); assert.deepStrictEqual(meta.website, metadata.website); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); it('should allow changing the storage class', async () => { @@ -1198,81 +1195,59 @@ describe('storage', () => { bucket.deleteLabels(done); }); - it('should set labels', done => { - bucket.setLabels(LABELS, err => { - assert.ifError(err); - - bucket.getLabels((err, labels) => { - assert.ifError(err); - assert.deepStrictEqual(labels, LABELS); - done(); - }); - }); + it('should set labels', async () => { + try { + await bucket.setLabels(LABELS); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, LABELS); + } catch (e) { + assert.ifError(e); + } }); - it('should update labels', done => { + it('should update labels', async () => { const newLabels = { siblinglabel: 'labelvalue', }; - bucket.setLabels(LABELS, err => { - assert.ifError(err); - - bucket.setLabels(newLabels, err => { - assert.ifError(err); - - bucket.getLabels((err, labels) => { - assert.ifError(err); - assert.deepStrictEqual( - labels, - Object.assign({}, LABELS, newLabels) - ); - done(); - }); - }); - }); + try { + await bucket.setLabels(LABELS); + await bucket.setLabels(newLabels); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, Object.assign({}, LABELS, newLabels)); + } catch (e) { + assert.ifError(e); + } }); - it('should delete a single label', done => { + it('should delete a single label', async () => { if (Object.keys(LABELS).length <= 1) { - done(new Error('Maintainer Error: `LABELS` needs 2 labels.')); - return; + throw new Error('Maintainer Error: `LABELS` needs 2 labels.'); } const labelKeyToDelete = Object.keys(LABELS)[0]; - bucket.setLabels(LABELS, err => { - assert.ifError(err); - - bucket.deleteLabels(labelKeyToDelete, err => { - assert.ifError(err); - - bucket.getLabels((err, labels) => { - assert.ifError(err); - - const expectedLabels = Object.assign({}, LABELS); - delete (expectedLabels as {[index: string]: {}})[ - labelKeyToDelete - ]; - - assert.deepStrictEqual(labels, expectedLabels); + try { + await bucket.setLabels(LABELS); + await bucket.deleteLabels(labelKeyToDelete); + const [labels] = await bucket.getLabels(); + const expectedLabels = Object.assign({}, LABELS); + delete (expectedLabels as {[index: string]: {}})[labelKeyToDelete]; - done(); - }); - }); - }); + assert.deepStrictEqual(labels, expectedLabels); + } catch (e) { + assert.ifError(e); + } }); - it('should delete all labels', done => { - bucket.deleteLabels(err => { - assert.ifError(err); - - bucket.getLabels((err, labels) => { - assert.ifError(err); - assert.deepStrictEqual(labels, {}); - done(); - }); - }); + it('should delete all labels', async () => { + try { + await bucket.deleteLabels(); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, {}); + } catch (e) { + assert.ifError(e); + } }); }); }); From f7e9f4a668580572e4711d01ba28028e1c2c9dae Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 19:13:37 +0000 Subject: [PATCH 08/13] next group of tests --- system-test/storage.ts | 98 ++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 66bd09795..0201c7b3c 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -1253,33 +1253,25 @@ describe('storage', () => { }); describe('bucket object lifecycle management', () => { - it('should add a rule', done => { - bucket.addLifecycleRule( - { - action: 'delete', - condition: { - age: 30, - isLive: true, - }, + it('should add a rule', async () => { + await bucket.addLifecycleRule({ + action: 'delete', + condition: { + age: 30, + isLive: true, }, - err => { - assert.ifError(err); - - const rules = [].slice.call(bucket.metadata.lifecycle.rule); - - assert.deepStrictEqual(rules.pop(), { - action: { - type: 'Delete', - }, - condition: { - age: 30, - isLive: true, - }, - }); + }); + const rules = [].slice.call(bucket.metadata.lifecycle.rule); - done(); - } - ); + assert.deepStrictEqual(rules.pop(), { + action: { + type: 'Delete', + }, + condition: { + age: 30, + isLive: true, + }, + }); }); it('should append a new rule', async () => { @@ -1346,31 +1338,26 @@ describe('storage', () => { ); }); - it('should convert a rule with createdBefore to a date in string', done => { - bucket.addLifecycleRule( - { + it('should convert a rule with createdBefore to a date in string', async () => { + try { + await bucket.addLifecycleRule({ action: 'delete', condition: { createdBefore: new Date('2018'), }, - }, - err => { - assert.ifError(err); - - const rules = [].slice.call(bucket.metadata.lifecycle.rule); - - assert.deepStrictEqual(rules.pop(), { - action: { - type: 'Delete', - }, - condition: { - createdBefore: '2018-01-01', - }, - }); - - done(); - } - ); + }); + const rules = [].slice.call(bucket.metadata.lifecycle.rule); + assert.deepStrictEqual(rules.pop(), { + action: { + type: 'Delete', + }, + condition: { + createdBefore: '2018-01-01', + }, + }); + } catch (e) { + assert.ifError(e); + } }); it('should add a noncurrent time rule', async () => { @@ -1417,17 +1404,16 @@ describe('storage', () => { ); }); - it('should remove all existing rules', done => { - bucket.setMetadata( - { + it('should remove all existing rules', async () => { + try { + await bucket.setMetadata({ lifecycle: null, - }, - (err: ApiError) => { - assert.ifError(err); - assert.strictEqual(bucket.metadata.lifecycle, undefined); - done(); - } - ); + }); + + assert.strictEqual(bucket.metadata.lifecycle, undefined); + } catch (e) { + assert.ifError(e); + } }); }); From 5c28422ad3630f5d833a80e8637a62db08a64824 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 19:20:24 +0000 Subject: [PATCH 09/13] more tests --- system-test/storage.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 0201c7b3c..571856086 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -1644,13 +1644,11 @@ describe('storage', () => { return deleteFilesAsync(); }); - it('should block an overwrite request', done => { + it('should block an overwrite request', async () => { createFile((err, file) => { assert.ifError(err); - - file!.save('new data', err => { - assert.strictEqual((err as ApiError).code, 403); - done(); + assert.rejects(file!.save('new data'), (err: ApiError) => { + assert.strictEqual(err.code, 403); }); }); }); @@ -1714,12 +1712,13 @@ describe('storage', () => { bucket.delete(done); }); - it('should have enabled requesterPays functionality', done => { - bucket.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); + it('should have enabled requesterPays functionality', async () => { + try { + const [metadata] = await bucket.getMetadata(); assert.strictEqual(metadata.billing.requesterPays, true); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); // These tests will verify that the requesterPays functionality works from From 5a8451a227bb61b74b9d4487a0c7e266d245ade9 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 21:05:03 +0000 Subject: [PATCH 10/13] more tests --- system-test/storage.ts | 187 +++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 101 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 571856086..8ceea7c0a 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -2293,72 +2293,65 @@ describe('storage', () => { assert.strictEqual(String(fileContents), String(remoteContents)); }); - it('should download a file to memory', done => { - const fileContents = fs.readFileSync(FILES.big.path); - bucket.upload(FILES.big.path, (err: Error | null, file?: File | null) => { - assert.ifError(err); - file!.download((err, remoteContents) => { - assert.ifError(err); - assert.strictEqual(String(fileContents), String(remoteContents)); - done(); - }); - }); + it('should download a file to memory', async () => { + try { + const fileContents = fs.readFileSync(FILES.big.path); + const [file] = await bucket.upload(FILES.big.path); + const [remoteContents] = await file.download(); + assert.strictEqual(String(fileContents), String(remoteContents)); + } catch (e) { + assert.ifError(e); + } }); - it('should download an empty file', done => { - const fileContents = fs.readFileSync(FILES.empty.path); - bucket.upload( - FILES.empty.path, - (err: Error | null, file?: File | null) => { - assert.ifError(err); - file!.download((err, remoteContents) => { - assert.ifError(err); - assert.strictEqual(String(fileContents), String(remoteContents)); - done(); - }); - } - ); + it('should download an empty file', async () => { + try { + const fileContents = fs.readFileSync(FILES.empty.path); + const [file] = await bucket.upload(FILES.empty.path); + const [remoteContents] = await file.download(); + assert.strictEqual(String(fileContents), String(remoteContents)); + } catch (e) { + assert.ifError(e); + } }); - it('should download the specified bytes of a file', done => { - const fileContents = fs.readFileSync(FILES.big.path); - bucket.upload(FILES.big.path, (err: Error | null, file?: File | null) => { - assert.ifError(err); - file!.download( - {start: FILE_DOWNLOAD_START_BYTE, end: FILE_DOWNLOAD_END_BYTE}, - (err, remoteContents) => { - assert.ifError(err); - assert.strictEqual( - String(fileContents).slice(0, 20), - String(remoteContents) - ); - done(); - } + it('should download the specified bytes of a file', async () => { + try { + const fileContents = fs.readFileSync(FILES.big.path); + const [file] = await bucket.upload(FILES.big.path); + const [remoteContents] = await file!.download({ + start: FILE_DOWNLOAD_START_BYTE, + end: FILE_DOWNLOAD_END_BYTE, + }); + assert.strictEqual( + String(fileContents).slice(0, 20), + String(remoteContents) ); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should handle non-network errors', done => { + it('should handle non-network errors', async () => { const file = bucket.file('hi.jpg'); - file.download(err => { + assert.rejects(file.download(), (err: ApiError) => { assert.strictEqual((err as ApiError).code, 404); - done(); }); }); - it('should gzip a file on the fly and download it', done => { + it('should gzip a file on the fly and download it', async () => { const options = { gzip: true, }; const expectedContents = fs.readFileSync(FILES.html.path, 'utf-8'); - bucket.upload(FILES.html.path, options, (err, file) => { - assert.ifError(err); - file!.download((err, contents) => { - assert.ifError(err); - assert.strictEqual(contents.toString(), expectedContents); - file!.delete(done); - }); - }); + try { + const [file] = await bucket.upload(FILES.html.path, options); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), expectedContents); + await file.delete(); + } catch (e) { + assert.ifError(e); + } }); it('should upload a gzipped file and download it', async () => { @@ -2447,22 +2440,22 @@ describe('storage', () => { }); }); - it('should write metadata', done => { + it('should write metadata', async () => { const options = { metadata: {contentType: 'image/png'}, resumable: false, }; - bucket.upload(FILES.logo.path, options, (err, file) => { - assert.ifError(err); - file!.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - assert.strictEqual( - metadata.contentType, - options.metadata.contentType - ); - file!.delete(done); - }); - }); + try { + const [file] = await bucket.upload(FILES.logo.path, options); + const [metadata] = await file.getMetadata(); + assert.strictEqual( + metadata.contentType, + options.metadata.contentType + ); + await file.delete(); + } catch (e) { + assert.ifError(e); + } }); it('should resume an upload after an interruption', done => { @@ -2557,31 +2550,26 @@ describe('storage', () => { file.save('secret data', {resumable: false}, done); }); - it('should not get the hashes from the unencrypted file', done => { - unencryptedFile.getMetadata( - (err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - assert.strictEqual(metadata.crc32c, undefined); - done(); - } - ); + it('should not get the hashes from the unencrypted file', async () => { + try { + const [metadata] = await unencryptedFile.getMetadata(); + assert.strictEqual(metadata.crc32c, undefined); + } catch (e) { + assert.ifError(e); + } }); - it('should get the hashes from the encrypted file', done => { - file.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); + it('should get the hashes from the encrypted file', async () => { + try { + const [metadata] = await file.getMetadata(); assert.notStrictEqual(metadata.crc32c, undefined); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should not download from the unencrypted file', done => { - unencryptedFile.download(err => { - if (!err) { - done(new Error('Expected an error.')); - return; - } - + it('should not download from the unencrypted file', async () => { + assert.rejects(unencryptedFile.download(), (err: ApiError) => { assert( err!.message.indexOf( [ @@ -2590,29 +2578,27 @@ describe('storage', () => { ].join(' ') ) > -1 ); - done(); }); }); - it('should download from the encrytped file', done => { - file.download((err, contents) => { - assert.ifError(err); + it('should download from the encrytped file', async () => { + try { + const [contents] = await file.download(); assert.strictEqual(contents.toString(), 'secret data'); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should rotate encryption keys', done => { + it('should rotate encryption keys', async () => { const newEncryptionKey = crypto.randomBytes(32); - - file.rotateEncryptionKey(newEncryptionKey, err => { - assert.ifError(err); - file.download((err, contents) => { - assert.ifError(err); - assert.strictEqual(contents.toString(), 'secret data'); - done(); - }); - }); + try { + await file.rotateEncryptionKey(newEncryptionKey); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), 'secret data'); + } catch (e) { + assert.ifError(e); + } }); }); @@ -3014,14 +3000,13 @@ describe('storage', () => { }); describe('channels', () => { - it('should stop a channel', done => { + it('should stop a channel', async () => { // We can't actually create a channel. But we can test to see that we're // reaching the right endpoint with the API request. const channel = storage.channel('id', 'resource-id'); - channel.stop(err => { + assert.rejects(channel.stop(), (err: ApiError) => { assert.strictEqual((err as ApiError).code, 404); assert.strictEqual(err!.message.indexOf("Channel 'id' not found"), 0); - done(); }); }); }); From 46ad30ebdb15d1dfe1bdbf17c1ed008b2bf7e672 Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Tue, 9 Aug 2022 21:24:30 +0000 Subject: [PATCH 11/13] rest of the tests --- system-test/storage.ts | 326 ++++++++++++++++++----------------------- 1 file changed, 143 insertions(+), 183 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 8ceea7c0a..bed860d66 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -2683,9 +2683,9 @@ describe('storage', () => { file.save(FILE_CONTENTS, {resumable: false}, done); }); - it('should have set kmsKeyName on created file', done => { - file.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); + it('should have set kmsKeyName on created file', async () => { + try { + const [metadata] = await file.getMetadata(); // Strip the project ID, as it could be the placeholder locally, but // the real value upstream. @@ -2700,37 +2700,33 @@ describe('storage', () => { expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should set kmsKeyName on resumable uploaded file', done => { - const file = bucket.file('resumable-file', {kmsKeyName}); - - file.save(FILE_CONTENTS, {resumable: true}, err => { - assert.ifError(err); - - file.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - - // Strip the project ID, as it could be the placeholder locally, - // but the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = metadata.kmsKeyName.replace( - projectIdRegExp, - '' - ); - let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); + it('should set kmsKeyName on resumable uploaded file', async () => { + try { + const file = bucket.file('resumable-file', {kmsKeyName}); + await file.save(FILE_CONTENTS, {resumable: true}); + const [metadata] = await file.getMetadata(); - // Upstream attaches a version. - expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + // Strip the project ID, as it could be the placeholder locally, + // but the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = metadata.kmsKeyName.replace( + projectIdRegExp, + '' + ); + let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); + // Upstream attaches a version. + expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; - done(); - }); - }); + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); + } catch (e) { + assert.ifError(e); + } }); it('should rotate encryption keys', async () => { @@ -2743,24 +2739,17 @@ describe('storage', () => { assert.strictEqual(contents.toString(), FILE_CONTENTS); }); - it('should convert CSEK to KMS key', done => { - const encryptionKey = crypto.randomBytes(32); - - const file = bucket.file('encrypted-file', {encryptionKey}); - - file.save(FILE_CONTENTS, {resumable: false}, err => { - assert.ifError(err); - - file.rotateEncryptionKey({kmsKeyName}, err => { - assert.ifError(err); - - file.download((err, contents) => { - assert.ifError(err); - assert.strictEqual(contents.toString(), 'secret data'); - done(); - }); - }); - }); + it('should convert CSEK to KMS key', async () => { + try { + const encryptionKey = crypto.randomBytes(32); + const file = bucket.file('encrypted-file', {encryptionKey}); + await file.save(FILE_CONTENTS, {resumable: false}); + await file.rotateEncryptionKey({kmsKeyName}); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), 'secret data'); + } catch (e) { + assert.ifError(e); + } }); }); @@ -2786,10 +2775,9 @@ describe('storage', () => { ); }); - it('should have set defaultKmsKeyName on created bucket', done => { - bucket.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - + it('should have set defaultKmsKeyName on created bucket', async () => { + try { + const [metadata] = await bucket.getMetadata(); // Strip the project ID, as it could be the placeholder locally, but // the real value upstream. const projectIdRegExp = /^.+\/locations/; @@ -2799,11 +2787,10 @@ describe('storage', () => { '' ); const expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - - done(); - }); + } catch (e) { + assert.ifError(e); + } }); it('should update the defaultKmsKeyName', async () => { @@ -2818,37 +2805,31 @@ describe('storage', () => { }); }); - it('should insert an object that inherits the kms key name', done => { - const file = bucket.file('kms-encrypted-file'); - - bucket.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - + it('should insert an object that inherits the kms key name', async () => { + try { + const file = bucket.file('kms-encrypted-file'); + const [metadata] = await bucket.getMetadata(); const defaultKmsKeyName = metadata.encryption.defaultKmsKeyName; + await file.save(FILE_CONTENTS, {resumable: false}); - file.save(FILE_CONTENTS, {resumable: false}, err => { - assert.ifError(err); - - // Strip the project ID, as it could be the placeholder locally, - // but the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = file.metadata.kmsKeyName.replace( - projectIdRegExp, - '' - ); - let expectedKmsKeyName = defaultKmsKeyName.replace( - projectIdRegExp, - '' - ); - - // Upstream attaches a version. - expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; - - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); + // Strip the project ID, as it could be the placeholder locally, + // but the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = file.metadata.kmsKeyName.replace( + projectIdRegExp, + '' + ); + let expectedKmsKeyName = defaultKmsKeyName.replace( + projectIdRegExp, + '' + ); - done(); - }); - }); + // Upstream attaches a version. + expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); + } catch (e) { + assert.ifError(e); + } }); }); }); @@ -2927,32 +2908,21 @@ describe('storage', () => { await file.delete(); }); - it('should copy to another bucket given a gs:// URL', done => { - const opts = {destination: 'CloudLogo'}; - bucket.upload(FILES.logo.path, opts, (err, file) => { - assert.ifError(err); - + it('should copy to another bucket given a gs:// URL', async () => { + try { + const opts = {destination: 'CloudLogo'}; + const [file] = await bucket.upload(FILES.logo.path, opts); const otherBucket = storage.bucket(generateName()); - otherBucket.create((err: Error) => { - assert.ifError(err); - - const destPath = 'gs://' + otherBucket.name + '/CloudLogoCopy'; - file!.copy(destPath, err => { - assert.ifError(err); - - otherBucket.getFiles((err, files) => { - assert.ifError(err); - - assert.strictEqual(files!.length, 1); - const newFile = files![0]; - - assert.strictEqual(newFile.name, 'CloudLogoCopy'); - - done(); - }); - }); - }); - }); + await otherBucket.create(); + const destPath = 'gs://' + otherBucket.name + '/CloudLogoCopy'; + await file!.copy(destPath); + const [files] = await otherBucket.getFiles(); + assert.strictEqual(files!.length, 1); + const newFile = files![0]; + assert.strictEqual(newFile.name, 'CloudLogoCopy'); + } catch (e) { + assert.ifError(e); + } }); it('should allow changing the storage class', async () => { @@ -3219,12 +3189,13 @@ describe('storage', () => { await Promise.all(NEW_FILES.map(file => deleteFileAsync(file))); }); - it('should get files', done => { - bucket.getFiles((err, files) => { - assert.ifError(err); + it('should get files', async () => { + try { + const [files] = await bucket.getFiles(); assert.strictEqual(files!.length, NEW_FILES.length); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); it('should get files as a stream', done => { @@ -3242,21 +3213,22 @@ describe('storage', () => { }); }); - it('should paginate the list', done => { - const query = { - maxResults: NEW_FILES.length - 1, - }; + it('should paginate the list', async () => { + try { + const query = { + maxResults: NEW_FILES.length - 1, + }; + + const [files, nextQuery] = await bucket.getFiles(query); - bucket.getFiles(query, (err, files, nextQuery) => { - assert.ifError(err); assert.strictEqual(files!.length, NEW_FILES.length - 1); assert(nextQuery); - bucket.getFiles(nextQuery!, (err, files) => { - assert.ifError(err); - assert.strictEqual(files!.length, 1); - done(); - }); - }); + const [nextFiles] = await bucket.getFiles(nextQuery); + + assert.strictEqual(nextFiles!.length, 1); + } catch (e) { + assert.ifError(e); + } }); }); @@ -3332,37 +3304,21 @@ describe('storage', () => { ); }); - it('should overwrite file, then get older version', done => { - const versionedFile = bucketWithVersioning.file(generateName()); - - versionedFile.save('a', err => { - assert.ifError(err); - - versionedFile.getMetadata( - (err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); - - const initialGeneration = metadata.generation; - - versionedFile.save('b', err => { - assert.ifError(err); - - const firstGenFile = bucketWithVersioning.file( - versionedFile.name, - { - generation: initialGeneration, - } - ); - - firstGenFile.download((err, contents) => { - assert.ifError(err); - assert.strictEqual(contents.toString(), 'a'); - done(); - }); - }); - } - ); - }); + it('should overwrite file, then get older version', async () => { + try { + const versionedFile = bucketWithVersioning.file(generateName()); + await versionedFile.save('a'); + const [metadata] = await versionedFile.getMetadata(); + const initialGeneration = metadata.generation; + await versionedFile.save('b'); + const firstGenFile = bucketWithVersioning.file(versionedFile.name, { + generation: initialGeneration, + }); + const [contents] = await firstGenFile.download(); + assert.strictEqual(contents.toString(), 'a'); + } catch (e) { + assert.ifError(e); + } }); it('should get all files scoped to their version', async () => { @@ -3717,46 +3673,50 @@ describe('storage', () => { ); }); - it('should get an existing notification', done => { - notification.get(err => { - assert.ifError(err); + it('should get an existing notification', async () => { + try { + await notification.get(); assert(Object.keys(notification.metadata).length > 0); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should get a notifications metadata', done => { - notification.getMetadata((err: ApiError | null, metadata: Metadata) => { - assert.ifError(err); + it('should get a notifications metadata', async () => { + try { + const [metadata] = await notification.getMetadata(); assert(metadata !== null && typeof metadata === 'object'); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should tell us if a notification exists', done => { - notification.exists((err: ApiError | null, exists: boolean) => { - assert.ifError(err); + it('should tell us if a notification exists', async () => { + try { + const [exists] = await notification.exists(); assert(exists); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should tell us if a notification does not exist', done => { - const notification = bucket.notification('123'); - - notification.exists((err: ApiError | null, exists: boolean) => { - assert.ifError(err); + it('should tell us if a notification does not exist', async () => { + try { + const notification = bucket.notification('123'); + const [exists] = await notification.exists(); assert.strictEqual(exists, false); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); - it('should get a list of notifications', done => { - bucket.getNotifications((err, notifications) => { - assert.ifError(err); + it('should get a list of notifications', async () => { + try { + const [notifications] = await bucket.getNotifications(); assert.strictEqual(notifications!.length, 1); - done(); - }); + } catch (e) { + assert.ifError(e); + } }); it('should emit events to a subscription', done => { From 6d694798ddcbd7bc656b46d4d1934870173e331b Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Wed, 10 Aug 2022 00:02:30 +0000 Subject: [PATCH 12/13] removed try catch --- system-test/storage.ts | 870 +++++++++++++++-------------------------- 1 file changed, 326 insertions(+), 544 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index bed860d66..1fca644ae 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -221,94 +221,67 @@ describe('storage', () => { describe('acls', () => { describe('buckets', () => { it('should get access controls', async () => { - try { - const accessControls = await bucket.acl.get(); - assert(Array.isArray(accessControls)); - } catch (e) { - assert.ifError(e); - } + const accessControls = await bucket.acl.get(); + assert(Array.isArray(accessControls)); }); it('should add entity to default access controls', async () => { - try { - const [accessControl] = await bucket.acl.default.add({ - entity: USER_ACCOUNT, - role: storage.acl.OWNER_ROLE, - }); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [accessControl] = await bucket.acl.default.add({ + entity: USER_ACCOUNT, + role: storage.acl.OWNER_ROLE, + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - const [updatedAccessControl] = await bucket.acl.default.update({ - entity: USER_ACCOUNT, - role: storage.acl.READER_ROLE, - }); - assert.strictEqual( - updatedAccessControl.role, - storage.acl.READER_ROLE - ); - await bucket.acl.default.delete({entity: USER_ACCOUNT}); - } catch (e) { - assert.ifError(e); - } + const [updatedAccessControl] = await bucket.acl.default.update({ + entity: USER_ACCOUNT, + role: storage.acl.READER_ROLE, + }); + assert.strictEqual(updatedAccessControl.role, storage.acl.READER_ROLE); + await bucket.acl.default.delete({entity: USER_ACCOUNT}); }); it('should get default access controls', async () => { - try { - const accessControls = await bucket.acl.default.get(); - assert(Array.isArray(accessControls)); - } catch (e) { - assert.ifError(e); - } + const accessControls = await bucket.acl.default.get(); + assert(Array.isArray(accessControls)); }); it('should grant an account access', async () => { - try { - const [accessControl] = await bucket.acl.add({ - entity: USER_ACCOUNT, - role: storage.acl.OWNER_ROLE, - }); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - const opts = {entity: USER_ACCOUNT}; - const [accessControlGet] = await bucket.acl.get(opts); - assert.strictEqual( - (accessControlGet as AccessControlObject).role, - storage.acl.OWNER_ROLE - ); - await bucket.acl.delete(opts); - } catch (e) { - assert.ifError(e); - } + const [accessControl] = await bucket.acl.add({ + entity: USER_ACCOUNT, + role: storage.acl.OWNER_ROLE, + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const opts = {entity: USER_ACCOUNT}; + const [accessControlGet] = await bucket.acl.get(opts); + assert.strictEqual( + (accessControlGet as AccessControlObject).role, + storage.acl.OWNER_ROLE + ); + await bucket.acl.delete(opts); }); it('should update an account', async () => { - try { - const [accessControl] = await bucket.acl.add({ - entity: USER_ACCOUNT, - role: storage.acl.OWNER_ROLE, - }); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - const [updatedAcl] = await bucket.acl.update({ - entity: USER_ACCOUNT, - role: storage.acl.WRITER_ROLE, - }); - assert.strictEqual(updatedAcl!.role, storage.acl.WRITER_ROLE); - await bucket.acl.delete({entity: USER_ACCOUNT}); - } catch (e) { - assert.ifError(e); - } + const [accessControl] = await bucket.acl.add({ + entity: USER_ACCOUNT, + role: storage.acl.OWNER_ROLE, + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [updatedAcl] = await bucket.acl.update({ + entity: USER_ACCOUNT, + role: storage.acl.WRITER_ROLE, + }); + assert.strictEqual(updatedAcl!.role, storage.acl.WRITER_ROLE); + await bucket.acl.delete({entity: USER_ACCOUNT}); }); it('should make a bucket public', async () => { - try { - await bucket.makePublic(); - const [aclObject] = await bucket.acl.get({entity: 'allUsers'}); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - await bucket.acl.delete({entity: 'allUsers'}); - } catch (e) { - assert.ifError(e); - } + await bucket.makePublic(); + const [aclObject] = await bucket.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); + await bucket.acl.delete({entity: 'allUsers'}); }); it('should make files public', async () => { @@ -329,12 +302,8 @@ describe('storage', () => { }); it('should make a bucket private', async () => { - try { - await bucket.makePublic(); - await bucket.makePrivate(); - } catch (e) { - assert.ifError(e); - } + await bucket.makePublic(); + await bucket.makePrivate(); const validateMakeBucketPrivateRejects = (err: ApiError) => { assert.strictEqual(err.code, 404); assert.strictEqual((err as ApiError).errors![0].reason, 'notFound'); @@ -383,12 +352,8 @@ describe('storage', () => { }); it('should get access controls', async () => { - try { - const [accessControls] = await file.acl.get(); - assert(Array.isArray(accessControls)); - } catch (e) { - assert.ifError(e); - } + const [accessControls] = await file.acl.get(); + assert(Array.isArray(accessControls)); }); it('should not expose default api', () => { @@ -397,56 +362,41 @@ describe('storage', () => { }); it('should grant an account access', async () => { - try { - const [accessControl] = await file.acl.add({ - entity: USER_ACCOUNT, - role: storage.acl.OWNER_ROLE, - }); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - const [accessControlGet] = await file.acl.get({entity: USER_ACCOUNT}); - assert.strictEqual( - (accessControlGet as AccessControlObject).role, - storage.acl.OWNER_ROLE - ); - await file.acl.delete({entity: USER_ACCOUNT}); - } catch (e) { - assert.ifError(e); - } + const [accessControl] = await file.acl.add({ + entity: USER_ACCOUNT, + role: storage.acl.OWNER_ROLE, + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [accessControlGet] = await file.acl.get({entity: USER_ACCOUNT}); + assert.strictEqual( + (accessControlGet as AccessControlObject).role, + storage.acl.OWNER_ROLE + ); + await file.acl.delete({entity: USER_ACCOUNT}); }); it('should update an account', async () => { - try { - const [accessControl] = await file.acl.add({ - entity: USER_ACCOUNT, - role: storage.acl.OWNER_ROLE, - }); - assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); - const [accessControlUpdate] = await file.acl.update({ - entity: USER_ACCOUNT, - role: storage.acl.READER_ROLE, - }); - assert.strictEqual( - accessControlUpdate!.role, - storage.acl.READER_ROLE - ); - await file.acl.delete({entity: USER_ACCOUNT}); - } catch (e) { - assert.ifError(e); - } + const [accessControl] = await file.acl.add({ + entity: USER_ACCOUNT, + role: storage.acl.OWNER_ROLE, + }); + assert.strictEqual(accessControl!.role, storage.acl.OWNER_ROLE); + const [accessControlUpdate] = await file.acl.update({ + entity: USER_ACCOUNT, + role: storage.acl.READER_ROLE, + }); + assert.strictEqual(accessControlUpdate!.role, storage.acl.READER_ROLE); + await file.acl.delete({entity: USER_ACCOUNT}); }); it('should make a file public', async () => { - try { - await file.makePublic(); - const [aclObject] = await file.acl.get({entity: 'allUsers'}); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - await file.acl.delete({entity: 'allUsers'}); - } catch (e) { - assert.ifError(e); - } + await file.makePublic(); + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); + await file.acl.delete({entity: 'allUsers'}); }); it('should make a file private', async () => { @@ -465,69 +415,53 @@ describe('storage', () => { it('should set custom encryption during the upload', async () => { const key = '12345678901234567890123456789012'; - try { - const [file] = await bucket.upload(FILES.big.path, { - encryptionKey: key, - resumable: false, - }); - const [metadata] = await file.getMetadata(); - assert.strictEqual( - metadata.customerEncryption.encryptionAlgorithm, - 'AES256' - ); - } catch (e) { - assert.ifError(e); - } + const [file] = await bucket.upload(FILES.big.path, { + encryptionKey: key, + resumable: false, + }); + const [metadata] = await file.getMetadata(); + assert.strictEqual( + metadata.customerEncryption.encryptionAlgorithm, + 'AES256' + ); }); it('should set custom encryption in a resumable upload', async () => { const key = crypto.randomBytes(32); - try { - const [file] = await bucket.upload(FILES.big.path, { - encryptionKey: key, - resumable: true, - }); - const [metadata] = await file.getMetadata(); - assert.strictEqual( - metadata.customerEncryption.encryptionAlgorithm, - 'AES256' - ); - } catch (e) { - assert.ifError(e); - } + const [file] = await bucket.upload(FILES.big.path, { + encryptionKey: key, + resumable: true, + }); + const [metadata] = await file.getMetadata(); + assert.strictEqual( + metadata.customerEncryption.encryptionAlgorithm, + 'AES256' + ); }); it('should make a file public during the upload', async () => { - try { - const [file] = await bucket.upload(FILES.big.path, { - resumable: false, - public: true, - }); + const [file] = await bucket.upload(FILES.big.path, { + resumable: false, + public: true, + }); - const [aclObject] = await file.acl.get({entity: 'allUsers'}); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - } catch (e) { - assert.ifError(e); - } + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); }); it('should make a file public from a resumable upload', async () => { - try { - const [file] = await bucket.upload(FILES.big.path, { - resumable: true, - public: true, - }); - const [aclObject] = await file.acl.get({entity: 'allUsers'}); - assert.deepStrictEqual(aclObject, { - entity: 'allUsers', - role: 'READER', - }); - } catch (e) { - assert.ifError(e); - } + const [file] = await bucket.upload(FILES.big.path, { + resumable: true, + public: true, + }); + const [aclObject] = await file.acl.get({entity: 'allUsers'}); + assert.deepStrictEqual(aclObject, { + entity: 'allUsers', + role: 'READER', + }); }); it('should make a file private from a resumable upload', async () => { @@ -573,43 +507,35 @@ describe('storage', () => { }); it('should get a policy', async () => { - try { - const [policy] = await bucket.iam.getPolicy(); - assert.deepStrictEqual(policy!.bindings, [ - { - members: [ - 'projectEditor:' + PROJECT_ID, - 'projectOwner:' + PROJECT_ID, - ], - role: 'roles/storage.legacyBucketOwner', - }, - { - members: ['projectViewer:' + PROJECT_ID], - role: 'roles/storage.legacyBucketReader', - }, - ]); - } catch (e) { - assert.ifError(e); - } + const [policy] = await bucket.iam.getPolicy(); + assert.deepStrictEqual(policy!.bindings, [ + { + members: [ + 'projectEditor:' + PROJECT_ID, + 'projectOwner:' + PROJECT_ID, + ], + role: 'roles/storage.legacyBucketOwner', + }, + { + members: ['projectViewer:' + PROJECT_ID], + role: 'roles/storage.legacyBucketReader', + }, + ]); }); it('should set a policy', async () => { - try { - const [policy] = await bucket.iam.getPolicy(); - policy!.bindings.push({ - role: 'roles/storage.legacyBucketReader', - members: ['allUsers'], - }); - const [newPolicy] = await bucket.iam.setPolicy(policy); - const legacyBucketReaderBinding = newPolicy!.bindings.filter( - binding => { - return binding.role === 'roles/storage.legacyBucketReader'; - } - )[0]; - assert(legacyBucketReaderBinding.members.includes('allUsers')); - } catch (e) { - assert.ifError(e); - } + const [policy] = await bucket.iam.getPolicy(); + policy!.bindings.push({ + role: 'roles/storage.legacyBucketReader', + members: ['allUsers'], + }); + const [newPolicy] = await bucket.iam.setPolicy(policy); + const legacyBucketReaderBinding = newPolicy!.bindings.filter( + binding => { + return binding.role === 'roles/storage.legacyBucketReader'; + } + )[0]; + assert(legacyBucketReaderBinding.members.includes('allUsers')); }); it('should get-modify-set a conditional policy', async () => { @@ -652,17 +578,11 @@ describe('storage', () => { 'storage.buckets.get', 'storage.buckets.getIamPolicy', ]; - try { - const [permissions] = await bucket.iam.testPermissions( - testPermissions - ); - assert.deepStrictEqual(permissions, { - 'storage.buckets.get': true, - 'storage.buckets.getIamPolicy': true, - }); - } catch (e) { - assert.ifError(e); - } + const [permissions] = await bucket.iam.testPermissions(testPermissions); + assert.deepStrictEqual(permissions, { + 'storage.buckets.get': true, + 'storage.buckets.getIamPolicy': true, + }); }); }); }); @@ -1091,15 +1011,11 @@ describe('storage', () => { }); it('should get buckets', async () => { - try { - const [buckets] = await storage.getBuckets(); - const createdBuckets = buckets.filter(bucket => { - return bucketsToCreate.indexOf(bucket.name) > -1; - }); - assert.strictEqual(createdBuckets.length, bucketsToCreate.length); - } catch (e) { - assert.ifError(e); - } + const [buckets] = await storage.getBuckets(); + const createdBuckets = buckets.filter(bucket => { + return bucketsToCreate.indexOf(bucket.name) > -1; + }); + assert.strictEqual(createdBuckets.length, bucketsToCreate.length); }); it('should get buckets as a stream', done => { @@ -1126,13 +1042,8 @@ describe('storage', () => { notFoundPage: 'http://fakeuri/404.html', }, }; - - try { - const [meta] = await bucket.setMetadata(metadata); - assert.deepStrictEqual(meta.website, metadata.website); - } catch (e) { - assert.ifError(e); - } + const [meta] = await bucket.setMetadata(metadata); + assert.deepStrictEqual(meta.website, metadata.website); }); it('should allow changing the storage class', async () => { @@ -1196,28 +1107,19 @@ describe('storage', () => { }); it('should set labels', async () => { - try { - await bucket.setLabels(LABELS); - const [labels] = await bucket.getLabels(); - assert.deepStrictEqual(labels, LABELS); - } catch (e) { - assert.ifError(e); - } + await bucket.setLabels(LABELS); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, LABELS); }); it('should update labels', async () => { const newLabels = { siblinglabel: 'labelvalue', }; - - try { - await bucket.setLabels(LABELS); - await bucket.setLabels(newLabels); - const [labels] = await bucket.getLabels(); - assert.deepStrictEqual(labels, Object.assign({}, LABELS, newLabels)); - } catch (e) { - assert.ifError(e); - } + await bucket.setLabels(LABELS); + await bucket.setLabels(newLabels); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, Object.assign({}, LABELS, newLabels)); }); it('should delete a single label', async () => { @@ -1226,28 +1128,19 @@ describe('storage', () => { } const labelKeyToDelete = Object.keys(LABELS)[0]; + await bucket.setLabels(LABELS); + await bucket.deleteLabels(labelKeyToDelete); + const [labels] = await bucket.getLabels(); + const expectedLabels = Object.assign({}, LABELS); + delete (expectedLabels as {[index: string]: {}})[labelKeyToDelete]; - try { - await bucket.setLabels(LABELS); - await bucket.deleteLabels(labelKeyToDelete); - const [labels] = await bucket.getLabels(); - const expectedLabels = Object.assign({}, LABELS); - delete (expectedLabels as {[index: string]: {}})[labelKeyToDelete]; - - assert.deepStrictEqual(labels, expectedLabels); - } catch (e) { - assert.ifError(e); - } + assert.deepStrictEqual(labels, expectedLabels); }); it('should delete all labels', async () => { - try { - await bucket.deleteLabels(); - const [labels] = await bucket.getLabels(); - assert.deepStrictEqual(labels, {}); - } catch (e) { - assert.ifError(e); - } + await bucket.deleteLabels(); + const [labels] = await bucket.getLabels(); + assert.deepStrictEqual(labels, {}); }); }); }); @@ -1339,25 +1232,21 @@ describe('storage', () => { }); it('should convert a rule with createdBefore to a date in string', async () => { - try { - await bucket.addLifecycleRule({ - action: 'delete', - condition: { - createdBefore: new Date('2018'), - }, - }); - const rules = [].slice.call(bucket.metadata.lifecycle.rule); - assert.deepStrictEqual(rules.pop(), { - action: { - type: 'Delete', - }, - condition: { - createdBefore: '2018-01-01', - }, - }); - } catch (e) { - assert.ifError(e); - } + await bucket.addLifecycleRule({ + action: 'delete', + condition: { + createdBefore: new Date('2018'), + }, + }); + const rules = [].slice.call(bucket.metadata.lifecycle.rule); + assert.deepStrictEqual(rules.pop(), { + action: { + type: 'Delete', + }, + condition: { + createdBefore: '2018-01-01', + }, + }); }); it('should add a noncurrent time rule', async () => { @@ -1405,15 +1294,11 @@ describe('storage', () => { }); it('should remove all existing rules', async () => { - try { - await bucket.setMetadata({ - lifecycle: null, - }); + await bucket.setMetadata({ + lifecycle: null, + }); - assert.strictEqual(bucket.metadata.lifecycle, undefined); - } catch (e) { - assert.ifError(e); - } + assert.strictEqual(bucket.metadata.lifecycle, undefined); }); }); @@ -1713,12 +1598,8 @@ describe('storage', () => { }); it('should have enabled requesterPays functionality', async () => { - try { - const [metadata] = await bucket.getMetadata(); - assert.strictEqual(metadata.billing.requesterPays, true); - } catch (e) { - assert.ifError(e); - } + const [metadata] = await bucket.getMetadata(); + assert.strictEqual(metadata.billing.requesterPays, true); }); // These tests will verify that the requesterPays functionality works from @@ -2294,42 +2175,30 @@ describe('storage', () => { }); it('should download a file to memory', async () => { - try { - const fileContents = fs.readFileSync(FILES.big.path); - const [file] = await bucket.upload(FILES.big.path); - const [remoteContents] = await file.download(); - assert.strictEqual(String(fileContents), String(remoteContents)); - } catch (e) { - assert.ifError(e); - } + const fileContents = fs.readFileSync(FILES.big.path); + const [file] = await bucket.upload(FILES.big.path); + const [remoteContents] = await file.download(); + assert.strictEqual(String(fileContents), String(remoteContents)); }); it('should download an empty file', async () => { - try { - const fileContents = fs.readFileSync(FILES.empty.path); - const [file] = await bucket.upload(FILES.empty.path); - const [remoteContents] = await file.download(); - assert.strictEqual(String(fileContents), String(remoteContents)); - } catch (e) { - assert.ifError(e); - } + const fileContents = fs.readFileSync(FILES.empty.path); + const [file] = await bucket.upload(FILES.empty.path); + const [remoteContents] = await file.download(); + assert.strictEqual(String(fileContents), String(remoteContents)); }); it('should download the specified bytes of a file', async () => { - try { - const fileContents = fs.readFileSync(FILES.big.path); - const [file] = await bucket.upload(FILES.big.path); - const [remoteContents] = await file!.download({ - start: FILE_DOWNLOAD_START_BYTE, - end: FILE_DOWNLOAD_END_BYTE, - }); - assert.strictEqual( - String(fileContents).slice(0, 20), - String(remoteContents) - ); - } catch (e) { - assert.ifError(e); - } + const fileContents = fs.readFileSync(FILES.big.path); + const [file] = await bucket.upload(FILES.big.path); + const [remoteContents] = await file!.download({ + start: FILE_DOWNLOAD_START_BYTE, + end: FILE_DOWNLOAD_END_BYTE, + }); + assert.strictEqual( + String(fileContents).slice(0, 20), + String(remoteContents) + ); }); it('should handle non-network errors', async () => { @@ -2344,14 +2213,10 @@ describe('storage', () => { gzip: true, }; const expectedContents = fs.readFileSync(FILES.html.path, 'utf-8'); - try { - const [file] = await bucket.upload(FILES.html.path, options); - const [contents] = await file.download(); - assert.strictEqual(contents.toString(), expectedContents); - await file.delete(); - } catch (e) { - assert.ifError(e); - } + const [file] = await bucket.upload(FILES.html.path, options); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), expectedContents); + await file.delete(); }); it('should upload a gzipped file and download it', async () => { @@ -2445,17 +2310,10 @@ describe('storage', () => { metadata: {contentType: 'image/png'}, resumable: false, }; - try { - const [file] = await bucket.upload(FILES.logo.path, options); - const [metadata] = await file.getMetadata(); - assert.strictEqual( - metadata.contentType, - options.metadata.contentType - ); - await file.delete(); - } catch (e) { - assert.ifError(e); - } + const [file] = await bucket.upload(FILES.logo.path, options); + const [metadata] = await file.getMetadata(); + assert.strictEqual(metadata.contentType, options.metadata.contentType); + await file.delete(); }); it('should resume an upload after an interruption', done => { @@ -2551,21 +2409,13 @@ describe('storage', () => { }); it('should not get the hashes from the unencrypted file', async () => { - try { - const [metadata] = await unencryptedFile.getMetadata(); - assert.strictEqual(metadata.crc32c, undefined); - } catch (e) { - assert.ifError(e); - } + const [metadata] = await unencryptedFile.getMetadata(); + assert.strictEqual(metadata.crc32c, undefined); }); it('should get the hashes from the encrypted file', async () => { - try { - const [metadata] = await file.getMetadata(); - assert.notStrictEqual(metadata.crc32c, undefined); - } catch (e) { - assert.ifError(e); - } + const [metadata] = await file.getMetadata(); + assert.notStrictEqual(metadata.crc32c, undefined); }); it('should not download from the unencrypted file', async () => { @@ -2582,23 +2432,15 @@ describe('storage', () => { }); it('should download from the encrytped file', async () => { - try { - const [contents] = await file.download(); - assert.strictEqual(contents.toString(), 'secret data'); - } catch (e) { - assert.ifError(e); - } + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), 'secret data'); }); it('should rotate encryption keys', async () => { const newEncryptionKey = crypto.randomBytes(32); - try { - await file.rotateEncryptionKey(newEncryptionKey); - const [contents] = await file.download(); - assert.strictEqual(contents.toString(), 'secret data'); - } catch (e) { - assert.ifError(e); - } + await file.rotateEncryptionKey(newEncryptionKey); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), 'secret data'); }); }); @@ -2684,49 +2526,41 @@ describe('storage', () => { }); it('should have set kmsKeyName on created file', async () => { - try { - const [metadata] = await file.getMetadata(); - - // Strip the project ID, as it could be the placeholder locally, but - // the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = metadata.kmsKeyName.replace( - projectIdRegExp, - '' - ); - let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); + const [metadata] = await file.getMetadata(); - // Upstream attaches a version. - expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + // Strip the project ID, as it could be the placeholder locally, but + // the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = metadata.kmsKeyName.replace( + projectIdRegExp, + '' + ); + let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - } catch (e) { - assert.ifError(e); - } + // Upstream attaches a version. + expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); }); it('should set kmsKeyName on resumable uploaded file', async () => { - try { - const file = bucket.file('resumable-file', {kmsKeyName}); - await file.save(FILE_CONTENTS, {resumable: true}); - const [metadata] = await file.getMetadata(); - - // Strip the project ID, as it could be the placeholder locally, - // but the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = metadata.kmsKeyName.replace( - projectIdRegExp, - '' - ); - let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); + const file = bucket.file('resumable-file', {kmsKeyName}); + await file.save(FILE_CONTENTS, {resumable: true}); + const [metadata] = await file.getMetadata(); - // Upstream attaches a version. - expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + // Strip the project ID, as it could be the placeholder locally, + // but the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = metadata.kmsKeyName.replace( + projectIdRegExp, + '' + ); + let expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - } catch (e) { - assert.ifError(e); - } + // Upstream attaches a version. + expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); }); it('should rotate encryption keys', async () => { @@ -2740,16 +2574,12 @@ describe('storage', () => { }); it('should convert CSEK to KMS key', async () => { - try { - const encryptionKey = crypto.randomBytes(32); - const file = bucket.file('encrypted-file', {encryptionKey}); - await file.save(FILE_CONTENTS, {resumable: false}); - await file.rotateEncryptionKey({kmsKeyName}); - const [contents] = await file.download(); - assert.strictEqual(contents.toString(), 'secret data'); - } catch (e) { - assert.ifError(e); - } + const encryptionKey = crypto.randomBytes(32); + const file = bucket.file('encrypted-file', {encryptionKey}); + await file.save(FILE_CONTENTS, {resumable: false}); + await file.rotateEncryptionKey({kmsKeyName}); + const [contents] = await file.download(); + assert.strictEqual(contents.toString(), 'secret data'); }); }); @@ -2776,21 +2606,14 @@ describe('storage', () => { }); it('should have set defaultKmsKeyName on created bucket', async () => { - try { - const [metadata] = await bucket.getMetadata(); - // Strip the project ID, as it could be the placeholder locally, but - // the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = - metadata.encryption.defaultKmsKeyName.replace( - projectIdRegExp, - '' - ); - const expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - } catch (e) { - assert.ifError(e); - } + const [metadata] = await bucket.getMetadata(); + // Strip the project ID, as it could be the placeholder locally, but + // the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = + metadata.encryption.defaultKmsKeyName.replace(projectIdRegExp, ''); + const expectedKmsKeyName = kmsKeyName.replace(projectIdRegExp, ''); + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); }); it('should update the defaultKmsKeyName', async () => { @@ -2806,30 +2629,26 @@ describe('storage', () => { }); it('should insert an object that inherits the kms key name', async () => { - try { - const file = bucket.file('kms-encrypted-file'); - const [metadata] = await bucket.getMetadata(); - const defaultKmsKeyName = metadata.encryption.defaultKmsKeyName; - await file.save(FILE_CONTENTS, {resumable: false}); - - // Strip the project ID, as it could be the placeholder locally, - // but the real value upstream. - const projectIdRegExp = /^.+\/locations/; - const actualKmsKeyName = file.metadata.kmsKeyName.replace( - projectIdRegExp, - '' - ); - let expectedKmsKeyName = defaultKmsKeyName.replace( - projectIdRegExp, - '' - ); + const file = bucket.file('kms-encrypted-file'); + const [metadata] = await bucket.getMetadata(); + const defaultKmsKeyName = metadata.encryption.defaultKmsKeyName; + await file.save(FILE_CONTENTS, {resumable: false}); + + // Strip the project ID, as it could be the placeholder locally, + // but the real value upstream. + const projectIdRegExp = /^.+\/locations/; + const actualKmsKeyName = file.metadata.kmsKeyName.replace( + projectIdRegExp, + '' + ); + let expectedKmsKeyName = defaultKmsKeyName.replace( + projectIdRegExp, + '' + ); - // Upstream attaches a version. - expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; - assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); - } catch (e) { - assert.ifError(e); - } + // Upstream attaches a version. + expectedKmsKeyName = `${expectedKmsKeyName}/cryptoKeyVersions/1`; + assert.strictEqual(actualKmsKeyName, expectedKmsKeyName); }); }); }); @@ -2909,20 +2728,16 @@ describe('storage', () => { }); it('should copy to another bucket given a gs:// URL', async () => { - try { - const opts = {destination: 'CloudLogo'}; - const [file] = await bucket.upload(FILES.logo.path, opts); - const otherBucket = storage.bucket(generateName()); - await otherBucket.create(); - const destPath = 'gs://' + otherBucket.name + '/CloudLogoCopy'; - await file!.copy(destPath); - const [files] = await otherBucket.getFiles(); - assert.strictEqual(files!.length, 1); - const newFile = files![0]; - assert.strictEqual(newFile.name, 'CloudLogoCopy'); - } catch (e) { - assert.ifError(e); - } + const opts = {destination: 'CloudLogo'}; + const [file] = await bucket.upload(FILES.logo.path, opts); + const otherBucket = storage.bucket(generateName()); + await otherBucket.create(); + const destPath = 'gs://' + otherBucket.name + '/CloudLogoCopy'; + await file!.copy(destPath); + const [files] = await otherBucket.getFiles(); + assert.strictEqual(files!.length, 1); + const newFile = files![0]; + assert.strictEqual(newFile.name, 'CloudLogoCopy'); }); it('should allow changing the storage class', async () => { @@ -3190,12 +3005,8 @@ describe('storage', () => { }); it('should get files', async () => { - try { - const [files] = await bucket.getFiles(); - assert.strictEqual(files!.length, NEW_FILES.length); - } catch (e) { - assert.ifError(e); - } + const [files] = await bucket.getFiles(); + assert.strictEqual(files!.length, NEW_FILES.length); }); it('should get files as a stream', done => { @@ -3214,21 +3025,16 @@ describe('storage', () => { }); it('should paginate the list', async () => { - try { - const query = { - maxResults: NEW_FILES.length - 1, - }; - - const [files, nextQuery] = await bucket.getFiles(query); + const query = { + maxResults: NEW_FILES.length - 1, + }; - assert.strictEqual(files!.length, NEW_FILES.length - 1); - assert(nextQuery); - const [nextFiles] = await bucket.getFiles(nextQuery); + const [files, nextQuery] = await bucket.getFiles(query); - assert.strictEqual(nextFiles!.length, 1); - } catch (e) { - assert.ifError(e); - } + assert.strictEqual(files!.length, NEW_FILES.length - 1); + assert(nextQuery); + const [nextFiles] = await bucket.getFiles(nextQuery); + assert.strictEqual(nextFiles!.length, 1); }); }); @@ -3305,20 +3111,16 @@ describe('storage', () => { }); it('should overwrite file, then get older version', async () => { - try { - const versionedFile = bucketWithVersioning.file(generateName()); - await versionedFile.save('a'); - const [metadata] = await versionedFile.getMetadata(); - const initialGeneration = metadata.generation; - await versionedFile.save('b'); - const firstGenFile = bucketWithVersioning.file(versionedFile.name, { - generation: initialGeneration, - }); - const [contents] = await firstGenFile.download(); - assert.strictEqual(contents.toString(), 'a'); - } catch (e) { - assert.ifError(e); - } + const versionedFile = bucketWithVersioning.file(generateName()); + await versionedFile.save('a'); + const [metadata] = await versionedFile.getMetadata(); + const initialGeneration = metadata.generation; + await versionedFile.save('b'); + const firstGenFile = bucketWithVersioning.file(versionedFile.name, { + generation: initialGeneration, + }); + const [contents] = await firstGenFile.download(); + assert.strictEqual(contents.toString(), 'a'); }); it('should get all files scoped to their version', async () => { @@ -3674,49 +3476,29 @@ describe('storage', () => { }); it('should get an existing notification', async () => { - try { - await notification.get(); - assert(Object.keys(notification.metadata).length > 0); - } catch (e) { - assert.ifError(e); - } + await notification.get(); + assert(Object.keys(notification.metadata).length > 0); }); it('should get a notifications metadata', async () => { - try { - const [metadata] = await notification.getMetadata(); - assert(metadata !== null && typeof metadata === 'object'); - } catch (e) { - assert.ifError(e); - } + const [metadata] = await notification.getMetadata(); + assert(metadata !== null && typeof metadata === 'object'); }); it('should tell us if a notification exists', async () => { - try { - const [exists] = await notification.exists(); - assert(exists); - } catch (e) { - assert.ifError(e); - } + const [exists] = await notification.exists(); + assert(exists); }); it('should tell us if a notification does not exist', async () => { - try { - const notification = bucket.notification('123'); - const [exists] = await notification.exists(); - assert.strictEqual(exists, false); - } catch (e) { - assert.ifError(e); - } + const notification = bucket.notification('123'); + const [exists] = await notification.exists(); + assert.strictEqual(exists, false); }); it('should get a list of notifications', async () => { - try { - const [notifications] = await bucket.getNotifications(); - assert.strictEqual(notifications!.length, 1); - } catch (e) { - assert.ifError(e); - } + const [notifications] = await bucket.getNotifications(); + assert.strictEqual(notifications!.length, 1); }); it('should emit events to a subscription', done => { From f7c551fa140bf1bdf700381eca14c28f1df8c8ea Mon Sep 17 00:00:00 2001 From: Sameena Shaffeeullah Date: Wed, 10 Aug 2022 00:08:49 +0000 Subject: [PATCH 13/13] refactored additional test --- system-test/storage.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/system-test/storage.ts b/system-test/storage.ts index 1fca644ae..c45d8dfd3 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -1473,12 +1473,9 @@ describe('storage', () => { assert.strictEqual(FILE.metadata.temporaryHold, false); }); - it('should get an expiration date', done => { - FILE.getExpirationDate((err, expirationDate) => { - assert.ifError(err); - assert(expirationDate instanceof Date); - done(); - }); + it('should get an expiration date', async () => { + const [expirationDate] = await FILE.getExpirationDate(); + assert(expirationDate instanceof Date); }); }); @@ -2280,16 +2277,12 @@ describe('storage', () => { }); describe('simple write', () => { - it('should save arbitrary data', done => { + it('should save arbitrary data', async () => { const file = bucket.file('TestFile'); const data = 'hello'; - file!.save(data, err => { - assert.ifError(err); - file!.download((err, contents) => { - assert.strictEqual(contents.toString(), data); - done(); - }); - }); + await file!.save(data); + const [contents] = await file!.download(); + assert.strictEqual(contents.toString(), data); }); });