code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/permissions_describe_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 TestDescribePermissionsFlags(t *testing.T) { 31 t.Run("Valid flags succeeds", testDescribePermissionsValidFlagsSucceeds) 32 t.Run("Missing flags fails", testDescribePermissionsWithMissingFlagsFails) 33 } 34 35 func testDescribePermissionsValidFlagsSucceeds(t *testing.T) { 36 // given 37 testDir := t.TempDir() 38 walletName := vgrand.RandomStr(10) 39 hostname := vgrand.RandomStr(10) 40 expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir) 41 42 f := &cmd.DescribePermissionsFlags{ 43 Wallet: walletName, 44 Hostname: hostname, 45 PassphraseFile: passphraseFilePath, 46 } 47 48 expectedReq := api.AdminDescribePermissionsParams{ 49 Wallet: walletName, 50 Hostname: hostname, 51 } 52 // when 53 req, passphrase, err := f.Validate() 54 55 // then 56 require.NoError(t, err) 57 assert.Equal(t, expectedReq, req) 58 assert.Equal(t, expectedPassphrase, passphrase) 59 } 60 61 func testDescribePermissionsWithMissingFlagsFails(t *testing.T) { 62 testDir := t.TempDir() 63 walletName := vgrand.RandomStr(10) 64 hostname := vgrand.RandomStr(10) 65 _, passphraseFilePath := NewPassphraseFile(t, testDir) 66 67 tcs := []struct { 68 name string 69 flags *cmd.DescribePermissionsFlags 70 missingFlag string 71 }{ 72 { 73 name: "without hostname", 74 flags: &cmd.DescribePermissionsFlags{ 75 Wallet: walletName, 76 Hostname: "", 77 PassphraseFile: passphraseFilePath, 78 }, 79 missingFlag: "hostname", 80 }, { 81 name: "without wallet", 82 flags: &cmd.DescribePermissionsFlags{ 83 Wallet: "", 84 Hostname: hostname, 85 PassphraseFile: passphraseFilePath, 86 }, 87 missingFlag: "wallet", 88 }, 89 } 90 91 for _, tc := range tcs { 92 t.Run(tc.name, func(tt *testing.T) { 93 // when 94 req, _, err := tc.flags.Validate() 95 96 // then 97 assert.ErrorIs(t, err, flags.MustBeSpecifiedError(tc.missingFlag)) 98 assert.Empty(t, req) 99 }) 100 } 101 }