diff --git a/src/bucket.ts b/src/bucket.ts index 2b41c7ec0..58caa4e8e 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -1455,7 +1455,7 @@ class Bucket extends ServiceObject { /** * @callback GetBucketMetadataCallback * @param {?Error} err Request error, if any. - * @param {object} files The bucket metadata. + * @param {object} metadata The bucket metadata. * @param {object} apiResponse The full API response. */ /** @@ -1590,6 +1590,9 @@ class Bucket extends ServiceObject { * Lock a previously-defined retention policy. This will prevent changes to * the policy. * + * @param {SetBucketMetadataCallback} [callback] Callback function. + * @returns {Promise} + * * @example * const storage = require('@google-cloud/storage')(); * const bucket = storage.bucket('albums'); @@ -1604,9 +1607,9 @@ class Bucket extends ServiceObject { * }); */ lock(callback) { - this.getMetadata((err, metadata) => { + this.getMetadata((err, metadata, apiResponse) => { if (err) { - callback(err); + callback(err, null, apiResponse); return; } diff --git a/src/file.ts b/src/file.ts index 7b6ef738f..0169406e0 100644 --- a/src/file.ts +++ b/src/file.ts @@ -1508,8 +1508,8 @@ class File extends ServiceObject { /** * @callback GetExpirationDateCallback * @param {?Error} err Request error, if any. - * @param {date} file A Date object representing the earliest time this file's - * retention policy will expire. + * @param {date} expirationDate A Date object representing the earliest time + * this file's retention policy will expire. */ /** * If this bucket has a retention policy defined, use this method to get a diff --git a/system-test/storage.js b/system-test/storage.js index c4bd36fd1..19ff4cb77 100644 --- a/system-test/storage.js +++ b/system-test/storage.js @@ -1020,13 +1020,13 @@ describe('storage', function() { }); it('should set and release an event-based hold', function() { - return FILE.hold() + return FILE.setMetadata({eventBasedHold: true}) .then(response => { const metadata = response[0]; assert.strictEqual(metadata.eventBasedHold, true); }) - .then(() => FILE.release()) + .then(() => FILE.setMetadata({eventBasedHold: false})) .then(() => FILE.getMetadata()) .then(response => { const metadata = response[0]; @@ -1036,13 +1036,13 @@ describe('storage', function() { }); it('should set and release a temporary hold', function() { - return FILE.hold({temporary: true}) + return FILE.setMetadata({temporaryHold: true}) .then(response => { const metadata = response[0]; assert.strictEqual(metadata.temporaryHold, true); }) - .then(() => FILE.release({temporary: true})) + .then(() => FILE.setMetadata({temporaryHold: false})) .then(() => FILE.getMetadata()) .then(response => { const metadata = response[0]; diff --git a/test/bucket.ts b/test/bucket.ts index 1564ef04f..6753d652c 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -1558,6 +1558,56 @@ describe('Bucket', () => { }); }); + describe('lock', () => { + it('should refresh metadata', done => { + bucket.getMetadata = () => { + done(); + }; + + bucket.lock(assert.ifError); + }); + + it('should return error from getMetadata', done => { + const error = new Error('Error.'); + const apiResponse = {} + + bucket.getMetadata = callback => { + callback(error, null, apiResponse); + }; + + bucket.lock((err, metadata, apiResponse_) => { + assert.strictEqual(err, error); + assert.strictEqual(metadata, null); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); + }); + + it('should make the correct request', done => { + const metadata = { + metageneration: 8, + }; + + bucket.getMetadata = callback => { + callback(null, metadata); + }; + + bucket.request = (reqOpts, callback) => { + assert.deepStrictEqual(reqOpts, { + method: 'POST', + uri: '/lockRetentionPolicy', + qs: { + ifMetagenerationMatch: metadata.metageneration, + }, + }); + + callback(); // done() + }; + + bucket.lock(done); + }); + }); + describe('makePrivate', () => { it('should set predefinedAcl & privatize files', done => { let didSetPredefinedAcl = false; @@ -1719,6 +1769,20 @@ describe('Bucket', () => { }); }); + describe('removeRetentionPeriod', () => { + it('should call setMetadata correctly', done => { + bucket.setMetadata = (metadata, callback) => { + assert.deepStrictEqual(metadata, { + retentionPolicy: null, + }); + + callback(); // done() + }; + + bucket.removeRetentionPeriod(done); + }); + }); + describe('request', () => { const USER_PROJECT = 'grape-spaceship-123'; @@ -1886,6 +1950,24 @@ describe('Bucket', () => { }); }); + describe('setRetentionPeriod', () => { + it('should call setMetadata correctly', done => { + const duration = 90000; + + bucket.setMetadata = (metadata, callback) => { + assert.deepStrictEqual(metadata, { + retentionPolicy: { + retentionPeriod: duration, + }, + }); + + callback(); // done() + }; + + bucket.setRetentionPeriod(duration, done); + }); + }); + describe('setStorageClass', () => { const STORAGE_CLASS = 'NEW_STORAGE_CLASS'; const OPTIONS = {}; diff --git a/test/file.ts b/test/file.ts index c9c7ad5d7..3754f0816 100644 --- a/test/file.ts +++ b/test/file.ts @@ -1966,6 +1966,66 @@ describe('File', () => { }); }); + describe('getExpirationDate', () => { + it('should refresh metadata', done => { + file.getMetadata = () => { + done(); + }; + + file.getExpirationDate(assert.ifError); + }); + + it('should return error from getMetadata', done => { + const error = new Error('Error.'); + const apiResponse = {}; + + file.getMetadata = callback => { + callback(error, null, apiResponse); + }; + + file.getExpirationDate((err, expirationDate, apiResponse_) => { + assert.strictEqual(err, error); + assert.strictEqual(expirationDate, null); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); + }); + + it('should return an error if there is no expiration time', done => { + const apiResponse = {}; + + file.getMetadata = callback => { + callback(null, {}, apiResponse); + }; + + file.getExpirationDate((err, expirationDate, apiResponse_) => { + assert.strictEqual(err.message, `An expiration time is not available.`); + assert.strictEqual(expirationDate, null); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); + }); + + it('should return the expiration time as a Date object', done => { + const expirationTime = new Date(); + + const apiResponse = { + retentionExpirationTime: expirationTime.toJSON(), + }; + + file.getMetadata = callback => { + callback(null, apiResponse, apiResponse); + }; + + file.getExpirationDate((err, expirationDate, apiResponse_) => { + assert.ifError(err); + assert.deepStrictEqual(expirationDate, expirationTime); + assert.strictEqual(apiResponse_, apiResponse); + done(); + }); + }); + }); + describe('getMetadata', () => { it('should make the correct request', done => { extend(file.parent, {