github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountsign_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/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    19  )
    20  
    21  func TestNewAccountSign(t *testing.T) {
    22  	require := require.New(t)
    23  	ctrl := gomock.NewController(t)
    24  	client := mock_ioctlclient.NewMockClient(ctrl)
    25  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    26  
    27  	testAccountFolder := t.TempDir()
    28  	ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP)
    29  	client.EXPECT().NewKeyStore().Return(ks).AnyTimes()
    30  
    31  	t.Run("invalid_account", func(t *testing.T) {
    32  		client.EXPECT().IsCryptoSm2().Return(false).Times(2)
    33  		client.EXPECT().Address(gomock.Any()).Return("io1rc2d2de7rtuucalsqv4d9ng0h297t63w7wvlph", nil).Times(1)
    34  		cmd := NewAccountSign(client)
    35  		require.NoError(cmd.Flag("signer").Value.Set("io1rc2d2de7rtuucalsqv4d9ng0h297t63w7wvlph"))
    36  		_, err := util.ExecuteCmd(cmd, "1234")
    37  		require.Equal("failed to sign message: invalid address #io1rc2d2de7rtuucalsqv4d9ng0h297t63w7wvlph", err.Error())
    38  	})
    39  
    40  	t.Run("valid_account", func(t *testing.T) {
    41  		client.EXPECT().IsCryptoSm2().Return(false).Times(1)
    42  		const pwd = "test"
    43  		acc, _ := ks.NewAccount(pwd)
    44  		accAddr, _ := address.FromBytes(acc.Address.Bytes())
    45  		client.EXPECT().ReadSecret().Return(pwd, nil).Times(1)
    46  		client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2)
    47  		cmd := NewAccountSign(client)
    48  		require.NoError(cmd.Flag("signer").Value.Set(accAddr.String()))
    49  		result, err := util.ExecuteCmd(cmd, "1234")
    50  		require.NoError(err)
    51  		require.Contains(result, accAddr.String())
    52  	})
    53  }