/
grpc.go
269 lines (258 loc) · 6.96 KB
/
grpc.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package testutil
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil/rest"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (s *IntegrationTestSuite) TestQueryGrantGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
grantsURL := val.APIAddress + "/cosmos/authz/v1beta1/grants?granter=%s&grantee=%s&msg_type_url=%s"
testCases := []struct {
name string
url string
expectErr bool
errorMsg string
}{
{
"fail invalid granter address",
fmt.Sprintf(grantsURL, "invalid_granter", grantee.String(), typeMsgSend),
true,
"decoding bech32 failed: invalid separator index -1: invalid request",
},
{
"fail invalid grantee address",
fmt.Sprintf(grantsURL, val.Address.String(), "invalid_grantee", typeMsgSend),
true,
"decoding bech32 failed: invalid separator index -1: invalid request",
},
{
"fail with empty granter",
fmt.Sprintf(grantsURL, "", grantee.String(), typeMsgSend),
true,
"empty address string is not allowed: invalid request",
},
{
"fail with empty grantee",
fmt.Sprintf(grantsURL, val.Address.String(), "", typeMsgSend),
true,
"empty address string is not allowed: invalid request",
},
{
"fail invalid msg-type",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String(), "invalidMsg"),
true,
"authorization not found for invalidMsg type",
},
{
"valid query",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String(), typeMsgSend),
false,
"",
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, _ := rest.GetRequest(tc.url)
require := s.Require()
if tc.expectErr {
require.Contains(string(resp), tc.errorMsg)
} else {
var g authz.QueryGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &g)
require.NoError(err)
require.Len(g.Grants, 1)
g.Grants[0].UnpackInterfaces(val.ClientCtx.InterfaceRegistry)
auth, err := g.Grants[0].GetAuthorization()
require.NoError(err)
require.Equal(auth.MsgTypeURL(), banktypes.SendAuthorization{}.MsgTypeURL())
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
grantsURL := val.APIAddress + "/cosmos/authz/v1beta1/grants?granter=%s&grantee=%s"
testCases := []struct {
name string
url string
expectErr bool
errMsg string
preRun func()
postRun func(*authz.QueryGrantsResponse)
}{
{
"valid query: expect single grant",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 1)
},
},
{
"valid query: expect two grants",
fmt.Sprintf(grantsURL, val.Address.String(), grantee.String()),
false,
"",
func() {
_, err := CreateGrant(val, []string{
grantee.String(),
"generic",
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", cli.FlagMsgType, typeMsgVote),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf("--%s=%d", cli.FlagExpiration, time.Now().Add(time.Minute*time.Duration(120)).Unix()),
})
s.Require().NoError(err)
},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 2)
},
},
{
"valid query: expect single grant with pagination",
fmt.Sprintf(grantsURL+"&pagination.limit=1", val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 1)
},
},
{
"valid query: expect two grants with pagination",
fmt.Sprintf(grantsURL+"&pagination.limit=2", val.Address.String(), grantee.String()),
false,
"",
func() {},
func(g *authz.QueryGrantsResponse) {
s.Require().Len(g.Grants, 2)
},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
tc.preRun()
resp, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expectErr {
s.Require().Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
s.Require().NoError(err)
tc.postRun(&authorizations)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGranterGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
require := s.Require()
testCases := []struct {
name string
url string
expectErr bool
errMsg string
numItems int
}{
{
"invalid account address",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, "invalid address"),
true,
"decoding bech32 failed",
0,
},
{
"no authorizations found",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, grantee.String()),
false,
"",
0,
},
{
"valid query",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/granter/%s", val.APIAddress, val.Address.String()),
false,
"",
8,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
require.NoError(err)
if tc.expectErr {
require.Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGranterGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
require.NoError(err)
// FIXME: https://github.com/cosmos/cosmos-sdk/issues/10965
require.Len(authorizations.Grants, tc.numItems)
}
})
}
}
func (s *IntegrationTestSuite) TestQueryGranteeGrantsGRPC() {
val := s.network.Validators[0]
grantee := s.grantee[1]
require := s.Require()
testCases := []struct {
name string
url string
expectErr bool
errMsg string
numItems int
}{
{
"invalid account address",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, "invalid address"),
true,
"decoding bech32 failed",
0,
},
{
"no authorizations found",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, val.Address.String()),
false,
"",
0,
},
{
"valid query",
fmt.Sprintf("%s/cosmos/authz/v1beta1/grants/grantee/%s", val.APIAddress, grantee.String()),
false,
"",
1,
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
require.NoError(err)
if tc.expectErr {
require.Contains(string(resp), tc.errMsg)
} else {
var authorizations authz.QueryGranteeGrantsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &authorizations)
require.NoError(err)
// FIXME: https://github.com/cosmos/cosmos-sdk/issues/10965
require.Len(authorizations.Grants, tc.numItems)
}
})
}
}