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