code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/transaction_send_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 TestSendCommandFlags(t *testing.T) { 33 t.Run("Valid flags succeeds", testSendCommandFlagsValidFlagsSucceeds) 34 t.Run("Missing wallet fails", testSendCommandFlagsMissingWalletFails) 35 t.Run("Missing log level fails", testSendCommandFlagsMissingLogLevelFails) 36 t.Run("Unsupported log level fails", testSendCommandFlagsUnsupportedLogLevelFails) 37 t.Run("Missing network and node address fails", testSendCommandFlagsMissingNetworkAndNodeAddressFails) 38 t.Run("Both network and node address specified fails", testSendCommandFlagsBothNetworkAndNodeAddressSpecifiedFails) 39 t.Run("Missing public key fails", testSendCommandFlagsMissingPubKeyFails) 40 t.Run("Missing request fails", testSendCommandFlagsMissingRequestFails) 41 t.Run("Malformed request fails", testSendCommandFlagsMalformedRequestFails) 42 } 43 44 func testSendCommandFlagsValidFlagsSucceeds(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.SendTransactionFlags{ 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.AdminSendTransactionParams{ 65 Network: network, 66 NodeAddress: "", 67 Wallet: walletName, 68 PublicKey: pubKey, 69 Retries: 10, 70 Transaction: testTransaction(t), 71 SendingMode: "TYPE_ASYNC", 72 } 73 74 // when 75 req, passphrase, err := f.Validate() 76 77 // then 78 require.NoError(t, err) 79 require.NotNil(t, req) 80 assert.Equal(t, expectedPassphrase, passphrase) 81 expectedJSON, _ := json.Marshal(expectedReq) 82 actualJSON, _ := json.Marshal(req) 83 assert.Equal(t, expectedJSON, actualJSON) 84 } 85 86 func testSendCommandFlagsMissingWalletFails(t *testing.T) { 87 testDir := t.TempDir() 88 89 // given 90 f := newSendCommandFlags(t, testDir) 91 f.Wallet = "" 92 93 // when 94 req, _, err := f.Validate() 95 96 // then 97 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet")) 98 assert.Empty(t, req) 99 } 100 101 func testSendCommandFlagsMissingLogLevelFails(t *testing.T) { 102 testDir := t.TempDir() 103 104 // given 105 f := newSendCommandFlags(t, testDir) 106 f.LogLevel = "" 107 108 // when 109 req, _, err := f.Validate() 110 111 // then 112 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("level")) 113 assert.Empty(t, req) 114 } 115 116 func testSendCommandFlagsUnsupportedLogLevelFails(t *testing.T) { 117 testDir := t.TempDir() 118 119 // given 120 f := newSendCommandFlags(t, testDir) 121 f.LogLevel = vgrand.RandomStr(5) 122 123 // when 124 req, _, err := f.Validate() 125 126 // then 127 assert.EqualError(t, err, fmt.Sprintf("unsupported log level %q, supported levels: debug, info, warn, error", f.LogLevel)) 128 assert.Empty(t, req) 129 } 130 131 func testSendCommandFlagsMissingNetworkAndNodeAddressFails(t *testing.T) { 132 testDir := t.TempDir() 133 134 // given 135 f := newSendCommandFlags(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 testSendCommandFlagsBothNetworkAndNodeAddressSpecifiedFails(t *testing.T) { 148 testDir := t.TempDir() 149 150 // given 151 f := newSendCommandFlags(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 testSendCommandFlagsMissingPubKeyFails(t *testing.T) { 164 testDir := t.TempDir() 165 166 // given 167 f := newSendCommandFlags(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 testSendCommandFlagsMissingRequestFails(t *testing.T) { 179 testDir := t.TempDir() 180 181 // given 182 f := newSendCommandFlags(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 testSendCommandFlagsMalformedRequestFails(t *testing.T) { 194 testDir := t.TempDir() 195 196 // given 197 f := newSendCommandFlags(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 newSendCommandFlags(t *testing.T, testDir string) *cmd.SendTransactionFlags { 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.SendTransactionFlags{ 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 }