github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/bcrypt_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"golang.org/x/crypto/bcrypt"
    11  )
    12  
    13  func TestGeneratePassword(t *testing.T) {
    14  	bcryptCmd := NewBcryptCmd()
    15  	bcryptCmd.SetArgs([]string{"--password", "abc"})
    16  	output := new(bytes.Buffer)
    17  	bcryptCmd.SetOut(output)
    18  	err := bcryptCmd.Execute()
    19  	if err != nil {
    20  		return
    21  	}
    22  	err = bcrypt.CompareHashAndPassword(output.Bytes(), []byte("abc"))
    23  	assert.NoError(t, err)
    24  }
    25  
    26  func TestGeneratePasswordWithStdin(t *testing.T) {
    27  	oldStdin := os.Stdin
    28  	defer func() {
    29  		os.Stdin = oldStdin
    30  	}()
    31  
    32  	input := bytes.NewBufferString("abc\n")
    33  	r, w, _ := os.Pipe()
    34  	_, _ = w.Write(input.Bytes())
    35  	w.Close()
    36  	os.Stdin = r
    37  
    38  	bcryptCmd := NewBcryptCmd()
    39  	bcryptCmd.SetArgs([]string{})
    40  	output := new(bytes.Buffer)
    41  	bcryptCmd.SetOut(output)
    42  
    43  	err := bcryptCmd.Execute()
    44  	require.NoError(t, err)
    45  
    46  	err = bcrypt.CompareHashAndPassword(output.Bytes(), []byte("abc"))
    47  	assert.NoError(t, err)
    48  }