github.com/hashicorp/cap@v0.6.0/oidc/options_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package oidc
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/coreos/go-oidc/v3/oidc"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestApplyOpts(t *testing.T) {
    15  	// ApplyOpts testing is covered by other tests but we do have just more
    16  	// more test to add here.
    17  	// Let's make sure we don't panic on nil options
    18  	anonymousOpts := struct {
    19  		Names []string
    20  	}{
    21  		nil,
    22  	}
    23  	ApplyOpts(anonymousOpts, nil)
    24  }
    25  
    26  func Test_WithNow(t *testing.T) {
    27  	t.Parallel()
    28  	testNow := func() time.Time {
    29  		return time.Now().Add(-1 * time.Minute)
    30  	}
    31  	t.Run("tokenOptions", func(t *testing.T) {
    32  		opts := getTokenOpts(WithNow(testNow))
    33  		testOpts := tokenDefaults()
    34  		testOpts.withNowFunc = testNow
    35  		testAssertEqualFunc(t, opts.withNowFunc, testNow, "now = %p,want %p", opts.withNowFunc, testNow)
    36  	})
    37  	t.Run("reqOptions", func(t *testing.T) {
    38  		opts := getReqOpts(WithNow(testNow))
    39  		testOpts := reqDefaults()
    40  		testOpts.withNowFunc = testNow
    41  		testAssertEqualFunc(t, opts.withNowFunc, testNow, "now = %p,want %p", opts.withNowFunc, testNow)
    42  	})
    43  }
    44  
    45  func Test_WithAudiences(t *testing.T) {
    46  	t.Parallel()
    47  	t.Run("configOptions", func(t *testing.T) {
    48  		assert := assert.New(t)
    49  		opts := getConfigOpts(WithAudiences("alice", "bob"))
    50  		testOpts := configDefaults()
    51  		testOpts.withAudiences = []string{"alice", "bob"}
    52  		assert.Equal(opts, testOpts)
    53  
    54  		opts = getConfigOpts(WithAudiences())
    55  		testOpts = configDefaults()
    56  		testOpts.withAudiences = nil
    57  		assert.Equal(opts, testOpts)
    58  	})
    59  	t.Run("reqOptions", func(t *testing.T) {
    60  		assert := assert.New(t)
    61  		opts := getReqOpts(WithAudiences("alice", "bob"))
    62  		testOpts := reqDefaults()
    63  		testOpts.withAudiences = []string{"alice", "bob"}
    64  		assert.Equal(opts, testOpts)
    65  
    66  		opts = getReqOpts(WithAudiences())
    67  		testOpts = reqDefaults()
    68  		testOpts.withAudiences = nil
    69  		assert.Equal(opts, testOpts)
    70  	})
    71  }
    72  
    73  func Test_WithScopes(t *testing.T) {
    74  	t.Parallel()
    75  	t.Run("configOptions", func(t *testing.T) {
    76  		assert := assert.New(t)
    77  		opts := getConfigOpts(WithScopes("alice", "bob"))
    78  		testOpts := configDefaults()
    79  		testOpts.withScopes = []string{oidc.ScopeOpenID, "alice", "bob"}
    80  		assert.Equal(opts, testOpts)
    81  
    82  		opts = getConfigOpts(WithScopes())
    83  		testOpts = configDefaults()
    84  		testOpts.withScopes = []string{oidc.ScopeOpenID}
    85  		assert.Equal(opts, testOpts)
    86  	})
    87  	t.Run("reqOptions", func(t *testing.T) {
    88  		t.Parallel()
    89  		assert := assert.New(t)
    90  		opts := getReqOpts(WithScopes("alice", "bob"))
    91  		testOpts := reqDefaults()
    92  		testOpts.withScopes = []string{oidc.ScopeOpenID, "alice", "bob"}
    93  		assert.Equal(opts, testOpts)
    94  
    95  		opts = getReqOpts(WithScopes())
    96  		testOpts = reqDefaults()
    97  		testOpts.withScopes = nil
    98  		assert.Equal(opts, testOpts)
    99  	})
   100  }