forked from googleapis/google-api-go-client
/
creds_test.go
238 lines (208 loc) · 8.41 KB
/
creds_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2017 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package internal
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type dummyTokenSource struct {
oauth2.TokenSource
}
func TestTokenSource(t *testing.T) {
ctx := context.Background()
// Pass in a TokenSource, get it back.
ts := &dummyTokenSource{}
ds := &DialSettings{TokenSource: ts}
got, err := Creds(ctx, ds)
if err != nil {
t.Fatal(err)
}
want := &google.DefaultCredentials{TokenSource: ts}
if !cmp.Equal(got, want) {
t.Error("did not get the same TokenSource back")
}
// If both a file and TokenSource are passed, the file takes precedence
// (existing behavior).
// TODO(jba): make this an error?
ds = &DialSettings{
TokenSource: ts,
CredentialsFile: "testdata/service-account.json",
DefaultScopes: []string{"foo"},
}
got, err = Creds(ctx, ds)
if err != nil {
t.Fatal(err)
}
if cmp.Equal(got, want) {
t.Error("got the same TokenSource back, wanted one from the JSON file")
}
// TODO(jba): find a way to test the call to google.DefaultTokenSource.
}
func TestDefaultServiceAccount(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
DefaultScopes: []string{"foo"},
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
DefaultScopes: []string{"foo"},
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
func TestJWTWithAudience(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{CredentialsFile: "testdata/service-account.json", Audiences: []string{"foo"}}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Audiences: []string{"foo"}}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
func TestJWTWithScope(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
Scopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
Scopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
func TestJWTWithDefaultScopes(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
DefaultScopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
DefaultScopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
func TestJWTWithDefaultAudience(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
DefaultAudience: "foo",
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
DefaultAudience: "foo",
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
func TestOAuth(t *testing.T) {
ctx := context.Background()
// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{CredentialsFile: "testdata/service-account.json", Scopes: []string{"foo"}}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Scopes: []string{"foo"}}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}
const validServiceAccountJSON = `{
"type": "service_account",
"project_id": "dumba-504",
"private_key_id": "adsfsdd",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDczcmZh9zhcDjd\nc+a1bcNu06QA+PGwjAZygTy9ays8qTLi4J8zWSjxgb18ZgYnv1gwVenmSBRuj+kg\nG03NPOxGmPrC/fTnZVBZpQRwBccBrPnRMvEM94egVrHKvPgqb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uWFYuGUS4ZZGNB8O1rzLDyA3r3i6jLmaRG/09D0TM9joCr6HdtkBAoGBAIi6\n1p3nw/MeA1520uligiOMpAqIYTBr9e3QvWgeOwKRwjic09hN+T2SdAewTiP7Ov8l\n3dC3P4aWtQR6HzAnHQrJQkJhHNd3uKJjnpvC0iPsGfKl1ND5k5niu/hdZsZHarvq\n+lBqtzSP9yNStkv63dI3YliHoIIcijBdpp1u5qXBAoGBAOLrmvUKnx4NLEcauQ0e\naHndQ/6y4ie6knn1iJsJdYNJnYh9RKqDPTgpi8DbE2eb5JNkBQl0nSBMl74+MhVl\nMKBPVprkv7p3BdxoanpsncY14TUnzWIngkH21Rk0gqE3t/iJ7xnCTSv8qv3yYDj3\nL54zu6Y9GbjLgn6BtfhLHG4v\n-----END PRIVATE KEY-----\n",
"client_email": "dumba-504@appspot.gserviceaccount.com",
"client_id": "111",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dumba-504%40appspot.gserviceaccount.com"
}`
func TestQuotaProjectFromCreds(t *testing.T) {
ctx := context.Background()
cred, err := credentialsFromJSON(
ctx,
[]byte(validServiceAccountJSON),
&DialSettings{
Endpoint: "foo.googleapis.com",
DefaultScopes: []string{"foo"},
})
if err != nil {
t.Fatalf("got %v, wanted no error", err)
}
if want, got := "", QuotaProjectFromCreds(cred); want != got {
t.Errorf("QuotaProjectFromCreds(validServiceAccountJSON): want %q, got %q", want, got)
}
quotaProjectJSON := []byte(`
{
"type": "authorized_user",
"quota_project_id": "foobar"
}`)
cred, err = credentialsFromJSON(
ctx,
[]byte(quotaProjectJSON),
&DialSettings{
Endpoint: "foo.googleapis.com",
DefaultScopes: []string{"foo"},
})
if err != nil {
t.Fatalf("got %v, wanted no error", err)
}
if want, got := "foobar", QuotaProjectFromCreds(cred); want != got {
t.Errorf("QuotaProjectFromCreds(quotaProjectJSON): want %q, got %q", want, got)
}
}