@@ -34,6 +34,7 @@ import (
3434)
3535
3636const (
37+ credEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
3738 testProjectID = "mock-project-id"
3839 testVersion = "test-version"
3940)
@@ -82,7 +83,6 @@ func TestNewClientWithServiceAccountCredentials(t *testing.T) {
8283 t .Fatal (err )
8384 }
8485 client , err := NewClient (context .Background (), & internal.AuthConfig {
85- Creds : creds ,
8686 Opts : optsWithServiceAcct ,
8787 ProjectID : creds .ProjectID ,
8888 Version : testVersion ,
@@ -176,7 +176,6 @@ func TestNewClientWithUserCredentials(t *testing.T) {
176176 }` ),
177177 }
178178 conf := & internal.AuthConfig {
179- Creds : creds ,
180179 Opts : []option.ClientOption {option .WithCredentials (creds )},
181180 Version : testVersion ,
182181 }
@@ -206,7 +205,11 @@ func TestNewClientWithMalformedCredentials(t *testing.T) {
206205 creds := & google.DefaultCredentials {
207206 JSON : []byte ("not json" ),
208207 }
209- conf := & internal.AuthConfig {Creds : creds }
208+ conf := & internal.AuthConfig {
209+ Opts : []option.ClientOption {
210+ option .WithCredentials (creds ),
211+ },
212+ }
210213 if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
211214 t .Errorf ("NewClient() = (%v,%v); want = (nil, error)" , c , err )
212215 }
@@ -222,12 +225,61 @@ func TestNewClientWithInvalidPrivateKey(t *testing.T) {
222225 t .Fatal (err )
223226 }
224227 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+ }
226233 if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
227234 t .Errorf ("NewClient() = (%v,%v); want = (nil, error)" , c , err )
228235 }
229236}
230237
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+
231283func TestCustomToken (t * testing.T ) {
232284 client := & Client {
233285 signer : testSigner ,
@@ -298,8 +350,7 @@ func TestCustomTokenError(t *testing.T) {
298350func TestCustomTokenInvalidCredential (t * testing.T ) {
299351 ctx := context .Background ()
300352 conf := & internal.AuthConfig {
301- Creds : nil ,
302- Opts : optsWithTokenSource ,
353+ Opts : optsWithTokenSource ,
303354 }
304355 s , err := NewClient (ctx , conf )
305356 if err != nil {
0 commit comments