github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/auth/provider_test.go (about)

     1  package auth_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/whoyao/protocol/auth"
    11  )
    12  
    13  func TestFileBasedKeyProvider(t *testing.T) {
    14  	keys := map[string]string{
    15  		"key1": "secret1",
    16  		"key2": "secret2",
    17  		"key3": "secret3",
    18  	}
    19  	f, err := os.CreateTemp("", "keyfile")
    20  	assert.NoError(t, err)
    21  	defer func() {
    22  		os.Remove(f.Name())
    23  	}()
    24  
    25  	f.WriteString("key1: secret1\n")
    26  	f.WriteString("key2: secret2 \r\n")
    27  	f.WriteString("\n")
    28  	f.WriteString("key3: secret3")
    29  
    30  	f.Close()
    31  
    32  	r, err := os.Open(f.Name())
    33  	require.NoError(t, err)
    34  	defer r.Close()
    35  	p, err := auth.NewFileBasedKeyProviderFromReader(r)
    36  	assert.NoError(t, err)
    37  
    38  	for key, val := range keys {
    39  		assert.Equal(t, val, p.GetSecret(key))
    40  	}
    41  }