code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/wallet_import_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  const recoveryPhrase = "swing ceiling chaos green put insane ripple desk match tip melt usual shrug turkey renew icon parade veteran lens govern path rough page render"
    31  
    32  func TestImportWalletFlags(t *testing.T) {
    33  	t.Run("Valid flags succeeds", testImportWalletFlagsValidFlagsSucceeds)
    34  	t.Run("Missing wallet fails", testImportWalletFlagsMissingWalletFails)
    35  	t.Run("Missing recovery phrase file fails", testImportWalletFlagsMissingRecoveryPhraseFileFails)
    36  }
    37  
    38  func testImportWalletFlagsValidFlagsSucceeds(t *testing.T) {
    39  	testDir := t.TempDir()
    40  
    41  	// given
    42  	passphrase, passphraseFilePath := NewPassphraseFile(t, testDir)
    43  	recoveryPhraseFilePath := NewFile(t, testDir, "recovery-phrase.txt", recoveryPhrase)
    44  	walletName := vgrand.RandomStr(10)
    45  
    46  	f := &cmd.ImportWalletFlags{
    47  		Wallet:             walletName,
    48  		RecoveryPhraseFile: recoveryPhraseFilePath,
    49  		PassphraseFile:     passphraseFilePath,
    50  	}
    51  
    52  	expectedReq := api.AdminImportWalletParams{
    53  		Wallet:         walletName,
    54  		RecoveryPhrase: recoveryPhrase,
    55  		Passphrase:     passphrase,
    56  	}
    57  
    58  	// when
    59  	req, err := f.Validate()
    60  
    61  	// then
    62  	require.NoError(t, err)
    63  	assert.Equal(t, expectedReq, req)
    64  }
    65  
    66  func testImportWalletFlagsMissingWalletFails(t *testing.T) {
    67  	testDir := t.TempDir()
    68  
    69  	// given
    70  	f := newImportWalletFlags(t, testDir)
    71  	f.Wallet = ""
    72  
    73  	// when
    74  	req, err := f.Validate()
    75  
    76  	// then
    77  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet"))
    78  	assert.Empty(t, req)
    79  }
    80  
    81  func testImportWalletFlagsMissingRecoveryPhraseFileFails(t *testing.T) {
    82  	testDir := t.TempDir()
    83  
    84  	// given
    85  	f := newImportWalletFlags(t, testDir)
    86  	f.RecoveryPhraseFile = ""
    87  
    88  	// when
    89  	req, err := f.Validate()
    90  
    91  	// then
    92  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("recovery-phrase-file"))
    93  	assert.Empty(t, req)
    94  }
    95  
    96  func newImportWalletFlags(t *testing.T, testDir string) *cmd.ImportWalletFlags {
    97  	t.Helper()
    98  
    99  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
   100  	NewFile(t, testDir, "recovery-phrase.txt", recoveryPhrase)
   101  	walletName := vgrand.RandomStr(10)
   102  	pubKey := vgrand.RandomStr(20)
   103  
   104  	return &cmd.ImportWalletFlags{
   105  		Wallet:             walletName,
   106  		RecoveryPhraseFile: pubKey,
   107  		PassphraseFile:     passphraseFilePath,
   108  	}
   109  }