code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/transaction_check_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  	"encoding/json"
    20  	"fmt"
    21  	"testing"
    22  
    23  	cmd "code.vegaprotocol.io/vega/cmd/vegawallet/commands"
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    25  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    26  	"code.vegaprotocol.io/vega/wallet/api"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestCheckCommandFlags(t *testing.T) {
    33  	t.Run("Valid flags succeeds", testCheckCommandFlagsValidFlagsSucceeds)
    34  	t.Run("Missing wallet fails", testCheckCommandFlagsMissingWalletFails)
    35  	t.Run("Missing log level fails", testCheckCommandFlagsMissingLogLevelFails)
    36  	t.Run("Unsupported log level fails", testCheckCommandFlagsUnsupportedLogLevelFails)
    37  	t.Run("Missing network and node address fails", testCheckCommandFlagsMissingNetworkAndNodeAddressFails)
    38  	t.Run("Both network and node address specified fails", testCheckCommandFlagsBothNetworkAndNodeAddressSpecifiedFails)
    39  	t.Run("Missing public key fails", testCheckCommandFlagsMissingPubKeyFails)
    40  	t.Run("Missing request fails", testCheckCommandFlagsMissingRequestFails)
    41  	t.Run("Malformed request fails", testCheckCommandFlagsMalformedRequestFails)
    42  }
    43  
    44  func testCheckCommandFlagsValidFlagsSucceeds(t *testing.T) {
    45  	testDir := t.TempDir()
    46  
    47  	// given
    48  	expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir)
    49  	network := vgrand.RandomStr(10)
    50  	walletName := vgrand.RandomStr(10)
    51  	pubKey := vgrand.RandomStr(20)
    52  
    53  	f := &cmd.CheckTransactionFlags{
    54  		Network:        network,
    55  		NodeAddress:    "",
    56  		Wallet:         walletName,
    57  		PubKey:         pubKey,
    58  		Retries:        10,
    59  		LogLevel:       "debug",
    60  		PassphraseFile: passphraseFilePath,
    61  		RawTransaction: testTransactionJSON,
    62  	}
    63  
    64  	expectedReq := &api.AdminCheckTransactionParams{
    65  		Network:     network,
    66  		NodeAddress: "",
    67  		Wallet:      walletName,
    68  		PublicKey:   pubKey,
    69  		Retries:     10,
    70  		Transaction: testTransaction(t),
    71  	}
    72  
    73  	// when
    74  	req, passphrase, err := f.Validate()
    75  
    76  	// then
    77  	require.NoError(t, err)
    78  	require.NotNil(t, req)
    79  	assert.Equal(t, expectedPassphrase, passphrase)
    80  	expectedJSON, _ := json.Marshal(expectedReq)
    81  	actualJSON, _ := json.Marshal(req)
    82  	assert.Equal(t, expectedJSON, actualJSON)
    83  }
    84  
    85  func testCheckCommandFlagsMissingWalletFails(t *testing.T) {
    86  	testDir := t.TempDir()
    87  
    88  	// given
    89  	f := newCheckCommandFlags(t, testDir)
    90  	f.Wallet = ""
    91  
    92  	// when
    93  	req, _, err := f.Validate()
    94  
    95  	// then
    96  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet"))
    97  	assert.Empty(t, req)
    98  }
    99  
   100  func testCheckCommandFlagsMissingLogLevelFails(t *testing.T) {
   101  	testDir := t.TempDir()
   102  
   103  	// given
   104  	f := newCheckCommandFlags(t, testDir)
   105  	f.LogLevel = ""
   106  
   107  	// when
   108  	req, _, err := f.Validate()
   109  
   110  	// then
   111  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("level"))
   112  	assert.Empty(t, req)
   113  }
   114  
   115  func testCheckCommandFlagsUnsupportedLogLevelFails(t *testing.T) {
   116  	testDir := t.TempDir()
   117  
   118  	// given
   119  	f := newCheckCommandFlags(t, testDir)
   120  	f.LogLevel = vgrand.RandomStr(5)
   121  
   122  	// when
   123  	req, _, err := f.Validate()
   124  
   125  	// then
   126  	assert.EqualError(t, err, fmt.Sprintf("unsupported log level %q, supported levels: debug, info, warn, error", f.LogLevel))
   127  	assert.Empty(t, req)
   128  }
   129  
   130  // Do we need this?
   131  func testCheckCommandFlagsMissingNetworkAndNodeAddressFails(t *testing.T) {
   132  	testDir := t.TempDir()
   133  
   134  	// given
   135  	f := newCheckCommandFlags(t, testDir)
   136  	f.Network = ""
   137  	f.NodeAddress = ""
   138  
   139  	// when
   140  	req, _, err := f.Validate()
   141  
   142  	// then
   143  	assert.ErrorIs(t, err, flags.OneOfFlagsMustBeSpecifiedError("network", "node-address"))
   144  	assert.Empty(t, req)
   145  }
   146  
   147  func testCheckCommandFlagsBothNetworkAndNodeAddressSpecifiedFails(t *testing.T) {
   148  	testDir := t.TempDir()
   149  
   150  	// given
   151  	f := newCheckCommandFlags(t, testDir)
   152  	f.Network = vgrand.RandomStr(10)
   153  	f.NodeAddress = vgrand.RandomStr(10)
   154  
   155  	// when
   156  	req, _, err := f.Validate()
   157  
   158  	// then
   159  	assert.ErrorIs(t, err, flags.MutuallyExclusiveError("network", "node-address"))
   160  	assert.Empty(t, req)
   161  }
   162  
   163  func testCheckCommandFlagsMissingPubKeyFails(t *testing.T) {
   164  	testDir := t.TempDir()
   165  
   166  	// given
   167  	f := newCheckCommandFlags(t, testDir)
   168  	f.PubKey = ""
   169  
   170  	// when
   171  	req, _, err := f.Validate()
   172  
   173  	// then
   174  	assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pubkey"))
   175  	assert.Empty(t, req)
   176  }
   177  
   178  func testCheckCommandFlagsMissingRequestFails(t *testing.T) {
   179  	testDir := t.TempDir()
   180  
   181  	// given
   182  	f := newCheckCommandFlags(t, testDir)
   183  	f.RawTransaction = ""
   184  
   185  	// when
   186  	req, _, err := f.Validate()
   187  
   188  	// then
   189  	assert.ErrorIs(t, err, flags.ArgMustBeSpecifiedError("transaction"))
   190  	assert.Empty(t, req)
   191  }
   192  
   193  func testCheckCommandFlagsMalformedRequestFails(t *testing.T) {
   194  	testDir := t.TempDir()
   195  
   196  	// given
   197  	f := newCheckCommandFlags(t, testDir)
   198  	f.RawTransaction = vgrand.RandomStr(5)
   199  
   200  	// when
   201  	req, _, err := f.Validate()
   202  
   203  	// then
   204  	assert.Error(t, err)
   205  	assert.Empty(t, req)
   206  }
   207  
   208  func newCheckCommandFlags(t *testing.T, testDir string) *cmd.CheckTransactionFlags {
   209  	t.Helper()
   210  
   211  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
   212  	networkName := vgrand.RandomStr(10)
   213  	walletName := vgrand.RandomStr(10)
   214  	pubKey := vgrand.RandomStr(20)
   215  
   216  	return &cmd.CheckTransactionFlags{
   217  		Network:        networkName,
   218  		NodeAddress:    "",
   219  		Retries:        10,
   220  		LogLevel:       "debug",
   221  		RawTransaction: `{"voteSubmission": {"proposalId": "some-id", "value": "VALUE_YES"}}`,
   222  		Wallet:         walletName,
   223  		PubKey:         pubKey,
   224  		PassphraseFile: passphraseFilePath,
   225  	}
   226  }