github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountinfo_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 account 7 8 import ( 9 "encoding/hex" 10 "encoding/json" 11 "testing" 12 13 "github.com/golang/mock/gomock" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi" 15 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 16 "github.com/iotexproject/iotex-proto/golang/iotextypes" 17 "github.com/pkg/errors" 18 "github.com/stretchr/testify/require" 19 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/test/identityset" 23 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 24 ) 25 26 func TestNewAccountInfo(t *testing.T) { 27 require := require.New(t) 28 ctrl := gomock.NewController(t) 29 defer ctrl.Finish() 30 client := mock_ioctlclient.NewMockClient(ctrl) 31 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 32 33 accAddr := identityset.Address(28) 34 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).AnyTimes() 35 client.EXPECT().Config().Return(config.Config{}).AnyTimes() 36 37 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 38 accountResponse := &iotexapi.GetAccountResponse{AccountMeta: &iotextypes.AccountMeta{ 39 Address: accAddr.String(), 40 Balance: "20000000132432000", 41 PendingNonce: uint64(1), 42 NumActions: uint64(2), 43 IsContract: true, 44 ContractByteCode: []byte("60806040526101f4600055603260015534801561001b57600080fd5b506102558061002b6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806358931c461461003b5780637f353d5514610045575b600080fd5b61004361004f565b005b61004d610097565b005b60006001905060005b6000548110156100935760028261006f9190610114565b915060028261007e91906100e3565b9150808061008b90610178565b915050610058565b5050565b60005b6001548110156100e057600281908060018154018082558091505060019003906000526020600020016000909190919091505580806100d890610178565b91505061009a565b50565b60006100ee8261016e565b91506100f98361016e565b925082610109576101086101f0565b5b828204905092915050565b600061011f8261016e565b915061012a8361016e565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0483118215151615610163576101626101c1565b5b828202905092915050565b6000819050919050565b60006101838261016e565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156101b6576101b56101c1565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fdfea2646970667358221220cb9cada3f1d447c978af17aa3529d6fe4f25f9c5a174085443e371b6940ae99b64736f6c63430008070033"), 45 }} 46 47 t.Run("retrieve account information successfully", func(t *testing.T) { 48 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil) 49 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil) 50 51 cmd := NewAccountInfo(client) 52 result, err := util.ExecuteCmd(cmd, "io187evpmjdankjh0g5dfz83w2z3p23ljhn4s9jw7") 53 require.NoError(err) 54 var info infoMessage 55 require.NoError(json.Unmarshal([]byte(result), &info)) 56 require.Equal(accAddr.String(), info.Address) 57 require.Equal(accountResponse.AccountMeta.PendingNonce, uint64(info.PendingNonce)) 58 require.Equal(accountResponse.AccountMeta.NumActions, uint64(info.NumActions)) 59 require.Equal(info.ContractByteCode, hex.EncodeToString(accountResponse.AccountMeta.ContractByteCode)) 60 }) 61 62 t.Run("failed to invoke GetAccount api", func(t *testing.T) { 63 expectedErr := errors.New("failed to dial grpc connection") 64 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil) 65 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 66 67 cmd := NewAccountInfo(client) 68 _, err := util.ExecuteCmd(cmd, "io187evpmjdankjh0g5dfz83w2z3p23ljhn4s9jw7") 69 require.Contains(err.Error(), expectedErr.Error()) 70 }) 71 72 t.Run("invalid account balance", func(t *testing.T) { 73 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil) 74 accountResponse.AccountMeta.Balance = "61xx44" 75 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil) 76 77 cmd := NewAccountInfo(client) 78 _, err := util.ExecuteCmd(cmd, "io187evpmjdankjh0g5dfz83w2z3p23ljhn4s9jw7") 79 require.Error(err) 80 }) 81 }