...

Source file src/cloud.google.com/go/auth/credentials/internal/externalaccount/file_provider_test.go

Documentation: cloud.google.com/go/auth/credentials/internal/externalaccount

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package externalaccount
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"cloud.google.com/go/auth/internal/credsfile"
    22  )
    23  
    24  func TestRetrieveFileSubjectToken(t *testing.T) {
    25  	var tests = []struct {
    26  		name string
    27  		cs   *credsfile.CredentialSource
    28  		want string
    29  	}{
    30  		{
    31  			name: "untyped file format",
    32  			cs: &credsfile.CredentialSource{
    33  				File: textBaseCredPath,
    34  			},
    35  			want: "street123",
    36  		},
    37  		{
    38  			name: "text file format",
    39  			cs: &credsfile.CredentialSource{
    40  				File:   textBaseCredPath,
    41  				Format: &credsfile.Format{Type: fileTypeText},
    42  			},
    43  			want: "street123",
    44  		},
    45  		{
    46  			name: "JSON file format",
    47  			cs: &credsfile.CredentialSource{
    48  				File:   jsonBaseCredPath,
    49  				Format: &credsfile.Format{Type: fileTypeJSON, SubjectTokenFieldName: "SubjToken"},
    50  			},
    51  			want: "321road",
    52  		},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		t.Run(test.name, func(t *testing.T) {
    57  			opts := cloneTestOpts()
    58  			opts.CredentialSource = test.cs
    59  			base, err := newSubjectTokenProvider(opts)
    60  			if err != nil {
    61  				t.Fatalf("parse() failed %v", err)
    62  			}
    63  
    64  			out, err := base.subjectToken(context.Background())
    65  			if err != nil {
    66  				t.Errorf("Method subjectToken() errored.")
    67  			} else if test.want != out {
    68  				t.Errorf("got %v, want %v", out, test.want)
    69  			}
    70  			if got, want := base.providerType(), fileProviderType; got != want {
    71  				t.Fatalf("got %q, want %q", got, want)
    72  			}
    73  		})
    74  	}
    75  }
    76  

View as plain text