code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/passphrase_update_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package cmd_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	cmd "code.vegaprotocol.io/vega/cmd/vegawallet/commands"
    22  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    23  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    24  	"code.vegaprotocol.io/vega/wallet/api"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestUpdatePassphrase(t *testing.T) {
    31  	t.Run("Valid flags succeeds", testUpdatePassphraseFlagsValidFlagsSucceeds)
    32  	t.Run("Missing wallet fails", testUpdatePassphraseFlagsMissingWalletFails)
    33  }
    34  
    35  func testUpdatePassphraseFlagsValidFlagsSucceeds(t *testing.T) {
    36  	testDir := t.TempDir()
    37  
    38  	// given
    39  	expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir)
    40  	isolatedPassphrase, isolatedPassphraseFilePath := NewPassphraseFile(t, testDir)
    41  	walletName := vgrand.RandomStr(10)
    42  
    43  	f := &cmd.UpdatePassphraseFlags{
    44  		Wallet:            walletName,
    45  		PassphraseFile:    passphraseFilePath,
    46  		NewPassphraseFile: isolatedPassphraseFilePath,
    47  	}
    48  
    49  	expectedReq := api.AdminUpdatePassphraseParams{
    50  		Wallet:        walletName,
    51  		NewPassphrase: isolatedPassphrase,
    52  	}
    53  
    54  	// when
    55  	req, passphrase, err := f.Validate()
    56  
    57  	// then
    58  	require.NoError(t, err)
    59  	assert.Equal(t, expectedReq, req)
    60  	assert.Equal(t, expectedPassphrase, passphrase)
    61  }
    62  
    63  func testUpdatePassphraseFlagsMissingWalletFails(t *testing.T) {
    64  	testDir := t.TempDir()
    65  
    66  	// given
    67  	f := newUpdatePassphraseFlags(t, testDir)
    68  	f.Wallet = ""
    69  
    70  	// when
    71  	req, _, err := f.Validate()
    72  
    73  	// then
    74  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet"))
    75  	assert.Empty(t, req)
    76  }
    77  
    78  func newUpdatePassphraseFlags(t *testing.T, testDir string) *cmd.UpdatePassphraseFlags {
    79  	t.Helper()
    80  
    81  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
    82  	_, newPassphraseFilePath := NewPassphraseFile(t, testDir)
    83  	walletName := vgrand.RandomStr(10)
    84  
    85  	return &cmd.UpdatePassphraseFlags{
    86  		Wallet:            walletName,
    87  		NewPassphraseFile: newPassphraseFilePath,
    88  		PassphraseFile:    passphraseFilePath,
    89  	}
    90  }