mukan-sdk/x/authz/keeper/grpc_query_test.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

292 lines
7.4 KiB
Go

package keeper_test
import (
gocontext "context"
"fmt"
"time"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (suite *TestSuite) TestGRPCQueryAuthorization() {
queryClient, addrs := suite.queryClient, suite.addrs
var (
req *authz.QueryGrantsRequest
expAuthorization authz.Authorization
)
testCases := []struct {
msg string
malleate func(require *require.Assertions)
expError string
postTest func(require *require.Assertions, res *authz.QueryGrantsResponse)
}{
{
"fail invalid granter addr",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{}
},
"empty address string is not allowed",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"fail invalid grantee addr",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{
Granter: addrs[0].String(),
}
},
"empty address string is not allowed",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"fail invalid msg-type",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{
Granter: addrs[0].String(),
Grantee: addrs[1].String(),
MsgTypeUrl: "unknown",
}
},
"authorization not found for unknown type",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"authorization not found",
func(require *require.Assertions) {
req = &authz.QueryGrantsRequest{
Granter: addrs[1].String(),
Grantee: addrs[0].String(),
MsgTypeUrl: banktypes.SendAuthorization{}.MsgTypeURL(),
}
},
"authorization not found for /cosmos.bank.v1beta1.MsgSend",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {},
},
{
"success",
func(require *require.Assertions) {
expAuthorization = suite.createSendAuthorization(addrs[0], addrs[1])
req = &authz.QueryGrantsRequest{
Granter: addrs[1].String(),
Grantee: addrs[0].String(),
MsgTypeUrl: expAuthorization.MsgTypeURL(),
}
},
"",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {
var auth authz.Authorization
require.Equal(1, len(res.Grants))
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth)
require.NoError(err)
require.NotNil(auth)
require.Equal(auth.String(), expAuthorization.String())
},
},
{
"success with allow list",
func(require *require.Assertions) {
expAuthorization = suite.createSendAuthorizationWithAllowList(addrs[0], addrs[1])
require.Len(expAuthorization.(*banktypes.SendAuthorization).GetAllowList(), 1)
req = &authz.QueryGrantsRequest{
Granter: addrs[1].String(),
Grantee: addrs[0].String(),
MsgTypeUrl: expAuthorization.MsgTypeURL(),
}
},
"",
func(require *require.Assertions, res *authz.QueryGrantsResponse) {
var auth authz.Authorization
require.Equal(1, len(res.Grants))
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Grants[0].Authorization, &auth)
require.NoError(err)
require.NotNil(auth)
require.Equal(auth.String(), expAuthorization.String())
require.Equal(auth.(*banktypes.SendAuthorization).GetAllowList(), expAuthorization.(*banktypes.SendAuthorization).GetAllowList())
},
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
require := suite.Require()
tc.malleate(require)
result, err := queryClient.Grants(gocontext.Background(), req)
if tc.expError == "" {
require.NoError(err)
} else {
require.Error(err)
require.Contains(err.Error(), tc.expError)
}
tc.postTest(require, result)
})
}
}
func (suite *TestSuite) TestGRPCQueryGranterGrants() {
require := suite.Require()
queryClient, addrs := suite.queryClient, suite.addrs
testCases := []struct {
msg string
preRun func()
expError bool
request authz.QueryGranterGrantsRequest
numItems int
}{
{
"fail invalid granter addr",
func() {},
true,
authz.QueryGranterGrantsRequest{},
0,
},
{
"valid case, single authorization",
func() {
suite.createSendAuthorization(addrs[1], addrs[0])
},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
},
1,
},
{
"valid case, multiple authorization",
func() {
suite.createSendAuthorization(addrs[2], addrs[0])
},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
},
2,
},
{
"valid case, pagination",
func() {
},
false,
authz.QueryGranterGrantsRequest{
Granter: addrs[0].String(),
Pagination: &query.PageRequest{
Limit: 1,
},
},
1,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
tc.preRun()
result, err := queryClient.GranterGrants(gocontext.Background(), &tc.request)
if tc.expError {
require.Error(err)
} else {
require.NoError(err)
require.Len(result.Grants, tc.numItems)
}
})
}
}
func (suite *TestSuite) TestGRPCQueryGranteeGrants() {
require := suite.Require()
queryClient, addrs := suite.queryClient, suite.addrs
testCases := []struct {
msg string
preRun func()
expError bool
request authz.QueryGranteeGrantsRequest
numItems int
}{
{
"fail invalid granter addr",
func() {},
true,
authz.QueryGranteeGrantsRequest{},
0,
},
{
"valid case, single authorization",
func() {
suite.createSendAuthorization(addrs[0], addrs[1])
},
false,
authz.QueryGranteeGrantsRequest{
Grantee: addrs[0].String(),
},
1,
},
{
"valid case, no authorization found",
func() {},
false,
authz.QueryGranteeGrantsRequest{
Grantee: addrs[2].String(),
},
0,
},
{
"valid case, multiple authorization",
func() {
suite.createSendAuthorization(addrs[0], addrs[2])
},
false,
authz.QueryGranteeGrantsRequest{
Grantee: addrs[0].String(),
},
2,
},
{
"valid case, pagination",
func() {},
false,
authz.QueryGranteeGrantsRequest{
Grantee: addrs[0].String(),
Pagination: &query.PageRequest{
Limit: 1,
},
},
1,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
tc.preRun()
result, err := queryClient.GranteeGrants(gocontext.Background(), &tc.request)
if tc.expError {
require.Error(err)
} else {
require.NoError(err)
require.Len(result.Grants, tc.numItems)
}
})
}
}
func (suite *TestSuite) createSendAuthorization(grantee, granter sdk.AccAddress) authz.Authorization {
exp := suite.ctx.BlockHeader().Time.Add(time.Hour)
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins}
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp)
suite.Require().NoError(err)
return authorization
}
func (suite *TestSuite) createSendAuthorizationWithAllowList(grantee, granter sdk.AccAddress) authz.Authorization {
exp := suite.ctx.BlockHeader().Time.Add(time.Hour)
newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100))
authorization := &banktypes.SendAuthorization{SpendLimit: newCoins, AllowList: []string{suite.addrs[5].String()}}
err := suite.authzKeeper.SaveGrant(suite.ctx, grantee, granter, authorization, &exp)
suite.Require().NoError(err)
return authorization
}