github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/keeper/grpc_query_test.go (about) 1 package keeper_test 2 3 import ( 4 gocontext "context" 5 "fmt" 6 "time" 7 8 "github.com/stretchr/testify/require" 9 10 sdk "github.com/cosmos/cosmos-sdk/types" 11 "github.com/cosmos/cosmos-sdk/types/query" 12 "github.com/cosmos/cosmos-sdk/x/authz" 13 banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" 14 ) 15 16 func (suite *TestSuite) TestGRPCQueryAuthorization() { 17 queryClient, addrs := suite.queryClient, suite.addrs 18 var ( 19 req *authz.QueryGrantsRequest 20 expAuthorization authz.Authorization 21 ) 22 23 testCases := []struct { 24 msg string 25 malleate func(require *require.Assertions) 26 expError string 27 postTest func(require *require.Assertions, res *authz.QueryGrantsResponse) 28 }{ 29 { 30 "fail invalid granter addr", 31 func(require *require.Assertions) { 32 req = &authz.QueryGrantsRequest{} 33 }, 34 "empty address string is not allowed", 35 func(require *require.Assertions, res *authz.QueryGrantsResponse) {}, 36 }, 37 { 38 "fail invalid grantee addr", 39 func(require *require.Assertions) { 40 req = &authz.QueryGrantsRequest{ 41 Granter: addrs[0].String(), 42 } 43 }, 44 "empty address string is not allowed", 45 func(require *require.Assertions, res *authz.QueryGrantsResponse) {}, 46 }, 47 { 48 "fail invalid msg-type", 49 func(require *require.Assertions) { 50 req = &authz.QueryGrantsRequest{ 51 Granter: addrs[0].String(), 52 Grantee: addrs[1].String(), 53 MsgTypeUrl: "unknown", 54 } 55 }, 56 "authorization not found for unknown type", 57 func(require *require.Assertions, res *authz.QueryGrantsResponse) {}, 58 }, 59 { 60 "authorization not found", 61 func(require *require.Assertions) { 62 req = &authz.QueryGrantsRequest{ 63 Granter: addrs[1].String(), 64 Grantee: addrs[0].String(), 65 MsgTypeUrl: banktypes.SendAuthorization{}.MsgTypeURL(), 66 } 67 }, 68 "authorization not found for /cosmos.bank.v1beta1.MsgSend", 69 func(require *require.Assertions, res *authz.QueryGrantsResponse) {}, 70 }, 71 { 72 "success", 73 func(require *require.Assertions) { 74 expAuthorization = suite.createSendAuthorization(addrs[0], addrs[1]) 75 req = &authz.QueryGrantsRequest{ 76 Granter: addrs[1].String(), 77 Grantee: addrs[0].String(), 78 MsgTypeUrl: expAuthorization.MsgTypeURL(), 79 } 80 }, 81 "", 82 func(require *require.Assertions, res *authz.QueryGrantsResponse) { 83 var auth authz.Authorization 84 require.Equal(1, len(res.Grants)) 85 err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth) 86 require.NoError(err) 87 require.NotNil(auth) 88 require.Equal(auth.String(), expAuthorization.String()) 89 }, 90 }, 91 { 92 "success with allow list", 93 func(require *require.Assertions) { 94 expAuthorization = suite.createSendAuthorizationWithAllowList(addrs[0], addrs[1]) 95 require.Len(expAuthorization.(*banktypes.SendAuthorization).GetAllowList(), 1) 96 req = &authz.QueryGrantsRequest{ 97 Granter: addrs[1].String(), 98 Grantee: addrs[0].String(), 99 MsgTypeUrl: expAuthorization.MsgTypeURL(), 100 } 101 }, 102 "", 103 func(require *require.Assertions, res *authz.QueryGrantsResponse) { 104 var auth authz.Authorization 105 require.Equal(1, len(res.Grants)) 106 err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth) 107 require.NoError(err) 108 require.NotNil(auth) 109 require.Equal(auth.String(), expAuthorization.String()) 110 require.Equal(auth.(*banktypes.SendAuthorization).GetAllowList(), expAuthorization.(*banktypes.SendAuthorization).GetAllowList()) 111 }, 112 }, 113 } 114 for _, tc := range testCases { 115 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 116 require := suite.Require() 117 tc.malleate(require) 118 result, err := queryClient.Grants(gocontext.Background(), req) 119 if tc.expError == "" { 120 require.NoError(err) 121 } else { 122 require.Error(err) 123 require.Contains(err.Error(), tc.expError) 124 } 125 tc.postTest(require, result) 126 }) 127 } 128 } 129 130 func (suite *TestSuite) TestGRPCQueryGranterGrants() { 131 require := suite.Require() 132 queryClient, addrs := suite.queryClient, suite.addrs 133 134 testCases := []struct { 135 msg string 136 preRun func() 137 expError bool 138 request authz.QueryGranterGrantsRequest 139 numItems int 140 }{ 141 { 142 "fail invalid granter addr", 143 func() {}, 144 true, 145 authz.QueryGranterGrantsRequest{}, 146 0, 147 }, 148 { 149 "valid case, single authorization", 150 func() { 151 suite.createSendAuthorization(addrs[1], addrs[0]) 152 }, 153 false, 154 authz.QueryGranterGrantsRequest{ 155 Granter: addrs[0].String(), 156 }, 157 1, 158 }, 159 { 160 "valid case, multiple authorization", 161 func() { 162 suite.createSendAuthorization(addrs[2], addrs[0]) 163 }, 164 false, 165 authz.QueryGranterGrantsRequest{ 166 Granter: addrs[0].String(), 167 }, 168 2, 169 }, 170 { 171 "valid case, pagination", 172 func() { 173 }, 174 false, 175 authz.QueryGranterGrantsRequest{ 176 Granter: addrs[0].String(), 177 Pagination: &query.PageRequest{ 178 Limit: 1, 179 }, 180 }, 181 1, 182 }, 183 } 184 185 for _, tc := range testCases { 186 tc := tc 187 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 188 tc.preRun() 189 result, err := queryClient.GranterGrants(gocontext.Background(), &tc.request) 190 if tc.expError { 191 require.Error(err) 192 } else { 193 require.NoError(err) 194 require.Len(result.Grants, tc.numItems) 195 } 196 }) 197 } 198 } 199 200 func (suite *TestSuite) TestGRPCQueryGranteeGrants() { 201 require := suite.Require() 202 queryClient, addrs := suite.queryClient, suite.addrs 203 204 testCases := []struct { 205 msg string 206 preRun func() 207 expError bool 208 request authz.QueryGranteeGrantsRequest 209 numItems int 210 }{ 211 { 212 "fail invalid granter addr", 213 func() {}, 214 true, 215 authz.QueryGranteeGrantsRequest{}, 216 0, 217 }, 218 { 219 "valid case, single authorization", 220 func() { 221 suite.createSendAuthorization(addrs[0], addrs[1]) 222 }, 223 false, 224 authz.QueryGranteeGrantsRequest{ 225 Grantee: addrs[0].String(), 226 }, 227 1, 228 }, 229 { 230 "valid case, no authorization found", 231 func() {}, 232 false, 233 authz.QueryGranteeGrantsRequest{ 234 Grantee: addrs[2].String(), 235 }, 236 0, 237 }, 238 { 239 "valid case, multiple authorization", 240 func() { 241 suite.createSendAuthorization(addrs[0], addrs[2]) 242 }, 243 false, 244 authz.QueryGranteeGrantsRequest{ 245 Grantee: addrs[0].String(), 246 }, 247 2, 248 }, 249 { 250 "valid case, pagination", 251 func() {}, 252 false, 253 authz.QueryGranteeGrantsRequest{ 254 Grantee: addrs[0].String(), 255 Pagination: &query.PageRequest{ 256 Limit: 1, 257 }, 258 }, 259 1, 260 }, 261 } 262 263 for _, tc := range testCases { 264 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 265 tc.preRun() 266 result, err := queryClient.GranteeGrants(gocontext.Background(), &tc.request) 267 if tc.expError { 268 require.Error(err) 269 } else { 270 require.NoError(err) 271 require.Len(result.Grants, tc.numItems) 272 } 273 }) 274 } 275 } 276 277 func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress) authz.Authorization { 278 exp := suite.ctx.BlockHeader().Time.Add(time.Hour) 279 newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) 280 authorization := &banktypes.SendAuthorization{SpendLimit: newCoins} 281 err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp) 282 suite.Require().NoError(err) 283 return authorization 284 } 285 286 func (suite *TestSuite) createSendAuthorizationWithAllowList(grantee, granter sdk.AccAddress) authz.Authorization { 287 exp := suite.ctx.BlockHeader().Time.Add(time.Hour) 288 newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) 289 authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{suite.addrs[5].String()}} 290 err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp) 291 suite.Require().NoError(err) 292 return authorization 293 }