...

Source file src/golang.org/x/oauth2/google/externalaccount/filecredsource_test.go

Documentation: golang.org/x/oauth2/google/externalaccount

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package externalaccount
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  )
    11  
    12  var testFileConfig = Config{
    13  	Audience:                       "32555940559.apps.googleusercontent.com",
    14  	SubjectTokenType:               "urn:ietf:params:oauth:token-type:jwt",
    15  	TokenURL:                       "http://localhost:8080/v1/token",
    16  	TokenInfoURL:                   "http://localhost:8080/v1/tokeninfo",
    17  	ServiceAccountImpersonationURL: "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/service-gcs-admin@$PROJECT_ID.iam.gserviceaccount.com:generateAccessToken",
    18  	ClientSecret:                   "notsosecret",
    19  	ClientID:                       "rbrgnognrhongo3bi4gb9ghg9g",
    20  }
    21  
    22  func TestRetrieveFileSubjectToken(t *testing.T) {
    23  	var fileSourceTests = []struct {
    24  		name string
    25  		cs   CredentialSource
    26  		want string
    27  	}{
    28  		{
    29  			name: "UntypedFileSource",
    30  			cs: CredentialSource{
    31  				File: textBaseCredPath,
    32  			},
    33  			want: "street123",
    34  		},
    35  		{
    36  			name: "TextFileSource",
    37  			cs: CredentialSource{
    38  				File:   textBaseCredPath,
    39  				Format: Format{Type: fileTypeText},
    40  			},
    41  			want: "street123",
    42  		},
    43  		{
    44  			name: "JSONFileSource",
    45  			cs: CredentialSource{
    46  				File:   jsonBaseCredPath,
    47  				Format: Format{Type: fileTypeJSON, SubjectTokenFieldName: "SubjToken"},
    48  			},
    49  			want: "321road",
    50  		},
    51  	}
    52  
    53  	for _, test := range fileSourceTests {
    54  		test := test
    55  		tfc := testFileConfig
    56  		tfc.CredentialSource = &test.cs
    57  
    58  		t.Run(test.name, func(t *testing.T) {
    59  			base, err := tfc.parse(context.Background())
    60  			if err != nil {
    61  				t.Fatalf("parse() failed %v", err)
    62  			}
    63  
    64  			out, err := base.subjectToken()
    65  			if err != nil {
    66  				t.Errorf("Method subjectToken() errored.")
    67  			} else if test.want != out {
    68  				t.Errorf("got %v but want %v", out, test.want)
    69  			}
    70  
    71  			if got, want := base.credentialSourceType(), "file"; got != want {
    72  				t.Errorf("got %v but want %v", got, want)
    73  			}
    74  		})
    75  	}
    76  }
    77  

View as plain text