github.com/openshift/installer@v1.4.17/pkg/asset/agent/gencrypto/auth_utils_test.go (about)

     1  package gencrypto
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/google/uuid"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestParseExpirationFromToken(t *testing.T) {
    12  	publicKey, privateKey, err := keyPairPEM()
    13  	assert.NotEmpty(t, publicKey)
    14  	assert.NotEmpty(t, privateKey)
    15  	assert.NoError(t, err)
    16  
    17  	infraEnvID := uuid.New().String()
    18  
    19  	tokenNoExp, err := generateToken(infraEnvID, privateKey)
    20  	assert.NotEmpty(t, tokenNoExp)
    21  	assert.NoError(t, err)
    22  
    23  	expiry := time.Now().UTC().Add(30 * time.Second)
    24  	tokenWithExp, err := generateToken(infraEnvID, privateKey, expiry)
    25  	assert.NotEmpty(t, tokenWithExp)
    26  	assert.NoError(t, err)
    27  
    28  	cases := []struct {
    29  		name, token, errorMessage string
    30  		expectedErr               bool
    31  	}{
    32  		{
    33  			name:         "exp-claim-not-set",
    34  			token:        tokenNoExp,
    35  			expectedErr:  true,
    36  			errorMessage: "token missing 'exp' claim",
    37  		},
    38  		{
    39  			name:  "exp-claim-set",
    40  			token: tokenWithExp,
    41  		},
    42  	}
    43  	for _, tc := range cases {
    44  		t.Run(tc.name, func(t *testing.T) {
    45  			_, err = ParseExpirationFromToken(tc.token)
    46  			if tc.expectedErr {
    47  				assert.EqualError(t, err, tc.errorMessage)
    48  			} else {
    49  				assert.NoError(t, err)
    50  			}
    51  		})
    52  	}
    53  }