11import { expect } from "chai" ;
22import nock from "../test/helpers/nock" ;
3+ import * as sinon from "sinon" ;
34import * as cloudbilling from "./cloudbilling" ;
45import { cloudbillingOrigin } from "../api" ;
56import { Setup } from "../init" ;
7+ import * as ensureApiEnabled from "../ensureApiEnabled" ;
68
79const PROJECT_ID = "test-project" ;
810
911describe ( "cloudbilling" , ( ) => {
12+ let ensureStub : sinon . SinonStub ;
13+
14+ beforeEach ( ( ) => {
15+ ensureStub = sinon . stub ( ensureApiEnabled , "ensure" ) . resolves ( ) ;
16+ } ) ;
17+
1018 afterEach ( ( ) => {
1119 nock . cleanAll ( ) ;
1220 cloudbilling . clearCache ( ) ;
21+ ensureStub . restore ( ) ;
1322 } ) ;
1423
1524 describe ( "checkBillingEnabled" , ( ) => {
1625 it ( "should resolve with true if billing is enabled" , async ( ) => {
1726 nock ( cloudbillingOrigin ( ) )
1827 . get ( `/v1/projects/${ PROJECT_ID } /billingInfo` )
28+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
1929 . reply ( 200 , { billingEnabled : true } ) ;
2030
2131 const result = await cloudbilling . checkBillingEnabled ( PROJECT_ID ) ;
@@ -27,6 +37,7 @@ describe("cloudbilling", () => {
2737 it ( "should resolve with false if billing is not enabled" , async ( ) => {
2838 nock ( cloudbillingOrigin ( ) )
2939 . get ( `/v1/projects/${ PROJECT_ID } /billingInfo` )
40+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
3041 . reply ( 200 , { billingEnabled : false } ) ;
3142
3243 const result = await cloudbilling . checkBillingEnabled ( PROJECT_ID ) ;
@@ -38,6 +49,7 @@ describe("cloudbilling", () => {
3849 it ( "should reject if the API call fails" , async ( ) => {
3950 nock ( cloudbillingOrigin ( ) )
4051 . get ( `/v1/projects/${ PROJECT_ID } /billingInfo` )
52+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
4153 . reply ( 404 , { error : { message : "Not Found" } } ) ;
4254
4355 await expect ( cloudbilling . checkBillingEnabled ( PROJECT_ID ) ) . to . be . rejectedWith ( "Not Found" ) ;
@@ -106,6 +118,7 @@ describe("cloudbilling", () => {
106118 } ;
107119 nock ( cloudbillingOrigin ( ) )
108120 . get ( `/v1/projects/${ PROJECT_ID } /billingInfo` )
121+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
109122 . reply ( 200 , { billingEnabled : true } ) ;
110123
111124 const result = await cloudbilling . isBillingEnabled ( setup ) ;
@@ -123,6 +136,7 @@ describe("cloudbilling", () => {
123136 . put ( `/v1/projects/${ PROJECT_ID } /billingInfo` , {
124137 billingAccountName : billingAccountName ,
125138 } )
139+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
126140 . reply ( 200 , { billingEnabled : true } ) ;
127141
128142 const result = await cloudbilling . setBillingAccount ( PROJECT_ID , billingAccountName ) ;
@@ -136,6 +150,7 @@ describe("cloudbilling", () => {
136150 . put ( `/v1/projects/${ PROJECT_ID } /billingInfo` , {
137151 billingAccountName : billingAccountName ,
138152 } )
153+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
139154 . reply ( 403 , { error : { message : "Permission Denied" } } ) ;
140155
141156 await expect (
@@ -195,5 +210,17 @@ describe("cloudbilling", () => {
195210 await expect ( cloudbilling . listBillingAccounts ( ) ) . to . be . rejectedWith ( "Not Found" ) ;
196211 expect ( nock . isDone ( ) ) . to . be . true ;
197212 } ) ;
213+
214+ it ( "should include x-goog-user-project header when projectId is provided" , async ( ) => {
215+ nock ( cloudbillingOrigin ( ) )
216+ . get ( "/v1/billingAccounts" )
217+ . matchHeader ( "x-goog-user-project" , PROJECT_ID )
218+ . reply ( 200 , { billingAccounts : [ billingAccount ] } ) ;
219+
220+ const result = await cloudbilling . listBillingAccounts ( PROJECT_ID ) ;
221+
222+ expect ( result ) . to . deep . equal ( [ billingAccount ] ) ;
223+ expect ( nock . isDone ( ) ) . to . be . true ;
224+ } ) ;
198225 } ) ;
199226} ) ;
0 commit comments