github.com/blend/go-sdk@v1.20220411.3/certutil/cert_manager_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package certutil
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  	"github.com/blend/go-sdk/uuid"
    15  )
    16  
    17  func TestNewCertManagerWithKeyPairs(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	assert := assert.New(t)
    21  
    22  	mgr, err := NewCertManagerWithKeyPairs(KeyPair{
    23  		CertPath: "testdata/server.cert.pem",
    24  		KeyPath:  "testdata/server.key.pem",
    25  	}, []KeyPair{{
    26  		CertPath: "testdata/ca.cert.pem",
    27  	}}, KeyPair{
    28  		CertPath: "testdata/client.cert.pem",
    29  	})
    30  	assert.Nil(err)
    31  	assert.NotEmpty(mgr.ClientCerts)
    32  	assert.NotNil(mgr.TLSConfig)
    33  	assert.NotNil(mgr.TLSConfig.RootCAs)
    34  }
    35  
    36  func TestCertManagerAddClientCert(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	assert := assert.New(t)
    40  
    41  	mgr := NewCertManager()
    42  	assert.Nil(mgr.AddClientCert(certLiteral))
    43  	assert.NotEmpty(mgr.ClientCerts)
    44  	assert.True(mgr.HasClientCert(certLiteralCommonName))
    45  	assert.NotNil(mgr.AddClientCert(uuid.V4()))
    46  }
    47  
    48  func TestCertManagerRemoveClientCert(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	assert := assert.New(t)
    52  
    53  	mgr := NewCertManager()
    54  	assert.Nil(mgr.AddClientCert(certLiteral))
    55  	assert.NotEmpty(mgr.ClientCerts)
    56  
    57  	assert.Nil(mgr.RemoveClientCert(certLiteralCommonName))
    58  	assert.Empty(mgr.ClientCerts)
    59  }
    60  
    61  func TestCertManagerGetConfigForClient(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	assert := assert.New(t)
    65  
    66  	mgr := NewCertManager()
    67  	assert.Nil(mgr.AddClientCert(certLiteral))
    68  	assert.NotEmpty(mgr.ClientCerts)
    69  
    70  	config, err := mgr.GetConfigForClient(nil)
    71  	assert.Nil(err)
    72  	assert.NotEmpty(config.ClientCAs.Subjects())
    73  }