github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountethaddr_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 account
     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/identityset"
    18  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    19  )
    20  
    21  func TestNewAccountEthAddr(t *testing.T) {
    22  	require := require.New(t)
    23  	ctrl := gomock.NewController(t)
    24  	defer ctrl.Finish()
    25  	client := mock_ioctlclient.NewMockClient(ctrl)
    26  	client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes()
    27  
    28  	t.Run("when an iotex address was given", func(t *testing.T) {
    29  		client.EXPECT().Address(gomock.Any()).Return(identityset.Address(28).String(), nil)
    30  		cmd := NewAccountEthAddr(client)
    31  		result, err := util.ExecuteCmd(cmd, "io187evpmjdankjh0g5dfz83w2z3p23ljhn4s9jw7")
    32  		require.NoError(err)
    33  		require.Contains(result, identityset.Address(28).String())
    34  	})
    35  
    36  	t.Run("when an ethereum address was given", func(t *testing.T) {
    37  		client.EXPECT().Address(gomock.Any()).Return(identityset.Address(28).String(), nil)
    38  		cmd := NewAccountEthAddr(client)
    39  		result, err := util.ExecuteCmd(cmd, "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5")
    40  		require.NoError(err)
    41  		require.Contains(result, identityset.Address(28).String())
    42  	})
    43  
    44  	t.Run("cannot find address for alias", func(t *testing.T) {
    45  		expectedErr := errors.New("cannot find address for alias ")
    46  		client.EXPECT().Address(gomock.Any()).Return("", expectedErr)
    47  		cmd := NewAccountEthAddr(client)
    48  		_, err := util.ExecuteCmd(cmd, "")
    49  		require.Contains(err.Error(), expectedErr.Error())
    50  	})
    51  }