From 6546ce2c5951e3c097abcd7d9002e67bd72d86d7 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Mon, 25 Jan 2021 15:14:27 -0800 Subject: [PATCH] fix: Renaming service functions to getAuth and getInstanceId --- etc/firebase-admin.api.md | 10 +++- src/app/firebase-app.ts | 4 +- src/auth/auth-namespace.ts | 18 ++++--- src/auth/index.ts | 2 +- src/instance-id/index.ts | 18 ++++--- test/unit/auth/index.spec.ts | 75 +++++++++++++++++++++++++++++ test/unit/index.spec.ts | 1 + test/unit/instance-id/index.spec.ts | 14 +++--- 8 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 test/unit/auth/index.spec.ts diff --git a/etc/firebase-admin.api.md b/etc/firebase-admin.api.md index 0464f9c959..535bd8e883 100644 --- a/etc/firebase-admin.api.md +++ b/etc/firebase-admin.api.md @@ -84,7 +84,7 @@ export class Auth extends BaseAuth { } // @public -export function auth(app?: App): Auth; +export function auth(app?: App): auth.Auth; // @public (undocumented) export namespace auth { @@ -400,6 +400,12 @@ export function getApp(name?: string): App; // @public (undocumented) export function getApps(): App[]; +// @public (undocumented) +export function getAuth(app?: App): Auth; + +// @public (undocumented) +export function getInstanceId(app?: App): InstanceId; + // @public export interface GetUsersResult { notFound: UserIdentifier[]; @@ -427,7 +433,7 @@ export class InstanceId { } // @public -export function instanceId(app?: App): InstanceId; +export function instanceId(app?: App): instanceId.InstanceId; // @public (undocumented) export namespace instanceId { diff --git a/src/app/firebase-app.ts b/src/app/firebase-app.ts index 40177fe793..4545a65c67 100644 --- a/src/app/firebase-app.ts +++ b/src/app/firebase-app.ts @@ -277,7 +277,7 @@ export class FirebaseApp implements app.App { * @return The Auth service instance of this app. */ public auth(): Auth { - const fn = require('../auth/index').auth; + const fn = require('../auth/index').getAuth; return fn(this); } @@ -332,7 +332,7 @@ export class FirebaseApp implements app.App { * @return The InstanceId service instance of this app. */ public instanceId(): InstanceId { - const fn = require('../instance-id/index').instanceId; + const fn = require('../instance-id/index').getInstanceId; return fn(this); } diff --git a/src/auth/auth-namespace.ts b/src/auth/auth-namespace.ts index b4f8361f0f..d7830bd046 100644 --- a/src/auth/auth-namespace.ts +++ b/src/auth/auth-namespace.ts @@ -95,6 +95,15 @@ import { UserRecord as TUserRecord, } from './user-record'; +export function getAuth(app?: App): Auth { + if (typeof app === 'undefined') { + app = getApp(); + } + + const firebaseApp: FirebaseApp = app as FirebaseApp; + return firebaseApp.getOrInitService('auth', (app) => new Auth(app)); +} + /** * Gets the {@link auth.Auth `Auth`} service for the default app or a * given app. @@ -116,14 +125,7 @@ import { * ``` * */ -export function auth(app?: App): Auth { - if (typeof app === 'undefined') { - app = getApp(); - } - - const firebaseApp: FirebaseApp = app as FirebaseApp; - return firebaseApp.getOrInitService('auth', (app) => new Auth(app)); -} +export declare function auth(app?: App): auth.Auth; /* eslint-disable @typescript-eslint/no-namespace */ export namespace auth { diff --git a/src/auth/index.ts b/src/auth/index.ts index 91609e66f7..619abf366c 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -91,4 +91,4 @@ export { UserRecord, } from './user-record'; -export { auth } from './auth-namespace'; +export { getAuth, auth } from './auth-namespace'; diff --git a/src/instance-id/index.ts b/src/instance-id/index.ts index 53b12bffcc..6f40351e7c 100644 --- a/src/instance-id/index.ts +++ b/src/instance-id/index.ts @@ -20,6 +20,15 @@ import { InstanceId } from './instance-id'; export { InstanceId }; +export function getInstanceId(app?: App): InstanceId { + if (typeof app === 'undefined') { + app = getApp(); + } + + const firebaseApp: FirebaseApp = app as FirebaseApp; + return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app)); +} + /** * Gets the {@link instanceId.InstanceId `InstanceId`} service for the * default app or a given app. @@ -50,14 +59,7 @@ export { InstanceId }; * no app is provided or the `InstanceId` service associated with the * provided app. */ -export function instanceId(app?: App): InstanceId { - if (typeof app === 'undefined') { - app = getApp(); - } - - const firebaseApp: FirebaseApp = app as FirebaseApp; - return firebaseApp.getOrInitService('instanceId', (app) => new InstanceId(app)); -} +export declare function instanceId(app?: App): instanceId.InstanceId; import { InstanceId as TInstanceId } from './instance-id'; diff --git a/test/unit/auth/index.spec.ts b/test/unit/auth/index.spec.ts new file mode 100644 index 0000000000..7d98b351c9 --- /dev/null +++ b/test/unit/auth/index.spec.ts @@ -0,0 +1,75 @@ +/*! + * @license + * Copyright 2021 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +import * as chai from 'chai'; +import * as sinonChai from 'sinon-chai'; +import * as chaiAsPromised from 'chai-as-promised'; + +import * as mocks from '../../resources/mocks'; +import { App } from '../../../src/app/index'; +import { getAuth, Auth } from '../../../src/auth/index'; + +chai.should(); +chai.use(sinonChai); +chai.use(chaiAsPromised); + +const expect = chai.expect; + +describe('InstanceId', () => { + let mockApp: App; + let mockCredentialApp: App; + + const noProjectIdError = 'Failed to determine project ID for Auth. Initialize the SDK ' + + 'with service account credentials or set project ID as an app option. Alternatively set the ' + + 'GOOGLE_CLOUD_PROJECT environment variable.'; + + beforeEach(() => { + mockApp = mocks.app(); + mockCredentialApp = mocks.mockCredentialApp(); + }); + + describe('getAuth()', () => { + it('should throw when default app is not available', () => { + expect(() => { + return getAuth(); + }).to.throw('The default Firebase app does not exist.'); + }); + + it('should reject given an invalid credential without project ID', () => { + // Project ID not set in the environment. + delete process.env.GOOGLE_CLOUD_PROJECT; + delete process.env.GCLOUD_PROJECT; + const auth = getAuth(mockCredentialApp); + return auth.getUser('uid') + .should.eventually.rejectedWith(noProjectIdError); + }); + + it('should not throw given a valid app', () => { + expect(() => { + return getAuth(mockApp); + }).not.to.throw(); + }); + + it('should return the same instance for a given app instance', () => { + const auth1: Auth = getAuth(mockApp); + const auth2: Auth = getAuth(mockApp); + expect(auth1).to.equal(auth2); + }); + }); +}); diff --git a/test/unit/index.spec.ts b/test/unit/index.spec.ts index 3730bdb936..316a41899c 100644 --- a/test/unit/index.spec.ts +++ b/test/unit/index.spec.ts @@ -30,6 +30,7 @@ import './utils/api-request.spec'; // Auth import './auth/auth.spec'; +import './auth/index.spec'; import './auth/user-record.spec'; import './auth/token-generator.spec'; import './auth/token-verifier.spec'; diff --git a/test/unit/instance-id/index.spec.ts b/test/unit/instance-id/index.spec.ts index 125d0ca6fa..b5a96eb39e 100644 --- a/test/unit/instance-id/index.spec.ts +++ b/test/unit/instance-id/index.spec.ts @@ -23,7 +23,7 @@ import * as chaiAsPromised from 'chai-as-promised'; import * as mocks from '../../resources/mocks'; import { App } from '../../../src/app/index'; -import { instanceId, InstanceId } from '../../../src/instance-id/index'; +import { getInstanceId, InstanceId } from '../../../src/instance-id/index'; chai.should(); chai.use(sinonChai); @@ -44,10 +44,10 @@ describe('InstanceId', () => { mockCredentialApp = mocks.mockCredentialApp(); }); - describe('instanceId()', () => { + describe('getInstanceId()', () => { it('should throw when default app is not available', () => { expect(() => { - return instanceId(); + return getInstanceId(); }).to.throw('The default Firebase app does not exist.'); }); @@ -55,20 +55,20 @@ describe('InstanceId', () => { // Project ID not set in the environment. delete process.env.GOOGLE_CLOUD_PROJECT; delete process.env.GCLOUD_PROJECT; - const iid = instanceId(mockCredentialApp); + const iid = getInstanceId(mockCredentialApp); return iid.deleteInstanceId('iid') .should.eventually.rejectedWith(noProjectIdError); }); it('should not throw given a valid app', () => { expect(() => { - return instanceId(mockApp); + return getInstanceId(mockApp); }).not.to.throw(); }); it('should return the same instance for a given app instance', () => { - const iid1: InstanceId = instanceId(mockApp); - const iid2: InstanceId = instanceId(mockApp); + const iid1: InstanceId = getInstanceId(mockApp); + const iid2: InstanceId = getInstanceId(mockApp); expect(iid1).to.equal(iid2); }); });