code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/key_isolate_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 TestIsolateKeyFlags(t *testing.T) {
    31  	t.Run("Valid flags succeeds", testIsolateKeyFlagsValidFlagsSucceeds)
    32  	t.Run("Missing wallet fails", testIsolateKeyFlagsMissingWalletFails)
    33  	t.Run("Missing public key fails", testIsolateKeyFlagsMissingPubKeyFails)
    34  }
    35  
    36  func testIsolateKeyFlagsValidFlagsSucceeds(t *testing.T) {
    37  	testDir := t.TempDir()
    38  
    39  	// given
    40  	expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir)
    41  	isolatedPassphrase, isolatedPassphraseFilePath := NewPassphraseFile(t, testDir)
    42  	walletName := vgrand.RandomStr(10)
    43  	pubKey := vgrand.RandomStr(20)
    44  
    45  	f := &cmd.IsolateKeyFlags{
    46  		Wallet:                       walletName,
    47  		PubKey:                       pubKey,
    48  		PassphraseFile:               passphraseFilePath,
    49  		IsolatedWalletPassphraseFile: isolatedPassphraseFilePath,
    50  	}
    51  
    52  	expectedReq := api.AdminIsolateKeyParams{
    53  		Wallet:                   walletName,
    54  		PublicKey:                pubKey,
    55  		IsolatedWalletPassphrase: isolatedPassphrase,
    56  	}
    57  
    58  	// when
    59  	req, passphrase, err := f.Validate()
    60  
    61  	// then
    62  	require.NoError(t, err)
    63  	assert.Equal(t, expectedReq, req)
    64  	assert.Equal(t, expectedPassphrase, passphrase)
    65  }
    66  
    67  func testIsolateKeyFlagsMissingWalletFails(t *testing.T) {
    68  	testDir := t.TempDir()
    69  
    70  	// given
    71  	f := newIsolateKeyFlags(t, testDir)
    72  	f.Wallet = ""
    73  
    74  	// when
    75  	req, _, err := f.Validate()
    76  
    77  	// then
    78  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet"))
    79  	assert.Empty(t, req)
    80  }
    81  
    82  func testIsolateKeyFlagsMissingPubKeyFails(t *testing.T) {
    83  	testDir := t.TempDir()
    84  
    85  	// given
    86  	f := newIsolateKeyFlags(t, testDir)
    87  	f.PubKey = ""
    88  
    89  	// when
    90  	req, _, err := f.Validate()
    91  
    92  	// then
    93  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pubkey"))
    94  	assert.Empty(t, req)
    95  }
    96  
    97  func newIsolateKeyFlags(t *testing.T, testDir string) *cmd.IsolateKeyFlags {
    98  	t.Helper()
    99  
   100  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
   101  	_, isolatedWalletPassphraseFilePath := NewPassphraseFile(t, testDir)
   102  	walletName := vgrand.RandomStr(10)
   103  	pubKey := vgrand.RandomStr(20)
   104  
   105  	return &cmd.IsolateKeyFlags{
   106  		Wallet:                       walletName,
   107  		PubKey:                       pubKey,
   108  		PassphraseFile:               passphraseFilePath,
   109  		IsolatedWalletPassphraseFile: isolatedWalletPassphraseFilePath,
   110  	}
   111  }