@@ -6,6 +6,7 @@ package externalaccount
6
6
7
7
import (
8
8
"context"
9
+ "encoding/json"
9
10
"fmt"
10
11
"io/ioutil"
11
12
"net/http"
@@ -99,15 +100,18 @@ func run(t *testing.T, config *Config, tets *testExchangeTokenServer) (*oauth2.T
99
100
return ts .Token ()
100
101
}
101
102
102
- func validateToken (t * testing.T , tok * oauth2.Token ) {
103
- if got , want := tok .AccessToken , correctAT ; got != want {
103
+ func validateToken (t * testing.T , tok * oauth2.Token , expectToken * oauth2.Token ) {
104
+ if expectToken == nil {
105
+ return
106
+ }
107
+ if got , want := tok .AccessToken , expectToken .AccessToken ; got != want {
104
108
t .Errorf ("Unexpected access token: got %v, but wanted %v" , got , want )
105
109
}
106
- if got , want := tok .TokenType , "Bearer" ; got != want {
110
+ if got , want := tok .TokenType , expectToken . TokenType ; got != want {
107
111
t .Errorf ("Unexpected TokenType: got %v, but wanted %v" , got , want )
108
112
}
109
113
110
- if got , want := tok .Expiry , testNow (). Add ( time . Duration ( 3600 ) * time . Second ) ; got != want {
114
+ if got , want := tok .Expiry , expectToken . Expiry ; got != want {
111
115
t .Errorf ("Unexpected Expiry: got %v, but wanted %v" , got , want )
112
116
}
113
117
}
@@ -117,30 +121,94 @@ func getExpectedMetricsHeader(source string, saImpersonation bool, configLifetim
117
121
}
118
122
119
123
func TestToken (t * testing.T ) {
120
- config := Config {
121
- Audience : "32555940559.apps.googleusercontent.com" ,
122
- SubjectTokenType : "urn:ietf:params:oauth:token-type:id_token" ,
123
- ClientSecret : "notsosecret" ,
124
- ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
125
- CredentialSource : testBaseCredSource ,
126
- Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
124
+ type MockSTSResponse struct {
125
+ AccessToken string `json:"access_token"`
126
+ IssuedTokenType string `json:"issued_token_type"`
127
+ TokenType string `json:"token_type"`
128
+ ExpiresIn int32 `json:"expires_in,omitempty"`
129
+ Scope string `json:"scopre,omitenpty"`
127
130
}
128
131
129
- server := testExchangeTokenServer {
130
- url : "/" ,
131
- authorization : "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" ,
132
- contentType : "application/x-www-form-urlencoded" ,
133
- metricsHeader : getExpectedMetricsHeader ("file" , false , false ),
134
- body : baseCredsRequestBody ,
135
- response : baseCredsResponseBody ,
132
+ testCases := []struct {
133
+ name string
134
+ responseBody MockSTSResponse
135
+ expectToken * oauth2.Token
136
+ expectErrorMsg string
137
+ }{
138
+ {
139
+ name : "happy case" ,
140
+ responseBody : MockSTSResponse {
141
+ AccessToken : correctAT ,
142
+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
143
+ TokenType : "Bearer" ,
144
+ ExpiresIn : 3600 ,
145
+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
146
+ },
147
+ expectToken : & oauth2.Token {
148
+ AccessToken : correctAT ,
149
+ TokenType : "Bearer" ,
150
+ Expiry : testNow ().Add (time .Duration (3600 ) * time .Second ),
151
+ },
152
+ },
153
+ {
154
+ name : "happy case, non expire token" ,
155
+ responseBody : MockSTSResponse {
156
+ AccessToken : correctAT ,
157
+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
158
+ TokenType : "Bearer" ,
159
+ ExpiresIn : 0 ,
160
+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
161
+ },
162
+ expectToken : & oauth2.Token {
163
+ AccessToken : correctAT ,
164
+ TokenType : "Bearer" ,
165
+ Expiry : maxUnixTime ,
166
+ },
167
+ },
168
+ {
169
+ name : "negative expiry time" ,
170
+ responseBody : MockSTSResponse {
171
+ AccessToken : correctAT ,
172
+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
173
+ TokenType : "Bearer" ,
174
+ ExpiresIn : - 1 ,
175
+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
176
+ },
177
+ expectToken : nil ,
178
+ expectErrorMsg : "oauth2/google: got invalid expiry from security token service" ,
179
+ },
136
180
}
137
181
138
- tok , err := run (t , & config , & server )
182
+ for _ , testCase := range testCases {
183
+ config := Config {
184
+ Audience : "32555940559.apps.googleusercontent.com" ,
185
+ SubjectTokenType : "urn:ietf:params:oauth:token-type:id_token" ,
186
+ ClientSecret : "notsosecret" ,
187
+ ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
188
+ CredentialSource : testBaseCredSource ,
189
+ Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
190
+ }
139
191
140
- if err != nil {
141
- t .Fatalf ("Unexpected error: %e" , err )
192
+ responseBody , _ := json .Marshal (testCase .responseBody )
193
+
194
+ server := testExchangeTokenServer {
195
+ url : "/" ,
196
+ authorization : "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" ,
197
+ contentType : "application/x-www-form-urlencoded" ,
198
+ metricsHeader : getExpectedMetricsHeader ("file" , false , false ),
199
+ body : baseCredsRequestBody ,
200
+ response : string (responseBody ),
201
+ }
202
+
203
+ tok , err := run (t , & config , & server )
204
+
205
+ if err != nil {
206
+ if err .Error () != testCase .expectErrorMsg {
207
+ t .Errorf ("Error actual = %v, and Expect = %v" , err , testCase .expectErrorMsg )
208
+ }
209
+ }
210
+ validateToken (t , tok , testCase .expectToken )
142
211
}
143
- validateToken (t , tok )
144
212
}
145
213
146
214
func TestWorkforcePoolTokenWithClientID (t * testing.T ) {
@@ -168,7 +236,12 @@ func TestWorkforcePoolTokenWithClientID(t *testing.T) {
168
236
if err != nil {
169
237
t .Fatalf ("Unexpected error: %e" , err )
170
238
}
171
- validateToken (t , tok )
239
+ expectToken := oauth2.Token {
240
+ AccessToken : correctAT ,
241
+ TokenType : "Bearer" ,
242
+ Expiry : testNow ().Add (time .Duration (3600 ) * time .Second ),
243
+ }
244
+ validateToken (t , tok , & expectToken )
172
245
}
173
246
174
247
func TestWorkforcePoolTokenWithoutClientID (t * testing.T ) {
@@ -195,7 +268,12 @@ func TestWorkforcePoolTokenWithoutClientID(t *testing.T) {
195
268
if err != nil {
196
269
t .Fatalf ("Unexpected error: %e" , err )
197
270
}
198
- validateToken (t , tok )
271
+ expectToken := oauth2.Token {
272
+ AccessToken : correctAT ,
273
+ TokenType : "Bearer" ,
274
+ Expiry : testNow ().Add (time .Duration (3600 ) * time .Second ),
275
+ }
276
+ validateToken (t , tok , & expectToken )
199
277
}
200
278
201
279
func TestNonworkforceWithWorkforcePoolUserProject (t * testing.T ) {
0 commit comments