github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountdelete_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/go-pkgs/crypto"
    14  	"github.com/iotexproject/iotex-address/address"
    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 TestNewAccountDelete(t *testing.T) {
    23  	require := require.New(t)
    24  	ctrl := gomock.NewController(t)
    25  	defer ctrl.Finish()
    26  	client := mock_ioctlclient.NewMockClient(ctrl)
    27  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString",
    28  		config.English).Times(30)
    29  
    30  	testAccountFolder := t.TempDir()
    31  
    32  	t.Run("CryptoSm2 is false", func(t *testing.T) {
    33  		client.EXPECT().IsCryptoSm2().Return(false).Times(2)
    34  		ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP)
    35  		acc, _ := ks.NewAccount("test")
    36  		accAddr, _ := address.FromBytes(acc.Address.Bytes())
    37  		client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(2)
    38  		client.EXPECT().NewKeyStore().Return(ks).Times(2)
    39  
    40  		client.EXPECT().AliasMap().Return(map[string]string{
    41  			accAddr.String(): "aaa",
    42  			"io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx": "bbb",
    43  			"io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1": "ccc",
    44  		})
    45  
    46  		client.EXPECT().AskToConfirm(gomock.Any()).Return(false, nil)
    47  		cmd := NewAccountDelete(client)
    48  		_, err := util.ExecuteCmd(cmd)
    49  		require.NoError(err)
    50  
    51  		client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil)
    52  		client.EXPECT().DeleteAlias("aaa").Return(nil)
    53  		cmd = NewAccountDelete(client)
    54  		result, err := util.ExecuteCmd(cmd)
    55  		require.NoError(err)
    56  		require.Contains(result, accAddr.String())
    57  	})
    58  
    59  	t.Run("CryptoSm2 is true", func(t *testing.T) {
    60  		client.EXPECT().IsCryptoSm2().Return(true).Times(1)
    61  		priKey2, _ := crypto.GenerateKeySm2()
    62  		addr2 := priKey2.PublicKey().Address()
    63  
    64  		client.EXPECT().AliasMap().Return(map[string]string{
    65  			addr2.String(): "aaa",
    66  			"io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx": "bbb",
    67  			"io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1": "ccc",
    68  		})
    69  
    70  		cfg := config.Config{
    71  			Wallet: testAccountFolder,
    72  			Aliases: map[string]string{
    73  				"aaa": addr2.String(),
    74  				"bbb": "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx",
    75  				"ccc": "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1",
    76  			},
    77  		}
    78  		client.EXPECT().Config().Return(cfg).Times(2)
    79  
    80  		pemFilePath := sm2KeyPath(client, addr2)
    81  		crypto.WritePrivateKeyToPem(pemFilePath, priKey2.(*crypto.P256sm2PrvKey), "test")
    82  		client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(addr2.String(), nil)
    83  
    84  		client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil)
    85  		client.EXPECT().DeleteAlias("aaa").Return(nil)
    86  		cmd := NewAccountDelete(client)
    87  		result, err := util.ExecuteCmd(cmd)
    88  		require.NoError(err)
    89  		require.Contains(result, addr2.String())
    90  	})
    91  }