code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/network_import_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 TestImportNetworkFlags(t *testing.T) {
    31  	t.Run("Valid flags with URL succeeds", testImportNetworkFlagsValidFlagsWithURLSucceeds)
    32  	t.Run("Valid flags with file path succeeds", testImportNetworkFlagsValidFlagsWithFilePathSucceeds)
    33  	t.Run("Missing URL and file path fails", testImportNetworkFlagsMissingURLAndFilePathFails)
    34  	t.Run("Both URL and filePath specified", testImportNetworkFlagsBothURLAndFilePathSpecifiedFails)
    35  }
    36  
    37  func testImportNetworkFlagsValidFlagsWithURLSucceeds(t *testing.T) {
    38  	// given
    39  	networkName := vgrand.RandomStr(10)
    40  	url := vgrand.RandomStr(20)
    41  
    42  	f := &cmd.ImportNetworkFlags{
    43  		Name:  networkName,
    44  		URL:   url,
    45  		Force: true,
    46  	}
    47  
    48  	expectedReq := api.AdminImportNetworkParams{
    49  		Name:      networkName,
    50  		URL:       url,
    51  		Overwrite: true,
    52  	}
    53  
    54  	// when
    55  	req, err := f.Validate()
    56  
    57  	// then
    58  	require.NoError(t, err)
    59  	require.NotNil(t, req)
    60  	assert.Equal(t, expectedReq, req)
    61  }
    62  
    63  func testImportNetworkFlagsValidFlagsWithFilePathSucceeds(t *testing.T) {
    64  	// given
    65  	networkName := vgrand.RandomStr(10)
    66  	filePath := vgrand.RandomStr(20)
    67  
    68  	f := &cmd.ImportNetworkFlags{
    69  		Name:     networkName,
    70  		FilePath: filePath,
    71  		Force:    true,
    72  	}
    73  
    74  	expectedReq := api.AdminImportNetworkParams{
    75  		Name:      networkName,
    76  		URL:       api.FileSchemePrefix + filePath,
    77  		Overwrite: true,
    78  	}
    79  
    80  	// when
    81  	req, err := f.Validate()
    82  
    83  	// then
    84  	require.NoError(t, err)
    85  	require.NotNil(t, req)
    86  	assert.Equal(t, expectedReq, req)
    87  }
    88  
    89  func testImportNetworkFlagsMissingURLAndFilePathFails(t *testing.T) {
    90  	// given
    91  	f := newImportNetworkFlags(t)
    92  	f.URL = ""
    93  	f.FilePath = ""
    94  
    95  	// when
    96  	req, err := f.Validate()
    97  
    98  	// then
    99  	assert.ErrorIs(t, err, flags.OneOfFlagsMustBeSpecifiedError("from-file", "from-url"))
   100  	assert.Empty(t, req)
   101  }
   102  
   103  func testImportNetworkFlagsBothURLAndFilePathSpecifiedFails(t *testing.T) {
   104  	// given
   105  	f := newImportNetworkFlags(t)
   106  	f.URL = vgrand.RandomStr(20)
   107  	f.FilePath = vgrand.RandomStr(20)
   108  
   109  	// when
   110  	req, err := f.Validate()
   111  
   112  	// then
   113  	assert.ErrorIs(t, err, flags.MutuallyExclusiveError("from-file", "from-url"))
   114  	assert.Empty(t, req)
   115  }
   116  
   117  func newImportNetworkFlags(t *testing.T) *cmd.ImportNetworkFlags {
   118  	t.Helper()
   119  
   120  	networkName := vgrand.RandomStr(10)
   121  
   122  	return &cmd.ImportNetworkFlags{
   123  		Name:  networkName,
   124  		Force: true,
   125  	}
   126  }