github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/hdwallet/hdwalletcreate_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 TestNewHdwalletCreateCmd(t *testing.T) {
    21  	require := require.New(t)
    22  	ctrl := gomock.NewController(t)
    23  	client := mock_ioctlclient.NewMockClient(ctrl)
    24  	password := "123"
    25  
    26  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    27  	client.EXPECT().IsHdWalletConfigFileExist().Return(false).Times(3)
    28  
    29  	t.Run("create hdwallet", func(t *testing.T) {
    30  		client.EXPECT().ReadSecret().Return(password, nil).Times(2)
    31  		client.EXPECT().WriteHdWalletConfigFile(gomock.Any(), gomock.Any()).Return(nil)
    32  
    33  		cmd := NewHdwalletCreateCmd(client)
    34  		result, err := util.ExecuteCmd(cmd)
    35  		require.NoError(err)
    36  		require.Contains(result, "It is used to recover your wallet in case you forgot the password. Write them down and store it in a safe place.")
    37  	})
    38  
    39  	t.Run("failed to get password", func(t *testing.T) {
    40  		expectedErr := errors.New("failed to get password")
    41  		client.EXPECT().ReadSecret().Return("", expectedErr)
    42  
    43  		cmd := NewHdwalletCreateCmd(client)
    44  		_, err := util.ExecuteCmd(cmd)
    45  		require.Contains(err.Error(), expectedErr.Error())
    46  	})
    47  
    48  	t.Run("password not match", func(t *testing.T) {
    49  		expectedErr := errors.New("password doesn't match")
    50  		client.EXPECT().ReadSecret().Return(password, nil)
    51  		client.EXPECT().ReadSecret().Return("test", nil)
    52  
    53  		cmd := NewHdwalletCreateCmd(client)
    54  		_, err := util.ExecuteCmd(cmd)
    55  		require.Equal(err.Error(), expectedErr.Error())
    56  	})
    57  }