github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountexportpublic_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/pkg/errors" 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 TestNewAccountExportPublic(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 testAccountFolder := t.TempDir() 29 ks := keystore.NewKeyStore(testAccountFolder, veryLightScryptN, veryLightScryptP) 30 client.EXPECT().NewKeyStore().Return(ks).AnyTimes() 31 32 t.Run("true AliasIsHdwalletKey", func(t *testing.T) { 33 client.EXPECT().ReadSecret().Return("", nil).Times(1) 34 cmd := NewAccountExportPublic(client) 35 _, err := util.ExecuteCmd(cmd, "hdw::1234") 36 require.Contains(err.Error(), "failed to get private key from keystore") 37 }) 38 39 t.Run("false AliasIsHdwalletKey", func(t *testing.T) { 40 client.EXPECT().IsCryptoSm2().Return(false).Times(2) 41 acc, err := ks.NewAccount("") 42 require.NoError(err) 43 accAddr, err := address.FromBytes(acc.Address.Bytes()) 44 require.NoError(err) 45 client.EXPECT().ReadSecret().Return("", nil).Times(1) 46 client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2) 47 cmd := NewAccountExportPublic(client) 48 result, err := util.ExecuteCmd(cmd, "1234") 49 require.NoError(err) 50 require.Contains(result, accAddr.String()) 51 }) 52 53 t.Run("invalid address", func(t *testing.T) { 54 client.EXPECT().Address(gomock.Any()).Return("", errors.New("mock error")).Times(1) 55 cmd := NewAccountExportPublic(client) 56 _, err := util.ExecuteCmd(cmd, "1234") 57 require.Contains(err.Error(), "failed to get address") 58 }) 59 }