@@ -129,6 +129,89 @@ describe('GoogleAuth', function() {
129129 } ) ;
130130 } ) ;
131131
132+ describe ( '.fromAPIKey' , function ( ) {
133+ var API_KEY = 'test-123' ;
134+ var STUB_PROJECT = 'my-awesome-project' ;
135+ describe ( 'Exception behaviour' , function ( ) {
136+ var auth ;
137+ before ( function ( ) {
138+ auth = new GoogleAuth ( ) ;
139+ } ) ;
140+ it ( 'Should error given an invalid api key' , function ( done ) {
141+ auth . fromAPIKey ( null , function ( err ) {
142+ assert ( err instanceof Error ) ;
143+ done ( ) ;
144+ } ) ;
145+ } ) ;
146+ } ) ;
147+ describe ( 'Request/response lifecycle mocking' , function ( ) {
148+ var ENDPOINT = '/events:report' ;
149+ var RESPONSE_BODY = 'RESPONSE_BODY' ;
150+ var BASE_URL = [
151+ 'https://clouderrorreporting.googleapis.com/v1beta1/projects' ,
152+ STUB_PROJECT
153+ ] . join ( '/' ) ;
154+ var auth ;
155+ beforeEach ( function ( ) {
156+ auth = new GoogleAuth ( ) ;
157+ insertEnvironmentVariableIntoAuth ( auth , 'GCLOUD_PROJECT' , STUB_PROJECT ) ;
158+ } ) ;
159+ afterEach ( function ( ) {
160+ nock . cleanAll ( ) ;
161+ } ) ;
162+ describe ( 'With no added query string parameters' , function ( ) {
163+ it ( 'should make a request with the api key' , function ( done ) {
164+ var fakeService = nock ( BASE_URL ) . post ( ENDPOINT )
165+ . query ( { key : API_KEY } ) . once ( ) . reply ( function ( uri ) {
166+ assert ( uri . indexOf ( 'key=' + API_KEY ) > - 1 ) ;
167+ return [ 200 , RESPONSE_BODY ] ;
168+ } ) ;
169+ auth . fromAPIKey ( API_KEY , function ( err , client ) {
170+ assert . strictEqual ( err , null ) ;
171+ client . request ( {
172+ url : BASE_URL + ENDPOINT ,
173+ method : 'POST' ,
174+ json : '{"test": true}'
175+ } , function ( err , body ) {
176+ assert . strictEqual ( err , null ) ;
177+ assert . strictEqual ( RESPONSE_BODY , body ) ;
178+ fakeService . done ( ) ;
179+ done ( ) ;
180+ } ) ;
181+ } ) ;
182+ } ) ;
183+ } ) ;
184+ describe ( 'With preexisting query string parameters' , function ( ) {
185+ it ( 'should make a request while preserving original parameters' ,
186+ function ( done ) {
187+ var OTHER_QS_PARAM = { test : 'abc' } ;
188+ var fakeService = nock ( BASE_URL ) . post ( ENDPOINT )
189+ . query ( { test : OTHER_QS_PARAM . test , key : API_KEY } ) . once ( )
190+ . reply ( function ( uri ) {
191+ assert ( uri . indexOf ( 'key=' + API_KEY ) > - 1 ) ;
192+ assert ( uri . indexOf ( 'test=' + OTHER_QS_PARAM . test ) > - 1 ) ;
193+ return [ 200 , RESPONSE_BODY ] ;
194+ } ) ;
195+ auth . fromAPIKey ( API_KEY , function ( err , client ) {
196+ assert . strictEqual ( err , null ) ;
197+ client . request ( {
198+ url : BASE_URL + ENDPOINT ,
199+ method : 'POST' ,
200+ json : '{"test": true}' ,
201+ qs : OTHER_QS_PARAM
202+ } , function ( err , body ) {
203+ assert . strictEqual ( err , null ) ;
204+ assert . strictEqual ( RESPONSE_BODY , body ) ;
205+ fakeService . done ( ) ;
206+ done ( ) ;
207+ } ) ;
208+ } ) ;
209+ }
210+ ) ;
211+ } ) ;
212+ } ) ;
213+ } ) ;
214+
132215 describe ( 'JWT token' , function ( ) {
133216
134217 it ( 'should error on empty json' , function ( done ) {
0 commit comments