...

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

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

     1  // Copyright 2024 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  	"errors"
    20  	"testing"
    21  )
    22  
    23  func TestRetrieveSubjectToken_ProgrammaticAuth(t *testing.T) {
    24  	want := "subjectToken"
    25  	opts := cloneTestOpts()
    26  
    27  	opts.SubjectTokenProvider = fakeSubjectTokenProvider{
    28  		subjectToken: want,
    29  	}
    30  
    31  	base, err := newSubjectTokenProvider(opts)
    32  	if err != nil {
    33  		t.Fatalf("newSubjectTokenProvider(): %v", err)
    34  	}
    35  
    36  	got, err := base.subjectToken(context.Background())
    37  	if err != nil {
    38  		t.Fatalf("subjectToken(): %v", err)
    39  	}
    40  
    41  	if got != want {
    42  		t.Fatalf("got %s, want %s", got, want)
    43  	}
    44  }
    45  
    46  func TestRetrieveSubjectToken_ProgrammaticAuthFails(t *testing.T) {
    47  	want := errors.New("test error")
    48  	opts := cloneTestOpts()
    49  
    50  	opts.SubjectTokenProvider = fakeSubjectTokenProvider{
    51  		err: want,
    52  	}
    53  
    54  	base, err := newSubjectTokenProvider(opts)
    55  	if err != nil {
    56  		t.Fatalf("newSubjectTokenProvider(): %v", err)
    57  	}
    58  
    59  	_, got := base.subjectToken(context.Background())
    60  	if got == nil {
    61  		t.Fatalf("subjectToken() = %v, want nil", got)
    62  	}
    63  	if got != want {
    64  		t.Errorf("got %v, want %v", got, want)
    65  	}
    66  }
    67  
    68  func TestRetrieveSubjectToken_ProgrammaticAuthOptions(t *testing.T) {
    69  	opts := cloneTestOpts()
    70  	tokOpts := &RequestOptions{Audience: opts.Audience, SubjectTokenType: opts.SubjectTokenType}
    71  
    72  	opts.SubjectTokenProvider = fakeSubjectTokenProvider{
    73  		subjectToken:    "subjectToken",
    74  		expectedOptions: tokOpts,
    75  	}
    76  
    77  	base, err := newSubjectTokenProvider(opts)
    78  	if err != nil {
    79  		t.Fatalf("newSubjectTokenProvider(): %v", err)
    80  	}
    81  
    82  	if _, err = base.subjectToken(context.Background()); err != nil {
    83  		t.Fatalf("subjectToken(): %v", err)
    84  	}
    85  }
    86  
    87  type fakeSubjectTokenProvider struct {
    88  	err             error
    89  	subjectToken    string
    90  	expectedOptions *RequestOptions
    91  }
    92  
    93  func (p fakeSubjectTokenProvider) SubjectToken(ctx context.Context, options *RequestOptions) (string, error) {
    94  	if p.err != nil {
    95  		return "", p.err
    96  	}
    97  	if p.expectedOptions != nil {
    98  		if p.expectedOptions.Audience != options.Audience {
    99  			return "", errors.New("audience does not match")
   100  		}
   101  		if p.expectedOptions.SubjectTokenType != options.SubjectTokenType {
   102  			return "", errors.New("audience does not match")
   103  		}
   104  	}
   105  
   106  	return p.subjectToken, nil
   107  }
   108  

View as plain text