github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountlist_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 "errors" 10 "testing" 11 12 "github.com/ethereum/go-ethereum/accounts/keystore" 13 "github.com/golang/mock/gomock" 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 TestNewAccountList(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 t.Run("When NewAccountList returns no error", func(t *testing.T) { 29 client.EXPECT().IsCryptoSm2().Return(false) 30 testAccountFolder := t.TempDir() 31 32 ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) 33 genAccount := func(passwd string) string { 34 account, err := ks.NewAccount(passwd) 35 require.NoError(err) 36 addr, err := address.FromBytes(account.Address.Bytes()) 37 require.NoError(err) 38 return addr.String() 39 } 40 addra := genAccount("test1") 41 addrb := genAccount("test2") 42 client.EXPECT().NewKeyStore().Return(ks) 43 client.EXPECT().AliasMap().Return(map[string]string{ 44 addra: "a", 45 addrb: "b", 46 }).Times(2) 47 48 cmd := NewAccountList(client) 49 result, err := util.ExecuteCmd(cmd) 50 require.NoError(err) 51 require.Contains(result, addra+" - a") 52 require.Contains(result, addrb+" - b") 53 }) 54 55 t.Run("When NewAccountList returns error", func(t *testing.T) { 56 client.EXPECT().IsCryptoSm2().Return(true) 57 client.EXPECT().Config().Return(config.Config{}).Times(1) 58 expectedErr := errors.New("failed to get sm2 accounts") 59 60 cmd := NewAccountList(client) 61 _, err := util.ExecuteCmd(cmd) 62 require.Error(err) 63 require.Contains(err.Error(), expectedErr.Error()) 64 }) 65 }