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