@@ -34,6 +34,7 @@ import (
34
34
)
35
35
36
36
const (
37
+ credEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
37
38
testProjectID = "mock-project-id"
38
39
testVersion = "test-version"
39
40
)
@@ -82,7 +83,6 @@ func TestNewClientWithServiceAccountCredentials(t *testing.T) {
82
83
t .Fatal (err )
83
84
}
84
85
client , err := NewClient (context .Background (), & internal.AuthConfig {
85
- Creds : creds ,
86
86
Opts : optsWithServiceAcct ,
87
87
ProjectID : creds .ProjectID ,
88
88
Version : testVersion ,
@@ -176,7 +176,6 @@ func TestNewClientWithUserCredentials(t *testing.T) {
176
176
}` ),
177
177
}
178
178
conf := & internal.AuthConfig {
179
- Creds : creds ,
180
179
Opts : []option.ClientOption {option .WithCredentials (creds )},
181
180
Version : testVersion ,
182
181
}
@@ -206,7 +205,11 @@ func TestNewClientWithMalformedCredentials(t *testing.T) {
206
205
creds := & google.DefaultCredentials {
207
206
JSON : []byte ("not json" ),
208
207
}
209
- conf := & internal.AuthConfig {Creds : creds }
208
+ conf := & internal.AuthConfig {
209
+ Opts : []option.ClientOption {
210
+ option .WithCredentials (creds ),
211
+ },
212
+ }
210
213
if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
211
214
t .Errorf ("NewClient() = (%v,%v); want = (nil, error)" , c , err )
212
215
}
@@ -222,12 +225,61 @@ func TestNewClientWithInvalidPrivateKey(t *testing.T) {
222
225
t .Fatal (err )
223
226
}
224
227
creds := & google.DefaultCredentials {JSON : b }
225
- conf := & internal.AuthConfig {Creds : creds }
228
+ conf := & internal.AuthConfig {
229
+ Opts : []option.ClientOption {
230
+ option .WithCredentials (creds ),
231
+ },
232
+ }
226
233
if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
227
234
t .Errorf ("NewClient() = (%v,%v); want = (nil, error)" , c , err )
228
235
}
229
236
}
230
237
238
+ func TestNewClientAppDefaultCredentialsWithInvalidFile (t * testing.T ) {
239
+ current := os .Getenv (credEnvVar )
240
+
241
+ if err := os .Setenv (credEnvVar , "../testdata/non_existing.json" ); err != nil {
242
+ t .Fatal (err )
243
+ }
244
+ defer os .Setenv (credEnvVar , current )
245
+
246
+ conf := & internal.AuthConfig {}
247
+ if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
248
+ t .Errorf ("Auth() = (%v, %v); want (nil, error)" , c , err )
249
+ }
250
+ }
251
+
252
+ func TestNewClientInvalidCredentialFile (t * testing.T ) {
253
+ invalidFiles := []string {
254
+ "testdata" ,
255
+ "testdata/plain_text.txt" ,
256
+ }
257
+
258
+ ctx := context .Background ()
259
+ for _ , tc := range invalidFiles {
260
+ conf := & internal.AuthConfig {
261
+ Opts : []option.ClientOption {
262
+ option .WithCredentialsFile (tc ),
263
+ },
264
+ }
265
+ if c , err := NewClient (ctx , conf ); c != nil || err == nil {
266
+ t .Errorf ("Auth() = (%v, %v); want (nil, error)" , c , err )
267
+ }
268
+ }
269
+ }
270
+
271
+ func TestNewClientExplicitNoAuth (t * testing.T ) {
272
+ ctx := context .Background ()
273
+ conf := & internal.AuthConfig {
274
+ Opts : []option.ClientOption {
275
+ option .WithoutAuthentication (),
276
+ },
277
+ }
278
+ if c , err := NewClient (ctx , conf ); c == nil || err != nil {
279
+ t .Errorf ("Auth() = (%v, %v); want (auth, nil)" , c , err )
280
+ }
281
+ }
282
+
231
283
func TestCustomToken (t * testing.T ) {
232
284
client := & Client {
233
285
signer : testSigner ,
@@ -298,8 +350,7 @@ func TestCustomTokenError(t *testing.T) {
298
350
func TestCustomTokenInvalidCredential (t * testing.T ) {
299
351
ctx := context .Background ()
300
352
conf := & internal.AuthConfig {
301
- Creds : nil ,
302
- Opts : optsWithTokenSource ,
353
+ Opts : optsWithTokenSource ,
303
354
}
304
355
s , err := NewClient (ctx , conf )
305
356
if err != nil {
0 commit comments