-
Notifications
You must be signed in to change notification settings - Fork 324
/
action_test.go
163 lines (134 loc) · 5.67 KB
/
action_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package action
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func TestSigner(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
t.Run("returns signer's address", func(t *testing.T) {
client.EXPECT().Config().Return(config.Config{
DefaultAccount: config.Context{
AddressOrAlias: "test",
},
})
client.EXPECT().Address(gomock.Any()).Return("test", nil)
result, err := Signer(client)
require.NoError(err)
require.Equal(result, "test")
})
t.Run("use 'ioctl config set defaultacc ADDRESS|ALIAS' to config default account first", func(t *testing.T) {
expectedErr := errors.New("use 'ioctl config set defaultacc ADDRESS|ALIAS' to config default account first")
client.EXPECT().Config().Return(config.Config{})
_, err := Signer(client)
require.Equal(err.Error(), expectedErr.Error())
})
}
func TestSendRaw(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
selp := &iotextypes.Action{}
response := &iotexapi.SendActionResponse{}
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(4)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(response, nil).Times(3)
t.Run("sends raw action to blockchain", func(t *testing.T) {
t.Run("endpoint iotexscan", func(t *testing.T) {
client.EXPECT().Config().Return(config.Config{
Explorer: "iotexscan",
Endpoint: "testnet1",
}).Times(2)
err := SendRaw(client, selp)
require.NoError(err)
})
t.Run("endpoint iotxplorer", func(t *testing.T) {
client.EXPECT().Config().Return(config.Config{
Explorer: "iotxplorer",
})
err := SendRaw(client, selp)
require.NoError(err)
})
t.Run("endpoint default", func(t *testing.T) {
client.EXPECT().Config().Return(config.Config{
Explorer: "test",
}).Times(2)
err := SendRaw(client, selp)
require.NoError(err)
})
})
t.Run("failed to invoke SendAction api", func(t *testing.T) {
expectedErr := errors.New("failed to invoke SendAction api")
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
err := SendRaw(client, selp)
require.Equal(err.Error(), expectedErr.Error())
})
}
func TestSendAction(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
response := &iotexapi.SendActionResponse{}
tx := &action.Execution{}
elp := (&action.EnvelopeBuilder{}).
SetNonce(0).
SetGasPrice(unit.ConvertIotxToRau(1100000)).
SetGasLimit(100).
SetAction(tx).Build()
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(2)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {})
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {})
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(response, nil)
t.Run("sends signed action to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
err := SendAction(client, cmd, elp, "test")
require.Error(err) // This should be NoError, but couldn't design a correct signer with public key.
})
}
func TestRead(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
contractAddr := identityset.Address(28)
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2)
client.EXPECT().Config().Return(config.Config{
DefaultAccount: config.Context{
AddressOrAlias: "test",
},
}).Times(2)
client.EXPECT().Address(gomock.Any()).Return("test", nil).Times(2)
t.Run("reads smart contract on IoTeX blockchain", func(t *testing.T) {
response := &iotexapi.ReadContractResponse{
Data: "test",
}
apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(response, nil)
result, err := Read(client, contractAddr, "0", []byte("0x1bfc56c600000000000000000000000000000000000000000000000000000000000000"))
require.NoError(err)
require.Equal(result, "test")
})
t.Run("failed to invoke ReadContract api", func(t *testing.T) {
expectedErr := errors.New("failed to invoke ReadContract api")
apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
_, err := Read(client, contractAddr, "0", []byte("0x1bfc56c600000000000000000000000000000000000000000000000000000000000000"))
require.Equal(err.Error(), expectedErr.Error())
})
}