code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/key_generate_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 TestGenerateKeyFlags(t *testing.T) { 32 t.Run("Valid flags succeeds", testGenerateKeyFlagsValidFlagsSucceeds) 33 t.Run("Missing wallet fails", testGenerateKeyFlagsMissingWalletFails) 34 t.Run("Invalid metadata fails", testGenerateKeyFlagsInvalidMetadataFails) 35 } 36 37 func testGenerateKeyFlagsValidFlagsSucceeds(t *testing.T) { 38 // given 39 testDir := t.TempDir() 40 walletName := vgrand.RandomStr(10) 41 expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir) 42 43 f := &cmd.GenerateKeyFlags{ 44 Wallet: walletName, 45 PassphraseFile: passphraseFilePath, 46 RawMetadata: []string{"name:my-wallet", "role:validation"}, 47 } 48 49 expectedReq := api.AdminGenerateKeyParams{ 50 Wallet: walletName, 51 Metadata: []wallet.Metadata{ 52 {Key: "name", Value: "my-wallet"}, 53 {Key: "role", Value: "validation"}, 54 }, 55 } 56 57 // when 58 req, passphrase, err := f.Validate() 59 60 // then 61 require.NoError(t, err) 62 require.NotNil(t, req) 63 assert.Equal(t, expectedReq, req) 64 assert.Equal(t, expectedPassphrase, passphrase) 65 } 66 67 func testGenerateKeyFlagsMissingWalletFails(t *testing.T) { 68 // given 69 f := newGenerateKeyFlags(t) 70 f.Wallet = "" 71 72 // when 73 req, _, err := f.Validate() 74 75 // then 76 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet")) 77 assert.Empty(t, req) 78 } 79 80 func testGenerateKeyFlagsInvalidMetadataFails(t *testing.T) { 81 // given 82 f := newGenerateKeyFlags(t) 83 f.RawMetadata = []string{"is=invalid"} 84 85 // when 86 req, _, err := f.Validate() 87 88 // then 89 assert.ErrorIs(t, err, flags.InvalidFlagFormatError("meta")) 90 assert.Empty(t, req) 91 } 92 93 func newGenerateKeyFlags(t *testing.T) *cmd.GenerateKeyFlags { 94 t.Helper() 95 testDir := t.TempDir() 96 _, passphraseFilePath := NewPassphraseFile(t, testDir) 97 return &cmd.GenerateKeyFlags{ 98 Wallet: vgrand.RandomStr(5), 99 PassphraseFile: passphraseFilePath, 100 RawMetadata: []string{"name:my-wallet", "role:validation"}, 101 } 102 }