@@ -23,115 +23,149 @@ var logger = require('@google/cloud-diagnostics-common').logger;
2323var defaultConfig = require ( '../../src/config.js' ) . debug ;
2424var Debuglet = require ( '../../src/agent/debuglet.js' ) ;
2525
26+ var envProject = process . env . GCLOUD_PROJECT ;
27+
2628nock . disableNetConnect ( ) ;
27- process . env . GCLOUD_PROJECT = 0 ;
29+
30+ function accept ( ) {
31+ return true ;
32+ }
33+
34+ function nockOAuth2 ( validator ) {
35+ return nock ( 'https://accounts.google.com' )
36+ . post ( '/o/oauth2/token' , validator )
37+ . reply ( 200 , {
38+ refresh_token : 'hello' ,
39+ access_token : 'goodbye' ,
40+ expiry_date : new Date ( 9999 , 1 , 1 )
41+ } ) ;
42+ }
43+
44+ function nockRegister ( validator ) {
45+ return nock ( 'https://clouddebugger.googleapis.com' )
46+ . post ( '/v2/controller/debuggees/register' , validator )
47+ . reply ( 200 ) ;
48+ }
2849
2950describe ( 'test-config-credentials' , function ( ) {
3051 var debuglet = null ;
3152
3253 beforeEach ( function ( ) {
54+ delete process . env . GCLOUD_PROJECT ;
3355 assert . equal ( debuglet , null ) ;
3456 } ) ;
3557
3658 afterEach ( function ( ) {
3759 assert . ok ( debuglet ) ;
3860 debuglet . stop ( ) ;
3961 debuglet = null ;
62+ process . env . GCLOUD_PROJECT = envProject ;
4063 } ) ;
4164
65+ it ( 'should use config.projectId in preference to the environment variable' ,
66+ function ( done ) {
67+ process . env . GCLOUD_PROJECT = 'should-not-be-used' ;
68+
69+ var config = extend ( { } , defaultConfig , {
70+ projectId : 'project-via-config' ,
71+ credentials : require ( '../fixtures/gcloud-credentials.json' )
72+ } ) ;
73+ var debug = require ( '../..' ) ( config ) ;
74+
75+ // TODO: also make sure we don't request the project from metadata
76+ // service.
77+
78+ var scope = nockOAuth2 ( accept ) ;
79+ nockRegister ( function ( body ) {
80+ assert . ok ( body . debuggee ) ;
81+ assert . equal ( body . debuggee . project , 'project-via-config' ) ;
82+ scope . done ( ) ;
83+ setImmediate ( done ) ;
84+ return true ;
85+ } ) ;
86+
87+ debuglet =
88+ new Debuglet ( debug , config , logger . create ( logger . WARN , 'testing' ) ) ;
89+ debuglet . start ( ) ;
90+ } ) ;
4291
4392 it ( 'should use the keyFilename field of the config object' , function ( done ) {
93+ process . env . GCLOUD_PROJECT = '0' ;
4494 var credentials = require ( '../fixtures/gcloud-credentials.json' ) ;
4595 var config = extend ( { } , defaultConfig , {
4696 keyFilename : path . join ( 'test' , 'fixtures' , 'gcloud-credentials.json' )
4797 } ) ;
4898 var debug = require ( '../..' ) ( config ) ;
49- var scope = nock ( 'https://accounts.google.com' )
50- . post ( '/o/oauth2/token' , function ( body ) {
51- assert . equal ( body . client_id , credentials . client_id ) ;
52- assert . equal ( body . client_secret , credentials . client_secret ) ;
53- assert . equal ( body . refresh_token , credentials . refresh_token ) ;
54- return true ;
55- } ) . reply ( 200 , {
56- refresh_token : 'hello' ,
57- access_token : 'goodbye' ,
58- expiry_date : new Date ( 9999 , 1 , 1 )
59- } ) ;
60- // Since we have to get an auth token, this always gets intercepted second
61- nock ( 'https://clouddebugger.googleapis.com' )
62- . post ( '/v2/controller/debuggees/register' , function ( ) {
63- scope . done ( ) ;
64- setImmediate ( done ) ;
65- return true ;
66- } ) . reply ( 200 ) ;
67- debuglet = new Debuglet ( debug , config , logger . create ( logger . WARN , 'testing' ) ) ;
99+ var scope = nockOAuth2 ( function ( body ) {
100+ assert . equal ( body . client_id , credentials . client_id ) ;
101+ assert . equal ( body . client_secret , credentials . client_secret ) ;
102+ assert . equal ( body . refresh_token , credentials . refresh_token ) ;
103+ return true ;
104+ } ) ;
105+ // Since we have to get an auth token, this always gets intercepted second.
106+ nockRegister ( function ( ) {
107+ scope . done ( ) ;
108+ setImmediate ( done ) ;
109+ return true ;
110+ } ) ;
111+ debuglet =
112+ new Debuglet ( debug , config , logger . create ( logger . WARN , 'testing' ) ) ;
68113 debuglet . start ( ) ;
69114 } ) ;
70115
71116 it ( 'should use the credentials field of the config object' , function ( done ) {
72- var config = extend ( { } , defaultConfig , {
73- credentials : require ( '../fixtures/gcloud-credentials.json' )
74- } ) ;
117+ process . env . GCLOUD_PROJECT = '0' ;
118+ var config =
119+ extend ( { } , defaultConfig ,
120+ { credentials : require ( '../fixtures/gcloud-credentials.json' ) } ) ;
75121 var debug = require ( '../..' ) ( config ) ;
76- var scope = nock ( 'https://accounts.google.com' )
77- . post ( '/o/oauth2/token' , function ( body ) {
78- assert . equal ( body . client_id , config . credentials . client_id ) ;
79- assert . equal ( body . client_secret , config . credentials . client_secret ) ;
80- assert . equal ( body . refresh_token , config . credentials . refresh_token ) ;
81- return true ;
82- } ) . reply ( 200 , {
83- refresh_token : 'hello' ,
84- access_token : 'goodbye' ,
85- expiry_date : new Date ( 9999 , 1 , 1 )
86- } ) ;
87- // Since we have to get an auth token, this always gets intercepted second
88- nock ( 'https://clouddebugger.googleapis.com' )
89- . post ( '/v2/controller/debuggees/register' , function ( ) {
90- scope . done ( ) ;
91- setImmediate ( done ) ;
92- return true ;
93- } ) . reply ( 200 ) ;
122+ var scope = nockOAuth2 ( function ( body ) {
123+ assert . equal ( body . client_id , config . credentials . client_id ) ;
124+ assert . equal ( body . client_secret , config . credentials . client_secret ) ;
125+ assert . equal ( body . refresh_token , config . credentials . refresh_token ) ;
126+ return true ;
127+ } ) ;
128+ // Since we have to get an auth token, this always gets intercepted second.
129+ nockRegister ( function ( ) {
130+ scope . done ( ) ;
131+ setImmediate ( done ) ;
132+ return true ;
133+ } ) ;
94134 debuglet = new Debuglet ( debug , config , logger . create ( undefined , 'testing' ) ) ;
95135 debuglet . start ( ) ;
96136 } ) ;
97137
98138 it ( 'should ignore keyFilename if credentials is provided' , function ( done ) {
139+ process . env . GCLOUD_PROJECT = '0' ;
99140 var fileCredentials = require ( '../fixtures/gcloud-credentials.json' ) ;
100141 var credentials = {
101- client_id : 'a' ,
102- client_secret : 'b' ,
103- refresh_token : 'c' ,
104- type : 'authorized_user'
142+ client_id : 'a' ,
143+ client_secret : 'b' ,
144+ refresh_token : 'c' ,
145+ type : 'authorized_user'
105146 } ;
106147 var config = extend ( { } , defaultConfig , {
107148 keyFilename : path . join ( 'test' , 'fixtures' , 'gcloud-credentials.json' ) ,
108149 credentials : credentials
109150 } ) ;
110151 var debug = require ( '../..' ) ( config ) ;
111- [ 'client_id' , 'client_secret' , 'refresh_token' ] . forEach ( function ( field ) {
152+ var scope = nockOAuth2 ( function ( body ) {
153+ assert . equal ( body . client_id , credentials . client_id ) ;
154+ assert . equal ( body . client_secret , credentials . client_secret ) ;
155+ assert . equal ( body . refresh_token , credentials . refresh_token ) ;
156+ return true ;
157+ } ) ;
158+ // Since we have to get an auth token, this always gets intercepted second.
159+ nockRegister ( function ( ) {
160+ scope . done ( ) ;
161+ setImmediate ( done ) ;
162+ return true ;
163+ } ) ;
164+ [ 'client_id' , 'client_secret' , 'refresh_token' ] . forEach ( function ( field ) {
112165 assert ( fileCredentials . hasOwnProperty ( field ) ) ;
113166 assert ( config . credentials . hasOwnProperty ( field ) ) ;
114- assert . notEqual ( config . credentials [ field ] ,
115- fileCredentials [ field ] ) ;
167+ assert . notEqual ( config . credentials [ field ] , fileCredentials [ field ] ) ;
116168 } ) ;
117- var scope = nock ( 'https://accounts.google.com' )
118- . post ( '/o/oauth2/token' , function ( body ) {
119- assert . equal ( body . client_id , credentials . client_id ) ;
120- assert . equal ( body . client_secret , credentials . client_secret ) ;
121- assert . equal ( body . refresh_token , credentials . refresh_token ) ;
122- return true ;
123- } ) . reply ( 200 , {
124- refresh_token : 'hello' ,
125- access_token : 'goodbye' ,
126- expiry_date : new Date ( 9999 , 1 , 1 )
127- } ) ;
128- // Since we have to get an auth token, this always gets intercepted second
129- nock ( 'https://clouddebugger.googleapis.com' )
130- . post ( '/v2/controller/debuggees/register' , function ( ) {
131- scope . done ( ) ;
132- setImmediate ( done ) ;
133- return true ;
134- } ) . reply ( 200 ) ;
135169 debuglet = new Debuglet ( debug , config , logger . create ( undefined , 'testing' ) ) ;
136170 debuglet . start ( ) ;
137171 } ) ;
0 commit comments