github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/hdwallet/hdwalletimport_test.go (about)

     1  // Copyright (c) 2022 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package hdwallet
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/util"
    17  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    18  )
    19  
    20  func TestNewHdwalletImportCmd(t *testing.T) {
    21  	require := require.New(t)
    22  	ctrl := gomock.NewController(t)
    23  	client := mock_ioctlclient.NewMockClient(ctrl)
    24  
    25  	mnemonic := "lake stove quarter shove dry matrix hire split wide attract argue core"
    26  	password := "123"
    27  
    28  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    29  	client.EXPECT().IsHdWalletConfigFileExist().Return(false).Times(2)
    30  
    31  	t.Run("import hdwallet", func(t *testing.T) {
    32  		client.EXPECT().ReadInput().Return(mnemonic, nil)
    33  		client.EXPECT().ReadSecret().Return(password, nil)
    34  		client.EXPECT().ReadSecret().Return(password, nil)
    35  		client.EXPECT().WriteHdWalletConfigFile(gomock.Any(), gomock.Any()).Return(nil)
    36  
    37  		cmd := NewHdwalletImportCmd(client)
    38  		result, err := util.ExecuteCmd(cmd)
    39  		require.NoError(err)
    40  		require.Contains(result, mnemonic)
    41  	})
    42  
    43  	t.Run("failed to write to config file", func(t *testing.T) {
    44  		expectErr := errors.New("failed to write to config file")
    45  		client.EXPECT().ReadInput().Return(mnemonic, nil)
    46  		client.EXPECT().ReadSecret().Return(password, nil)
    47  		client.EXPECT().ReadSecret().Return(password, nil)
    48  		client.EXPECT().WriteHdWalletConfigFile(gomock.Any(), gomock.Any()).Return(expectErr)
    49  
    50  		cmd := NewHdwalletImportCmd(client)
    51  		_, err := util.ExecuteCmd(cmd)
    52  		require.Contains(err.Error(), expectErr.Error())
    53  	})
    54  }