@@ -16,6 +16,7 @@ package externalaccount
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"fmt"
20
21
"io"
21
22
"net/http"
@@ -24,6 +25,7 @@ import (
24
25
"time"
25
26
26
27
"cloud.google.com/go/auth"
28
+ "cloud.google.com/go/auth/credentials/internal/stsexchange"
27
29
"cloud.google.com/go/auth/internal"
28
30
"cloud.google.com/go/auth/internal/internaldetect"
29
31
)
58
60
)
59
61
60
62
func 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
+ }
61
136
opts := & Options {
62
137
Audience : "32555940559.apps.googleusercontent.com" ,
63
138
SubjectTokenType : idTokenType ,
0 commit comments