Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 6, 2018
1 parent 7796136 commit 06d0e89
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
/**
Expand Down Expand Up @@ -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<SetBucketMetadataResponse>}
*
* @example
* const storage = require('@google-cloud/storage')();
* const bucket = storage.bucket('albums');
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions system-test/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down
82 changes: 82 additions & 0 deletions test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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 = {};
Expand Down
60 changes: 60 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 06d0e89

Please sign in to comment.