github.com/jfrog/jfrog-cli-platform-services@v1.2.0/model/secrets_test.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestEncryption(t *testing.T) {
    12  	const secretValue = "my-secret-value"
    13  	const password = "my-password"
    14  
    15  	encrypted, err := EncryptSecret(password, secretValue)
    16  	require.NoError(t, err)
    17  	require.NotEqual(t, secretValue, encrypted)
    18  
    19  	decrypted, err := DecryptSecret(password, encrypted)
    20  	require.NoError(t, err)
    21  
    22  	assert.Equal(t, secretValue, decrypted)
    23  }
    24  
    25  func Test_validateSecretPassword(t *testing.T) {
    26  	tests := []struct {
    27  		name            string
    28  		password        string
    29  		validationError string
    30  	}{
    31  		{
    32  			name:     "valid",
    33  			password: "/abcdef123456!",
    34  		},
    35  		{
    36  			name:            "too short",
    37  			password:        "abcdef",
    38  			validationError: fmt.Sprintf("a secret should have a minimum length of %d, got 6", minPasswordLength),
    39  		},
    40  	}
    41  
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			err := validateSecretPassword(tt.password)
    45  			if tt.validationError == "" {
    46  				assert.NoError(t, err)
    47  			} else {
    48  				assert.EqualError(t, err, tt.validationError)
    49  			}
    50  		})
    51  	}
    52  }