github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountactions_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 "bytes" 10 "encoding/json" 11 "fmt" 12 "io" 13 "net/http" 14 "strconv" 15 "strings" 16 "testing" 17 18 "github.com/golang/mock/gomock" 19 "github.com/golang/protobuf/ptypes/timestamp" 20 "github.com/pkg/errors" 21 "github.com/stretchr/testify/require" 22 23 "github.com/iotexproject/iotex-core/ioctl/config" 24 "github.com/iotexproject/iotex-core/ioctl/util" 25 "github.com/iotexproject/iotex-core/test/identityset" 26 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 27 ) 28 29 func TestNewAccountAction(t *testing.T) { 30 require := require.New(t) 31 ctrl := gomock.NewController(t) 32 defer ctrl.Finish() 33 client := mock_ioctlclient.NewMockClient(ctrl) 34 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", 35 config.English).AnyTimes() 36 accAddr := identityset.Address(28).String() 37 38 t.Run("empty offset", func(t *testing.T) { 39 cmd := NewAccountActions(client) 40 _, err := util.ExecuteCmd(cmd, accAddr, "") 41 require.Error(err) 42 require.Contains(err.Error(), "failed to convert skip") 43 }) 44 45 t.Run("failed to send request", func(t *testing.T) { 46 client.EXPECT().QueryAnalyser(gomock.Any()).Return(nil, errors.New("failed to send request")).Times(1) 47 client.EXPECT().Address(gomock.Any()).Return(accAddr, nil) 48 cmd := NewAccountActions(client) 49 _, err := util.ExecuteCmd(cmd, accAddr, "0") 50 require.Error(err) 51 require.Contains(err.Error(), "failed to send request") 52 }) 53 54 t.Run("get account action", func(t *testing.T) { 55 client.EXPECT().Address(gomock.Any()).Return(accAddr, nil).Times(2) 56 57 reqData := map[string]string{ 58 "address": accAddr, 59 "offset": fmt.Sprint(0), 60 } 61 62 client.EXPECT().QueryAnalyser(reqData).DoAndReturn(func(reqData interface{}) (*http.Response, error) { 63 jsonData, err := json.Marshal(reqData) 64 require.NoError(err) 65 resp, err := http.Post("https://url.com", "application/json", bytes.NewBuffer(jsonData)) 66 require.NoError(err) 67 timestamp := strconv.Itoa(int(timestamp.Timestamp{Seconds: 10, Nanos: 10}.Seconds)) 68 sender := reqData.(map[string]string)["address"] 69 testData := ` 70 { 71 "Count": "1", 72 "Results": [{ 73 "ActHash": "9b1d77d8b8902e8d4e662e7cd07d8a74179e032f030d92441ca7fba1ca68e0f4", 74 "Timestamp": "%s", 75 "BlkHeight": "1", 76 "RecordType": "1", 77 "ActType": "1", 78 "Sender": "%s", 79 "Recipient": "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", 80 "Amount": "0.020000000132432" 81 }] 82 } 83 ` 84 testData = fmt.Sprintf(testData, timestamp, sender) 85 stringReader := strings.NewReader(testData) 86 stringReadCloser := io.NopCloser(stringReader) 87 resp.Body = stringReadCloser 88 return resp, nil 89 }).Times(2) 90 cmd := NewAccountActions(client) 91 result, err := util.ExecuteCmd(cmd, accAddr, "0") 92 require.NoError(err) 93 require.Contains(result, "Total") 94 result, err = util.ExecuteCmd(cmd, accAddr) 95 require.NoError(err) 96 require.Contains(result, "Total") 97 }) 98 99 t.Run("empty address", func(t *testing.T) { 100 client.EXPECT().QueryAnalyser(gomock.Any()).DoAndReturn(func(reqData interface{}) (*http.Response, error) { 101 jsonData, err := json.Marshal(reqData) 102 require.NoError(err) 103 resp, err := http.Post("https://url.com", "application/json", bytes.NewBuffer(jsonData)) 104 require.NoError(err) 105 return resp, nil 106 }).Times(1) 107 client.EXPECT().Address(gomock.Any()).Return("", nil) 108 cmd := NewAccountActions(client) 109 _, err := util.ExecuteCmd(cmd, "", "0") 110 require.Error(err) 111 require.Contains(err.Error(), "failed to deserialize the response") 112 }) 113 }