code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/api_token_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/service/v2/connections"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestGenerateAPITokenFlags(t *testing.T) {
    31  	t.Run("Valid flags succeeds", testGenerateAPITokenValidFlagsSucceeds)
    32  	t.Run("Missing flags fails", testGenerateAPITokenWithMissingFlagsFails)
    33  }
    34  
    35  func testGenerateAPITokenValidFlagsSucceeds(t *testing.T) {
    36  	// given
    37  	testDir := t.TempDir()
    38  	description := vgrand.RandomStr(10)
    39  	wallet := vgrand.RandomStr(10)
    40  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
    41  	walletPassphrase, walletPassphraseFilePath := NewPassphraseFile(t, testDir)
    42  
    43  	f := &cmd.GenerateAPITokenFlags{
    44  		Description:          description,
    45  		PassphraseFile:       passphraseFilePath,
    46  		WalletName:           wallet,
    47  		WalletPassphraseFile: walletPassphraseFilePath,
    48  	}
    49  
    50  	expectedReq := connections.GenerateAPITokenParams{
    51  		Description: description,
    52  		Wallet: connections.GenerateAPITokenWalletParams{
    53  			Name:       wallet,
    54  			Passphrase: walletPassphrase,
    55  		},
    56  	}
    57  	// when
    58  	req, err := f.Validate()
    59  
    60  	// then
    61  	require.NoError(t, err)
    62  	assert.EqualValues(t, expectedReq, req)
    63  }
    64  
    65  func testGenerateAPITokenWithMissingFlagsFails(t *testing.T) {
    66  	testDir := t.TempDir()
    67  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
    68  	_, walletPassphraseFilePath := NewPassphraseFile(t, testDir)
    69  
    70  	tcs := []struct {
    71  		name        string
    72  		flags       *cmd.GenerateAPITokenFlags
    73  		missingFlag string
    74  	}{
    75  		{
    76  			name: "without wallet name",
    77  			flags: &cmd.GenerateAPITokenFlags{
    78  				PassphraseFile:       passphraseFilePath,
    79  				WalletPassphraseFile: walletPassphraseFilePath,
    80  			},
    81  			missingFlag: "wallet-name",
    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  			assert.Empty(t, req)
    93  		})
    94  	}
    95  }