| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcp_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"gocloud.dev/gcp" |
| 22 |
"gocloud.dev/internal/testing/setup" |
| 23 |
"golang.org/x/oauth2/google" |
| 24 |
) |
| 25 |
|
| 26 |
func TestNewHTTPClient(t *testing.T) { |
| 27 |
transport := gcp.DefaultTransport() |
| 28 |
_, err := gcp.NewHTTPClient(transport, nil) |
| 29 |
if err == nil { |
| 30 |
t.Error("got nil want error") |
| 31 |
} |
| 32 |
creds, err := setup.FakeGCPCredentials(context.Background()) |
| 33 |
if err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
_, err = gcp.NewHTTPClient(transport, gcp.CredentialsTokenSource(creds)) |
| 37 |
if err != nil { |
| 38 |
t.Error(err) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestCredentialsTokenSource(t *testing.T) { |
| 43 |
ts := gcp.CredentialsTokenSource(nil) |
| 44 |
if ts != nil { |
| 45 |
t.Error("got non-nil TokenSource from nil creds, want nil") |
| 46 |
} |
| 47 |
creds, err := setup.FakeGCPCredentials(context.Background()) |
| 48 |
if err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
ts = gcp.CredentialsTokenSource(creds) |
| 52 |
if ts == nil { |
| 53 |
t.Error("got nil TokenSource from creds, want non-nil") |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestDefaultProjectID(t *testing.T) { |
| 58 |
_, err := gcp.DefaultProjectID(nil) |
| 59 |
if err == nil { |
| 60 |
t.Error("got nil error from nil creds, want error") |
| 61 |
} |
| 62 |
creds, err := setup.FakeGCPCredentials(context.Background()) |
| 63 |
if err != nil { |
| 64 |
t.Fatal(err) |
| 65 |
} |
| 66 |
_, err = gcp.DefaultProjectID(creds) |
| 67 |
if err != nil { |
| 68 |
t.Error(err) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestDefaultCredentialsWithParams(t *testing.T) { |
| 73 |
cleanup := setup.FakeGCPDefaultCredentials(t) |
| 74 |
defer cleanup() |
| 75 |
|
| 76 |
ctx := context.Background() |
| 77 |
|
| 78 |
// Test with empty params (should use default scope and default universe domain) |
| 79 |
creds, err := gcp.DefaultCredentialsWithParams(ctx, google.CredentialsParams{}) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("DefaultCredentialsWithParams with empty params failed: %v", err) |
| 82 |
} |
| 83 |
if creds == nil { |
| 84 |
t.Error("got nil credentials, want non-nil") |
| 85 |
} |
| 86 |
// Verify default universe domain (googleapis.com) |
| 87 |
gotDomain, err := creds.GetUniverseDomain() |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("GetUniverseDomain failed: %v", err) |
| 90 |
} |
| 91 |
if gotDomain != "googleapis.com" { |
| 92 |
t.Errorf("got default universe domain %q, want %q", gotDomain, "googleapis.com") |
| 93 |
} |
| 94 |
|
| 95 |
// Test with universe domain parameter. |
| 96 |
// Note: The fake "authorized_user" credentials may not support universe domain |
| 97 |
// overrides, but we can verify the function accepts the parameter without error. |
| 98 |
creds, err = gcp.DefaultCredentialsWithParams(ctx, google.CredentialsParams{ |
| 99 |
UniverseDomain: "example.com", |
| 100 |
}) |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("DefaultCredentialsWithParams with universe domain failed: %v", err) |
| 103 |
} |
| 104 |
if creds == nil { |
| 105 |
t.Error("got nil credentials, want non-nil") |
| 106 |
} |
| 107 |
// The universe domain may not be set for authorized_user credential type, |
| 108 |
// but the function should not error. |
| 109 |
_, err = creds.GetUniverseDomain() |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("GetUniverseDomain failed: %v", err) |
| 112 |
} |
| 113 |
|
| 114 |
// Test with custom scopes |
| 115 |
creds, err = gcp.DefaultCredentialsWithParams(ctx, google.CredentialsParams{ |
| 116 |
Scopes: []string{"https://www.googleapis.com/auth/devstorage.read_only"}, |
| 117 |
}) |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("DefaultCredentialsWithParams with custom scopes failed: %v", err) |
| 120 |
} |
| 121 |
if creds == nil { |
| 122 |
t.Error("got nil credentials, want non-nil") |
| 123 |
} |
| 124 |
} |