code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/api_token_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  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestDescribeAPITokenFlags(t *testing.T) {
    30  	t.Run("Valid flags succeeds", testDescribeAPITokenValidFlagsSucceeds)
    31  	t.Run("Missing flags fails", testDescribeAPITokenWithMissingFlagsFails)
    32  }
    33  
    34  func testDescribeAPITokenValidFlagsSucceeds(t *testing.T) {
    35  	// given
    36  	testDir := t.TempDir()
    37  	token := vgrand.RandomStr(10)
    38  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
    39  
    40  	f := &cmd.DescribeAPITokenFlags{
    41  		Token:          token,
    42  		PassphraseFile: passphraseFilePath,
    43  	}
    44  
    45  	// when
    46  	err := f.Validate()
    47  
    48  	// then
    49  	require.NoError(t, err)
    50  }
    51  
    52  func testDescribeAPITokenWithMissingFlagsFails(t *testing.T) {
    53  	testDir := t.TempDir()
    54  	_, passphraseFilePath := NewPassphraseFile(t, testDir)
    55  
    56  	tcs := []struct {
    57  		name        string
    58  		flags       *cmd.DescribeAPITokenFlags
    59  		missingFlag string
    60  	}{
    61  		{
    62  			name: "without token",
    63  			flags: &cmd.DescribeAPITokenFlags{
    64  				PassphraseFile: passphraseFilePath,
    65  			},
    66  			missingFlag: "token",
    67  		},
    68  	}
    69  
    70  	for _, tc := range tcs {
    71  		t.Run(tc.name, func(tt *testing.T) {
    72  			// when
    73  			err := tc.flags.Validate()
    74  
    75  			// then
    76  			assert.ErrorIs(t, err, flags.MustBeSpecifiedError(tc.missingFlag))
    77  		})
    78  	}
    79  }