Skip to content

Commit

Permalink
fix notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 committed Jul 15, 2024
1 parent 0cdab1f commit ccb10d9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 99 deletions.
8 changes: 4 additions & 4 deletions src/nodejs-common/service-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
const methodConfig =
(typeof this.methods.delete === 'object' && this.methods.delete) || {};

let url = `${this.baseUrl}/${this.name}`;
let url = `${this.baseUrl}/${this.id}`;
if (this.parent instanceof Bucket) {
url = `${this.parent.baseUrl}/${this.parent.name}/${url}`;
url = `${this.parent.baseUrl}/${this.parent.id}/${url}`;
}

this.storageTransport.makeRequest(
Expand Down Expand Up @@ -438,9 +438,9 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
this.methods.getMetadata) ||
{};

let url = `${this.baseUrl}/${this.name}`;
let url = `${this.baseUrl}/${this.id}`;
if (this.parent instanceof Bucket) {
url = `${this.parent.baseUrl}/${this.parent.name}/${url}`;
url = `${this.parent.baseUrl}/${this.parent.id}/${url}`;
}

this.storageTransport.makeRequest<K>(
Expand Down
2 changes: 1 addition & 1 deletion src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class Notification extends ServiceObject<Notification, NotificationMetadata> {
super({
storageTransport: bucket.storage.storageTransport,
parent: bucket,
baseUrl: '/notificationConfigs',
baseUrl: 'notificationConfigs',
id: id.toString(),
createMethod: bucket.createNotification.bind(bucket),
methods,
Expand Down
193 changes: 99 additions & 94 deletions test/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,85 @@
import assert from 'assert';
import {describe, it, before, beforeEach} from 'mocha';
import {Bucket, GaxiosError} from '../src/index.js';
import {Notification} from '../src/index.js';
import {Notification, Storage} from '../src/index.js';
import * as sinon from 'sinon';
import {StorageTransport} from '../src/storage-transport.js';

describe('Notification', () => {
let notification: Notification;
let BUCKET: Bucket;
let storageTransport: StorageTransport;
let storage: Storage;
let sandbox: sinon.SinonSandbox;
const ID = '123';

before(() => {
BUCKET = sinon.createStubInstance(Bucket);
sandbox = sinon.createSandbox();
storage = sandbox.createStubInstance(Storage);
BUCKET = sandbox.createStubInstance(Bucket);
storageTransport = sandbox.createStubInstance(StorageTransport);
BUCKET.baseUrl = '';
BUCKET.storage = storage;
BUCKET.id = 'test-bucket';
BUCKET.storage.storageTransport = storageTransport;
BUCKET.storageTransport = storageTransport;
});

beforeEach(() => {
notification = new Notification(BUCKET, ID);
});

afterEach(() => {
sandbox.restore();
});

describe('delete', () => {
it('should make the correct request', done => {
const options = {};

BUCKET.storageTransport.makeRequest = (reqOpts, callback) => {
assert.strictEqual(reqOpts.method, 'DELETE');
assert.strictEqual(reqOpts.url, 'notificationConfigs/123');
assert.deepStrictEqual(reqOpts.queryParameters, options);
callback!(null); // the done fn
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake((reqOpts, callback) => {
assert.strictEqual(reqOpts.method, 'DELETE');
assert.strictEqual(
reqOpts.url,
'/test-bucket/notificationConfigs/123'
);
assert.deepStrictEqual(reqOpts.queryParameters, options);
callback!(null); // the done fn
return Promise.resolve();
});

notification.delete(options, done);
});

it('should optionally accept options', done => {
BUCKET.storageTransport.makeRequest = (reqOpts, callback) => {
assert.deepStrictEqual(reqOpts.queryParameters, {});
callback!(null); // the done fn
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake((reqOpts, callback) => {
assert.deepStrictEqual(reqOpts.queryParameters, {});
callback!(null); // the done fn
return Promise.resolve();
});

notification.delete(done);
});

it('should optionally accept a callback', done => {
BUCKET.storageTransport.makeRequest = (_reqOpts, callback) => {
callback!(null); // the done fn
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake((_reqOpts, callback) => {
callback!(null); // the done fn
return Promise.resolve();
});

notification.delete(done);
});
});

describe('get', () => {
it('should get the metadata', done => {
sinon.stub(notification, 'getMetadata').callsFake(() => {
sandbox.stub(notification, 'getMetadata').callsFake(() => {
done();
});

Expand All @@ -78,43 +103,43 @@ describe('Notification', () => {
it('should accept an options object', done => {
const options = {};

sinon.stub(notification, 'getMetadata').callsFake(options_ => {
sandbox.stub(notification, 'getMetadata').callsFake(options_ => {
assert.deepStrictEqual(options_, options);
done();
});

notification.get(options, assert.ifError);
});

it('should execute callback with error & metadata', done => {
it('should execute callback with error', done => {
const error = new GaxiosError('Error.', {});
const metadata = {};

sinon.stub(notification, 'getMetadata').callsFake(callback => {
callback!(error, metadata);
});
notification.getMetadata = sandbox
.stub()
.callsFake((reqOpts, callback) => {
callback!(error, metadata);
});

notification.get((err: Error, instance: {}, metadata_: {}) => {
notification.get(err => {
assert.strictEqual(err, error);
assert.strictEqual(instance, null);
assert.strictEqual(metadata_, metadata);

done();
});
});

it('should execute callback with instance & metadata', done => {
it('should execute callback with instance', done => {
const metadata = {};

sinon.stub(notification, 'getMetadata').callsFake(callback => {
callback!(null, metadata);
});
notification.getMetadata = sandbox
.stub()
.callsFake((reqOpts, callback) => {
callback!(null, metadata);
});

notification.get((err: Error, instance: {}, metadata_: {}) => {
notification.get((err, instance) => {
assert.ifError(err);

assert.strictEqual(instance, notification);
assert.strictEqual(metadata_, metadata);

done();
});
Expand All @@ -132,7 +157,7 @@ describe('Notification', () => {
autoCreate: true,
};

sinon.stub(notification, 'getMetadata').callsFake(callback => {
sandbox.stub(notification, 'getMetadata').callsFake(callback => {
callback(ERROR, METADATA);
});
});
Expand All @@ -145,56 +170,29 @@ describe('Notification', () => {
}
);

sinon.stub(notification, 'get').callsFake(config_ => {
sandbox.stub(notification, 'get').callsFake(config_ => {
assert.deepStrictEqual(config_, config);
done();
});

notification.get(config);
});

it('should pass only a callback to create if no config', done => {
sinon.stub(notification, 'create').callsFake(callback => {
callback(null);
});

notification.get(AUTO_CREATE_CONFIG, done);
});

describe('error', () => {
it('should execute callback with error & API response', done => {
it('should execute callback with error', done => {
const error = new GaxiosError('Error.', {});
const apiResponse = {};
sinon.stub(notification, 'get').callsFake((config, callback) => {
assert.deepStrictEqual(config, {});
callback!(null); // done()
sandbox.stub(notification, 'get').callsFake((config, callback) => {
callback!(error); // done()
});
sinon.stub(notification, 'create').callsFake(callback => {
callback(error, null, apiResponse);
sandbox.stub(notification, 'create').callsFake(callback => {
callback(error);
});

notification.get(AUTO_CREATE_CONFIG, (err, instance, resp) => {
notification.get(AUTO_CREATE_CONFIG, err => {
assert.strictEqual(err, error);
assert.strictEqual(instance, null);
assert.strictEqual(resp, apiResponse);
done();
});
});

it('should refresh the metadata after a 409', done => {
const error = new GaxiosError('409', {});
error.status = 409;

sinon.stub(notification, 'get').callsFake((config, callback) => {
assert.deepStrictEqual(config, {});
callback(null); // done()
});
sinon.stub(notification, 'create').callsFake(callback => {
callback(error);
});

notification.get(AUTO_CREATE_CONFIG, done);
});
});
});
});
Expand All @@ -203,52 +201,59 @@ describe('Notification', () => {
it('should make the correct request', done => {
const options = {};

BUCKET.storageTransport.makeRequest = reqOpts => {
assert.strictEqual(reqOpts.url, 'notificationConfigs/123');
assert.deepStrictEqual(reqOpts.queryParameters, options);
done();
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake(reqOpts => {
assert.strictEqual(
reqOpts.url,
'/test-bucket/notificationConfigs/123'
);
assert.deepStrictEqual(reqOpts.queryParameters, options);
done();
return Promise.resolve();
});

notification.getMetadata(options, assert.ifError);
});

it('should optionally accept options', done => {
BUCKET.storageTransport.makeRequest = reqOpts => {
assert.deepStrictEqual(reqOpts.queryParameters, {});
done();
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake(reqOpts => {
assert.deepStrictEqual(reqOpts.queryParameters, {});
done();
return Promise.resolve();
});

notification.getMetadata(assert.ifError);
});

it('should return any errors to the callback', done => {
const error = new GaxiosError('err', {});
const response = {};

BUCKET.storageTransport.makeRequest = (_reqOpts, callback) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback!(error, response as any, response as any);
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake((_reqOpts, callback) => {
callback!(error);
return Promise.resolve();
});

notification.getMetadata((err: Error, metadata: {}, resp: {}) => {
notification.getMetadata((err: GaxiosError | null) => {
assert.strictEqual(err, error);
assert.strictEqual(metadata, response);
assert.strictEqual(resp, response);
done();
});
});

it('should set and return the metadata', done => {
const response = {};

BUCKET.storageTransport.makeRequest = (_reqOpts, callback) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback!(null, response as any, response as any);
return Promise.resolve();
};
BUCKET.storageTransport.makeRequest = sandbox
.stub()
.callsFake((_reqOpts, callback) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback!(null, response as any, response as any);
return Promise.resolve();
});

notification.getMetadata((err: Error, metadata: {}, resp: {}) => {
assert.ifError(err);
Expand Down

0 comments on commit ccb10d9

Please sign in to comment.