github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didgenerate_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 did
     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/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
    15  	"github.com/pkg/errors"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/iotexproject/iotex-core/ioctl/config"
    19  	"github.com/iotexproject/iotex-core/ioctl/util"
    20  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    21  )
    22  
    23  func TestNewDidGenerateCmd(t *testing.T) {
    24  	require := require.New(t)
    25  	ctrl := gomock.NewController(t)
    26  	defer ctrl.Finish()
    27  	client := mock_ioctlclient.NewMockClient(ctrl)
    28  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    29  	passwd := "123456"
    30  
    31  	ks := keystore.NewKeyStore(t.TempDir(), 2, 1)
    32  	acc, err := ks.NewAccount(passwd)
    33  	require.NoError(err)
    34  	accAddr, err := address.FromBytes(acc.Address.Bytes())
    35  	require.NoError(err)
    36  
    37  	client.EXPECT().SelectTranslation(gomock.Any()).Return("did", config.English).AnyTimes()
    38  	client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(3)
    39  
    40  	t.Run("failed to get password", func(t *testing.T) {
    41  		expectedErr := errors.New("failed to get password")
    42  		client.EXPECT().ReadSecret().Return("", expectedErr)
    43  		cmd := NewDidGenerateCmd(client)
    44  		_, err := util.ExecuteCmd(cmd)
    45  		require.Contains(err.Error(), expectedErr.Error())
    46  	})
    47  
    48  	client.EXPECT().ReadSecret().Return(passwd, nil).Times(2)
    49  	client.EXPECT().IsCryptoSm2().Return(false).Times(3)
    50  	client.EXPECT().NewKeyStore().Return(ks).Times(3)
    51  	client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes()
    52  
    53  	t.Run("generate did", func(t *testing.T) {
    54  		client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil)
    55  		cmd := NewDidGenerateCmd(client)
    56  		result, err := util.ExecuteCmd(cmd, "--signer", accAddr.String())
    57  		require.NoError(err)
    58  		require.Contains(result, acc.Address.Hex())
    59  	})
    60  
    61  	t.Run("failed to get private key from signer", func(t *testing.T) {
    62  		expectedErr := errors.New("failed to get private key from signer")
    63  		client.EXPECT().Address(gomock.Any()).Return("", expectedErr)
    64  		cmd := NewDidGenerateCmd(client)
    65  		_, err := util.ExecuteCmd(cmd, "--signer", accAddr.String())
    66  		require.Contains(err.Error(), expectedErr.Error())
    67  	})
    68  
    69  	t.Run("failed to get signer addr", func(t *testing.T) {
    70  		expectedErr := errors.New("failed to get signer addr")
    71  		client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr)
    72  		cmd := NewDidGenerateCmd(client)
    73  		_, err := util.ExecuteCmd(cmd)
    74  		require.Contains(err.Error(), expectedErr.Error())
    75  	})
    76  }