@@ -4,9 +4,12 @@ import (
44 "crypto/rand"
55 "crypto/rsa"
66 "crypto/x509"
7+ "encoding/json"
78 "encoding/pem"
89 "fmt"
910 "io"
11+ "net/http"
12+ "net/http/httptest"
1013 "strconv"
1114 "testing"
1215 "time"
@@ -235,58 +238,28 @@ func TestAuthenticationConfig(t *testing.T) {
235238
236239func TestInitKeyFlow (t * testing.T ) {
237240 tests := []struct {
238- description string
239- accessTokenSet bool
240- refreshToken string
241- saKey string
242- privateKeySet bool
243- tokenEndpoint string
244- isValid bool
241+ description string
242+ saKey string
243+ privateKeySet bool
244+ isValid bool
245245 }{
246246 {
247- description : "base" ,
248- accessTokenSet : true ,
249- refreshToken : "refresh_token" ,
250- saKey : testServiceAccountKey ,
251- privateKeySet : true ,
252- tokenEndpoint : "token_url" ,
253- isValid : true ,
247+ description : "base" ,
248+ saKey : testServiceAccountKey ,
249+ privateKeySet : true ,
250+ isValid : true ,
254251 },
255252 {
256- description : "invalid_service_account_key" ,
257- accessTokenSet : true ,
258- refreshToken : "refresh_token" ,
259- saKey : "" ,
260- privateKeySet : true ,
261- tokenEndpoint : "token_url" ,
262- isValid : false ,
263- },
264- {
265- description : "invalid_private_key" ,
266- accessTokenSet : true ,
267- refreshToken : "refresh_token" ,
268- saKey : testServiceAccountKey ,
269- privateKeySet : false ,
270- tokenEndpoint : "token_url" ,
271- isValid : false ,
272- },
273- {
274- description : "invalid_access_token" ,
275- accessTokenSet : false ,
276- refreshToken : "refresh_token" ,
277- saKey : testServiceAccountKey ,
278- privateKeySet : true ,
279- tokenEndpoint : "token_url" ,
280- isValid : false ,
253+ description : "invalid_service_account_key" ,
254+ saKey : "" ,
255+ privateKeySet : true ,
256+ isValid : false ,
281257 },
282258 {
283- description : "empty_refresh_token" ,
284- accessTokenSet : false ,
285- refreshToken : "" ,
286- saKey : testServiceAccountKey ,
287- privateKeySet : true ,
288- tokenEndpoint : "token_url" ,
289- isValid : false ,
259+ description : "no_private_key_set" ,
260+ saKey : testServiceAccountKey ,
261+ privateKeySet : false ,
262+ isValid : false ,
290263 },
291264 }
292265
@@ -297,13 +270,11 @@ func TestInitKeyFlow(t *testing.T) {
297270 authFields := make (map [authFieldKey ]string )
298271 var accessToken string
299272 var err error
300- if tt .accessTokenSet {
301- accessTokenJWT := jwt .NewWithClaims (jwt .SigningMethodHS256 , jwt.RegisteredClaims {
302- ExpiresAt : jwt .NewNumericDate (timestamp )})
303- accessToken , err = accessTokenJWT .SignedString (testSigningKey )
304- if err != nil {
305- t .Fatalf ("Get test access token as string: %s" , err )
306- }
273+ accessTokenJWT := jwt .NewWithClaims (jwt .SigningMethodHS256 , jwt.RegisteredClaims {
274+ ExpiresAt : jwt .NewNumericDate (timestamp )})
275+ accessToken , err = accessTokenJWT .SignedString (testSigningKey )
276+ if err != nil {
277+ t .Fatalf ("Get test access token as string: %s" , err )
307278 }
308279 if tt .privateKeySet {
309280 privateKey , err := generatePrivateKey ()
@@ -313,16 +284,42 @@ func TestInitKeyFlow(t *testing.T) {
313284 authFields [PRIVATE_KEY ] = string (privateKey )
314285 }
315286 authFields [ACCESS_TOKEN ] = accessToken
316- authFields [REFRESH_TOKEN ] = tt .refreshToken
317287 authFields [SERVICE_ACCOUNT_KEY ] = tt .saKey
318- authFields [TOKEN_CUSTOM_ENDPOINT ] = tt .tokenEndpoint
288+
289+ // Mock server to avoid HTTP calls
290+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
291+ w .WriteHeader (http .StatusOK )
292+ resp := clients.TokenResponseBody {
293+ AccessToken : accessToken ,
294+ ExpiresIn : 3600 ,
295+ TokenType : "Bearer" ,
296+ }
297+ jsonResp , err := json .Marshal (resp )
298+ if err != nil {
299+ t .Fatalf ("Failed to marshal json: %v" , err )
300+ }
301+ _ , err = w .Write (jsonResp )
302+ if err != nil {
303+ t .Fatalf ("Failed to write response: %v" , err )
304+ }
305+ }))
306+ defer server .Close ()
307+ authFields [TOKEN_CUSTOM_ENDPOINT ] = server .URL
308+
319309 err = SetAuthFieldMap (authFields )
320310 if err != nil {
321311 t .Fatalf ("Failed to set in auth storage: %v" , err )
322312 }
323313
324314 keyFlowWithStorage , err := initKeyFlowWithStorage ()
315+ if err != nil {
316+ if ! tt .isValid {
317+ return
318+ }
319+ t .Fatalf ("Expected no error but error was returned: %v" , err )
320+ }
325321
322+ getAccessToken , err := keyFlowWithStorage .keyFlow .GetAccessToken ()
326323 if ! tt .isValid {
327324 if err == nil {
328325 t .Fatalf ("Expected error but no error was returned" )
@@ -331,15 +328,8 @@ func TestInitKeyFlow(t *testing.T) {
331328 if err != nil {
332329 t .Fatalf ("Expected no error but error was returned: %v" , err )
333330 }
334- expectedToken := & clients.TokenResponseBody {
335- AccessToken : accessToken ,
336- ExpiresIn : int (timestamp .Unix ()),
337- RefreshToken : tt .refreshToken ,
338- Scope : "" ,
339- TokenType : "Bearer" ,
340- }
341- if ! cmp .Equal (* expectedToken , keyFlowWithStorage .keyFlow .GetToken ()) {
342- t .Errorf ("The returned result is wrong. Expected %+v, got %+v" , expectedToken , keyFlowWithStorage .keyFlow .GetToken ())
331+ if ! cmp .Equal (accessToken , getAccessToken ) {
332+ t .Errorf ("The returned result is wrong. Expected %+v, got %+v" , accessToken , getAccessToken )
343333 }
344334 }
345335 })
0 commit comments