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

     1  // Copyright (c) 2022 IoTeX Foundation
     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 TestNewHdwalletDeriveCmd(t *testing.T) {
    21  	require := require.New(t)
    22  	ctrl := gomock.NewController(t)
    23  	client := mock_ioctlclient.NewMockClient(ctrl)
    24  	mnemonic := "lake stove quarter shove dry matrix hire split wide attract argue core"
    25  
    26  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    27  	client.EXPECT().ReadSecret().Return("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", nil).Times(2)
    28  
    29  	t.Run("get address from hdwallet", func(t *testing.T) {
    30  		client.EXPECT().HdwalletMnemonic(gomock.Any()).Return(mnemonic, nil)
    31  
    32  		cmd := NewHdwalletDeriveCmd(client)
    33  		result, err := util.ExecuteCmd(cmd, "1/1/2")
    34  		require.NoError(err)
    35  		require.Contains(result, "address: io1pt2wml6v2tx683ctjn3k9mfgq97zx4c7rzwfuf")
    36  	})
    37  
    38  	t.Run("invalid hdwallet key format", func(t *testing.T) {
    39  		expectedErr := errors.New("invalid hdwallet key format")
    40  
    41  		cmd := NewHdwalletDeriveCmd(client)
    42  		_, err := util.ExecuteCmd(cmd, "1")
    43  		require.Contains(err.Error(), expectedErr.Error())
    44  	})
    45  
    46  	t.Run("failed to export mnemonic", func(t *testing.T) {
    47  		expectedErr := errors.New("failed to export mnemonic")
    48  		client.EXPECT().HdwalletMnemonic(gomock.Any()).Return("", expectedErr)
    49  
    50  		cmd := NewHdwalletDeriveCmd(client)
    51  		_, err := util.ExecuteCmd(cmd, "1/1/2")
    52  		require.Contains(err.Error(), expectedErr.Error())
    53  	})
    54  }