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

     1  // Copyright (c) 2019 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 account
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/ethereum/go-ethereum/accounts/keystore"
    12  	"github.com/golang/mock/gomock"
    13  	"github.com/iotexproject/iotex-address/address"
    14  	"github.com/pkg/errors"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/iotexproject/iotex-core/ioctl/config"
    18  	"github.com/iotexproject/iotex-core/ioctl/util"
    19  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    20  )
    21  
    22  func TestNewAccountExport(t *testing.T) {
    23  	require := require.New(t)
    24  	ctrl := gomock.NewController(t)
    25  	client := mock_ioctlclient.NewMockClient(ctrl)
    26  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    27  
    28  	testAccountFolder := t.TempDir()
    29  
    30  	ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP)
    31  	client.EXPECT().NewKeyStore().Return(ks).AnyTimes()
    32  
    33  	t.Run("true AliasIsHdwalletKey", func(t *testing.T) {
    34  		client.EXPECT().ReadSecret().Return("", nil).Times(1)
    35  		cmd := NewAccountExport(client)
    36  		_, err := util.ExecuteCmd(cmd, "hdw::1234")
    37  		require.Contains(err.Error(), "failed to get private key from keystore")
    38  	})
    39  
    40  	t.Run("false AliasIsHdwalletKey", func(t *testing.T) {
    41  		client.EXPECT().IsCryptoSm2().Return(false).Times(2)
    42  		acc, _ := ks.NewAccount("")
    43  		accAddr, _ := address.FromBytes(acc.Address.Bytes())
    44  		client.EXPECT().ReadSecret().Return("", nil).Times(1)
    45  		client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2)
    46  		cmd := NewAccountExport(client)
    47  		result, err := util.ExecuteCmd(cmd, "1234")
    48  		require.NoError(err)
    49  		require.Contains(result, accAddr.String())
    50  	})
    51  
    52  	t.Run("invalid address", func(t *testing.T) {
    53  		client.EXPECT().Address(gomock.Any()).Return("", errors.New("mock error")).Times(1)
    54  		cmd := NewAccountExport(client)
    55  		_, err := util.ExecuteCmd(cmd, "1234")
    56  		require.Contains(err.Error(), "failed to get address")
    57  	})
    58  }