1616
1717import * as _ from 'lodash' ;
1818import { expect } from 'chai' ;
19+ import * as sinon from 'sinon' ;
1920
2021import * as mocks from '../../resources/mocks' ;
2122import {
22- addReadonlyGetter , getProjectId , findProjectId , toWebSafeBase64 , formatString , generateUpdateMask ,
23+ addReadonlyGetter , getExplicitProjectId , findProjectId ,
24+ toWebSafeBase64 , formatString , generateUpdateMask ,
2325} from '../../../src/utils/index' ;
2426import { isNonEmptyString } from '../../../src/utils/validator' ;
2527import { FirebaseApp , FirebaseAppOptions } from '../../../src/firebase-app' ;
28+ import { ComputeEngineCredential } from '../../../src/auth/credential' ;
29+ import { HttpClient } from '../../../src/utils/api-request' ;
30+ import * as utils from '../utils' ;
31+ import { FirebaseAppError } from '../../../src/utils/error' ;
2632
2733interface Obj {
2834 [ key : string ] : any ;
@@ -66,7 +72,7 @@ describe('toWebSafeBase64()', () => {
6672 } ) ;
6773} ) ;
6874
69- describe ( 'getProjectId ()' , ( ) => {
75+ describe ( 'getExplicitProjectId ()' , ( ) => {
7076 let googleCloudProject : string | undefined ;
7177 let gcloudProject : string | undefined ;
7278
@@ -95,37 +101,38 @@ describe('getProjectId()', () => {
95101 projectId : 'explicit-project-id' ,
96102 } ;
97103 const app : FirebaseApp = mocks . appWithOptions ( options ) ;
98- expect ( getProjectId ( app ) ) . to . equal ( options . projectId ) ;
104+ expect ( getExplicitProjectId ( app ) ) . to . equal ( options . projectId ) ;
99105 } ) ;
100106
101107 it ( 'should return the project ID from service account' , ( ) => {
102108 const app : FirebaseApp = mocks . app ( ) ;
103- expect ( getProjectId ( app ) ) . to . equal ( 'project_id' ) ;
109+ expect ( getExplicitProjectId ( app ) ) . to . equal ( 'project_id' ) ;
104110 } ) ;
105111
106112 it ( 'should return the project ID set in GOOGLE_CLOUD_PROJECT environment variable' , ( ) => {
107113 process . env . GOOGLE_CLOUD_PROJECT = 'env-var-project-id' ;
108114 const app : FirebaseApp = mocks . mockCredentialApp ( ) ;
109- expect ( getProjectId ( app ) ) . to . equal ( 'env-var-project-id' ) ;
115+ expect ( getExplicitProjectId ( app ) ) . to . equal ( 'env-var-project-id' ) ;
110116 } ) ;
111117
112118 it ( 'should return the project ID set in GCLOUD_PROJECT environment variable' , ( ) => {
113119 process . env . GCLOUD_PROJECT = 'env-var-project-id' ;
114120 const app : FirebaseApp = mocks . mockCredentialApp ( ) ;
115- expect ( getProjectId ( app ) ) . to . equal ( 'env-var-project-id' ) ;
121+ expect ( getExplicitProjectId ( app ) ) . to . equal ( 'env-var-project-id' ) ;
116122 } ) ;
117123
118124 it ( 'should return null when project ID is not set' , ( ) => {
119125 delete process . env . GOOGLE_CLOUD_PROJECT ;
120126 delete process . env . GCLOUD_PROJECT ;
121127 const app : FirebaseApp = mocks . mockCredentialApp ( ) ;
122- expect ( getProjectId ( app ) ) . to . be . null ;
128+ expect ( getExplicitProjectId ( app ) ) . to . be . null ;
123129 } ) ;
124130} ) ;
125131
126132describe ( 'findProjectId()' , ( ) => {
127133 let googleCloudProject : string | undefined ;
128134 let gcloudProject : string | undefined ;
135+ let httpStub : sinon . SinonStub ;
129136
130137 before ( ( ) => {
131138 googleCloudProject = process . env . GOOGLE_CLOUD_PROJECT ;
@@ -146,6 +153,16 @@ describe('findProjectId()', () => {
146153 }
147154 } ) ;
148155
156+ beforeEach ( ( ) => {
157+ delete process . env . GOOGLE_CLOUD_PROJECT ;
158+ delete process . env . GCLOUD_PROJECT ;
159+ httpStub = sinon . stub ( HttpClient . prototype , 'send' ) ;
160+ } ) ;
161+
162+ afterEach ( ( ) => {
163+ httpStub . restore ( ) ;
164+ } ) ;
165+
149166 it ( 'should return the explicitly specified project ID from app options' , ( ) => {
150167 const options : FirebaseAppOptions = {
151168 credential : new mocks . MockCredential ( ) ,
@@ -172,9 +189,26 @@ describe('findProjectId()', () => {
172189 return findProjectId ( app ) . should . eventually . equal ( 'env-var-project-id' ) ;
173190 } ) ;
174191
175- it ( 'should return null when project ID is not set' , ( ) => {
176- delete process . env . GOOGLE_CLOUD_PROJECT ;
177- delete process . env . GCLOUD_PROJECT ;
192+ it ( 'should return the project ID discovered from the metadata service' , ( ) => {
193+ const expectedProjectId = 'test-project-id' ;
194+ const response = utils . responseFrom ( expectedProjectId ) ;
195+ httpStub . resolves ( response ) ;
196+ const app : FirebaseApp = mocks . appWithOptions ( {
197+ credential : new ComputeEngineCredential ( ) ,
198+ } ) ;
199+ return findProjectId ( app ) . should . eventually . equal ( expectedProjectId ) ;
200+ } ) ;
201+
202+ it ( 'should reject when the metadata service is not available' , ( ) => {
203+ httpStub . rejects ( new FirebaseAppError ( 'network-error' , 'Failed to connect' ) ) ;
204+ const app : FirebaseApp = mocks . appWithOptions ( {
205+ credential : new ComputeEngineCredential ( ) ,
206+ } ) ;
207+ return findProjectId ( app ) . should . eventually
208+ . rejectedWith ( 'Failed to determine project ID: Failed to connect' ) ;
209+ } ) ;
210+
211+ it ( 'should return null when project ID is not set and discoverable' , ( ) => {
178212 const app : FirebaseApp = mocks . mockCredentialApp ( ) ;
179213 return findProjectId ( app ) . should . eventually . be . null ;
180214 } ) ;
0 commit comments