@@ -16,6 +16,7 @@ package externalaccount
1616
1717import (
1818 "context"
19+ "encoding/json"
1920 "fmt"
2021 "io"
2122 "net/http"
@@ -24,6 +25,7 @@ import (
2425 "time"
2526
2627 "cloud.google.com/go/auth"
28+ "cloud.google.com/go/auth/credentials/internal/stsexchange"
2729 "cloud.google.com/go/auth/internal"
2830 "cloud.google.com/go/auth/internal/internaldetect"
2931)
5860)
5961
6062func TestToken (t * testing.T ) {
63+ tests := []struct {
64+ name string
65+ respBody * stsexchange.TokenResponse
66+ wantError bool
67+ }{
68+ {
69+ name : "works" ,
70+ respBody : & stsexchange.TokenResponse {
71+ AccessToken : correctAT ,
72+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
73+ TokenType : "Bearer" ,
74+ ExpiresIn : 3600 ,
75+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
76+ },
77+ },
78+ {
79+ name : "no exp time on tok" ,
80+ respBody : & stsexchange.TokenResponse {
81+ AccessToken : correctAT ,
82+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
83+ TokenType : "Bearer" ,
84+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
85+ },
86+ wantError : true ,
87+ },
88+ {
89+ name : "negative exp time" ,
90+ respBody : & stsexchange.TokenResponse {
91+ AccessToken : correctAT ,
92+ IssuedTokenType : "urn:ietf:params:oauth:token-type:access_token" ,
93+ TokenType : "Bearer" ,
94+ ExpiresIn : - 1 ,
95+ Scope : "https://www.googleapis.com/auth/cloud-platform" ,
96+ },
97+ wantError : true ,
98+ },
99+ }
100+ for _ , tt := range tests {
101+ opts := & Options {
102+ Audience : "32555940559.apps.googleusercontent.com" ,
103+ SubjectTokenType : idTokenType ,
104+ ClientSecret : "notsosecret" ,
105+ ClientID : "rbrgnognrhongo3bi4gb9ghg9g" ,
106+ CredentialSource : testBaseCredSource ,
107+ Scopes : []string {"https://www.googleapis.com/auth/devstorage.full_control" },
108+ }
109+
110+ respBody , err := json .Marshal (tt .respBody )
111+ if err != nil {
112+ t .Fatal (err )
113+ }
114+
115+ server := & testExchangeTokenServer {
116+ url : "/" ,
117+ authorization : "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" ,
118+ contentType : "application/x-www-form-urlencoded" ,
119+ body : baseCredsRequestBody ,
120+ response : string (respBody ),
121+ metricsHeader : expectedMetricsHeader ("file" , false , false ),
122+ }
123+
124+ tok , err := run (t , opts , server )
125+ if err != nil && ! tt .wantError {
126+ t .Fatal (err )
127+ }
128+ if tt .wantError {
129+ if err == nil {
130+ t .Fatal ("want err, got nil" )
131+ }
132+ continue
133+ }
134+ validateToken (t , tok )
135+ }
61136 opts := & Options {
62137 Audience : "32555940559.apps.googleusercontent.com" ,
63138 SubjectTokenType : idTokenType ,
0 commit comments