code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/key_annotate_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 "code.vegaprotocol.io/vega/wallet/wallet" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 ) 30 31 func TestAnnotateKeyFlags(t *testing.T) { 32 t.Run("Valid flags succeeds", testAnnotateKeyFlagsValidFlagsSucceeds) 33 t.Run("Missing wallet fails", testAnnotateKeyFlagsMissingWalletFails) 34 t.Run("Missing public key fails", testAnnotateKeyFlagsMissingPubKeyFails) 35 t.Run("Missing metadata fails", testAnnotateKeyFlagsMissingMetadataAndClearFails) 36 t.Run("Clearing with metadata fails", testAnnotateKeyFlagsClearingWithMetadataFails) 37 t.Run("Invalid metadata fails", testAnnotateKeyFlagsInvalidMetadataFails) 38 } 39 40 func testAnnotateKeyFlagsValidFlagsSucceeds(t *testing.T) { 41 testDir := t.TempDir() 42 43 // given 44 expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir) 45 walletName := vgrand.RandomStr(10) 46 pubKey := vgrand.RandomStr(20) 47 48 f := &cmd.AnnotateKeyFlags{ 49 Wallet: walletName, 50 PubKey: pubKey, 51 PassphraseFile: passphraseFilePath, 52 RawMetadata: []string{"name:my-wallet", "role:validation"}, 53 Clear: false, 54 } 55 56 expectedReq := api.AdminAnnotateKeyParams{ 57 Wallet: walletName, 58 PublicKey: pubKey, 59 Metadata: []wallet.Metadata{ 60 {Key: "name", Value: "my-wallet"}, 61 {Key: "role", Value: "validation"}, 62 }, 63 } 64 65 // when 66 req, passphrase, err := f.Validate() 67 68 // then 69 require.NoError(t, err) 70 require.NotNil(t, req) 71 assert.Equal(t, expectedReq, req) 72 assert.Equal(t, expectedPassphrase, passphrase) 73 } 74 75 func testAnnotateKeyFlagsMissingWalletFails(t *testing.T) { 76 testDir := t.TempDir() 77 78 // given 79 f := newAnnotateKeyFlags(t, testDir) 80 f.Wallet = "" 81 82 // when 83 req, _, err := f.Validate() 84 85 // then 86 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet")) 87 assert.Empty(t, req) 88 } 89 90 func testAnnotateKeyFlagsMissingPubKeyFails(t *testing.T) { 91 testDir := t.TempDir() 92 93 // given 94 f := newAnnotateKeyFlags(t, testDir) 95 f.PubKey = "" 96 97 // when 98 req, _, err := f.Validate() 99 100 // then 101 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("pubkey")) 102 assert.Empty(t, req) 103 } 104 105 func testAnnotateKeyFlagsMissingMetadataAndClearFails(t *testing.T) { 106 testDir := t.TempDir() 107 108 // given 109 f := newAnnotateKeyFlags(t, testDir) 110 f.RawMetadata = []string{} 111 112 // when 113 req, _, err := f.Validate() 114 115 // then 116 assert.ErrorIs(t, err, flags.OneOfFlagsMustBeSpecifiedError("meta", "clear")) 117 assert.Empty(t, req) 118 } 119 120 func testAnnotateKeyFlagsClearingWithMetadataFails(t *testing.T) { 121 testDir := t.TempDir() 122 123 // given 124 f := newAnnotateKeyFlags(t, testDir) 125 f.Clear = true 126 127 // when 128 req, _, err := f.Validate() 129 130 // then 131 assert.ErrorIs(t, err, flags.MutuallyExclusiveError("meta", "clear")) 132 assert.Empty(t, req) 133 } 134 135 func testAnnotateKeyFlagsInvalidMetadataFails(t *testing.T) { 136 testDir := t.TempDir() 137 138 // given 139 f := newAnnotateKeyFlags(t, testDir) 140 f.RawMetadata = []string{"is=invalid"} 141 142 // when 143 req, _, err := f.Validate() 144 145 // then 146 assert.ErrorIs(t, err, flags.InvalidFlagFormatError("meta")) 147 assert.Empty(t, req) 148 } 149 150 func newAnnotateKeyFlags(t *testing.T, testDir string) *cmd.AnnotateKeyFlags { 151 t.Helper() 152 153 _, passphraseFilePath := NewPassphraseFile(t, testDir) 154 walletName := vgrand.RandomStr(10) 155 pubKey := vgrand.RandomStr(20) 156 157 return &cmd.AnnotateKeyFlags{ 158 Wallet: walletName, 159 PubKey: pubKey, 160 PassphraseFile: passphraseFilePath, 161 RawMetadata: []string{"name:my-wallet", "role:validation"}, 162 Clear: false, 163 } 164 }