Skip to content

feat: Add support for FIREBASE_STORAGE_EMULATOR_HOST env var #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class Storage implements StorageInterface {
});
}

if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) {
process.env.STORAGE_EMULATOR_HOST = process.env.FIREBASE_STORAGE_EMULATOR_HOST;
}

let storage: typeof StorageClient;
try {
storage = require('@google-cloud/storage').Storage;
Expand Down
162 changes: 92 additions & 70 deletions test/unit/storage/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,88 +29,110 @@ describe('Storage', () => {
let mockCredentialApp: FirebaseApp;
let storage: Storage;

beforeEach(() => {
mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
storage = new Storage(mockApp);
});

afterEach(() => {
return mockApp.delete();
});

describe('Constructor', () => {
const invalidApps = [null, NaN, 0, 1, true, false, '', 'a', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidApps.forEach((invalidApp) => {
it(`should throw given invalid app: ${ JSON.stringify(invalidApp) }`, () => {
describe('Storage App', () => {
beforeEach(() => {
mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
storage = new Storage(mockApp);
});

afterEach(() => {
return mockApp.delete();
});

describe('Constructor', () => {
const invalidApps = [null, NaN, 0, 1, true, false, '', 'a', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidApps.forEach((invalidApp) => {
it(`should throw given invalid app: ${ JSON.stringify(invalidApp) }`, () => {
expect(() => {
const storageAny: any = Storage;
return new storageAny(invalidApp);
}).to.throw('First argument passed to admin.storage() must be a valid Firebase app instance.');
});
});

it('should throw given no app', () => {
expect(() => {
const storageAny: any = Storage;
return new storageAny(invalidApp);
return new storageAny();
}).to.throw('First argument passed to admin.storage() must be a valid Firebase app instance.');
});

it('should throw given invalid credential', () => {
const expectedError = 'Failed to initialize Google Cloud Storage client with the available ' +
'credential. Must initialize the SDK with a certificate credential or application default ' +
'credentials to use Cloud Storage API.';
expect(() => {
const storageAny: any = Storage;
return new storageAny(mockCredentialApp);
}).to.throw(expectedError);
});

it('should not throw given a valid app', () => {
expect(() => {
return new Storage(mockApp);
}).not.to.throw();
});
});

it('should throw given no app', () => {
expect(() => {
const storageAny: any = Storage;
return new storageAny();
}).to.throw('First argument passed to admin.storage() must be a valid Firebase app instance.');
});

it('should throw given invalid credential', () => {
const expectedError = 'Failed to initialize Google Cloud Storage client with the available ' +
'credential. Must initialize the SDK with a certificate credential or application default ' +
'credentials to use Cloud Storage API.';
expect(() => {
const storageAny: any = Storage;
return new storageAny(mockCredentialApp);
}).to.throw(expectedError);
});

it('should not throw given a valid app', () => {
expect(() => {
return new Storage(mockApp);
}).not.to.throw();

describe('app', () => {
it('returns the app from the constructor', () => {
// We expect referential equality here
expect(storage.app).to.equal(mockApp);
});

it('is read-only', () => {
expect(() => {
(storage as any).app = mockApp;
}).to.throw('Cannot set property app of #<Storage> which has only a getter');
});
});
});

describe('app', () => {
it('returns the app from the constructor', () => {
// We expect referential equality here
expect(storage.app).to.equal(mockApp);

describe('bucket(invalid)', () => {
const expectedError = 'Bucket name not specified or invalid. Specify a valid bucket name via ' +
'the storageBucket option when initializing the app, or specify the bucket name ' +
'explicitly when calling the getBucket() method.';
const invalidNames = [null, NaN, 0, 1, true, false, '', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidNames.forEach((invalidName) => {
it(`should throw given invalid bucket name: ${ JSON.stringify(invalidName) }`, () => {
expect(() => {
const bucketAny: any = storage.bucket;
bucketAny(invalidName);
}).to.throw(expectedError);
});
});
});

it('is read-only', () => {
expect(() => {
(storage as any).app = mockApp;
}).to.throw('Cannot set property app of #<Storage> which has only a getter');
describe('bucket()', () => {
it('should return a bucket object', () => {
expect(storage.bucket().name).to.equal('bucketName.appspot.com');
});
});
});

describe('bucket(invalid)', () => {
const expectedError = 'Bucket name not specified or invalid. Specify a valid bucket name via ' +
'the storageBucket option when initializing the app, or specify the bucket name ' +
'explicitly when calling the getBucket() method.';
const invalidNames = [null, NaN, 0, 1, true, false, '', [], [1, 'a'], {}, { a: 1 }, _.noop];
invalidNames.forEach((invalidName) => {
it(`should throw given invalid bucket name: ${ JSON.stringify(invalidName) }`, () => {
expect(() => {
const bucketAny: any = storage.bucket;
bucketAny(invalidName);
}).to.throw(expectedError);

describe('bucket(valid)', () => {
it('should return a bucket object', () => {
expect(storage.bucket('foo').name).to.equal('foo');
});
});
});

describe('bucket()', () => {
it('should return a bucket object', () => {
expect(storage.bucket().name).to.equal('bucketName.appspot.com');
});
});
describe('Emulator Support', () => {
it('sets STORAGE_EMULATOR_HOST if FIREBASE_STORAGE_EMULATOR_HOST is set', () => {
const EMULATOR_HOST = 'http://localhost:9199';
delete process.env.STORAGE_EMULATOR_HOST;
process.env.FIREBASE_STORAGE_EMULATOR_HOST = EMULATOR_HOST;

mockApp = mocks.app();
mockCredentialApp = mocks.mockCredentialApp();
storage = new Storage(mockApp);

describe('bucket(valid)', () => {
it('should return a bucket object', () => {
expect(storage.bucket('foo').name).to.equal('foo');
expect(process.env.STORAGE_EMULATOR_HOST).to.equal(EMULATOR_HOST);
});
});

afterEach(() => {
delete process.env.STORAGE_EMULATOR_HOST;
delete process.env.FIREBASE_STORAGE_EMULATOR_HOST;
return mockApp.delete();
});
})
});