Skip to content

Commit

Permalink
add tests for grpc query
Browse files Browse the repository at this point in the history
  • Loading branch information
yutianwu committed Jul 31, 2023
1 parent be69978 commit cce7d87
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 2 deletions.
2 changes: 1 addition & 1 deletion e2e/tests/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ func (s *StorageTestSuite) TestDeleteGroupPolicy() {
s.SendTxBlock(owner, msgDeletePolicy)

// verify permission
queryPolicyForAccountResp, err = s.Client.QueryPolicyForAccount(ctx, &queryPolicyForAccountReq)
_, err = s.Client.QueryPolicyForAccount(ctx, &queryPolicyForAccountReq)
s.Require().Error(err)
s.Require().ErrorContains(err, "No such Policy")
}
Expand Down
293 changes: 293 additions & 0 deletions x/storage/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/stretchr/testify/require"

"github.com/bnb-chain/greenfield/testutil/sample"

"github.com/bnb-chain/greenfield/x/storage/keeper"
"github.com/bnb-chain/greenfield/x/storage/types"
)
Expand Down Expand Up @@ -85,3 +88,293 @@ func TestVersionedParamsQuery(t *testing.T) {
p = responseT2.GetParams()
require.EqualValues(t, p.GetMaxSegmentSize(), 2)
}

func TestHeadGroupNFT(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadGroupNFT(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadGroupNFT(ctx, &types.QueryNFTRequest{
TokenId: "xxx",
})
require.ErrorContains(t, err, "invalid token id")

// group not exist
_, err = k.HeadGroupNFT(ctx, &types.QueryNFTRequest{
TokenId: "0",
})
require.ErrorIs(t, err, types.ErrNoSuchGroup)
}

func TestHeadObjectNFT(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadObjectNFT(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadObjectNFT(ctx, &types.QueryNFTRequest{
TokenId: "xxx",
})
require.ErrorContains(t, err, "invalid token id")

// object not exist
_, err = k.HeadObjectNFT(ctx, &types.QueryNFTRequest{
TokenId: "0",
})
require.ErrorIs(t, err, types.ErrNoSuchObject)
}

func TestHeadBucketNFT(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadBucketNFT(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadBucketNFT(ctx, &types.QueryNFTRequest{
TokenId: "xxx",
})
require.ErrorContains(t, err, "invalid token id")

// bucket not exist
_, err = k.HeadBucketNFT(ctx, &types.QueryNFTRequest{
TokenId: "0",
})
require.ErrorIs(t, err, types.ErrNoSuchBucket)
}

func TestHeadBucketById(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadBucketById(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadBucketById(ctx, &types.QueryHeadBucketByIdRequest{
BucketId: "xxx",
})
require.ErrorContains(t, err, "invalid bucket id")

// bucket not exist
_, err = k.HeadBucketById(ctx, &types.QueryHeadBucketByIdRequest{
BucketId: "0",
})
require.ErrorIs(t, err, types.ErrNoSuchBucket)
}

func TestHeadObject(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadObject(ctx, nil)
require.ErrorContains(t, err, "invalid request")

// object not exist
_, err = k.HeadObject(ctx, &types.QueryHeadObjectRequest{
BucketName: "bucket",
ObjectName: "object",
})
require.ErrorIs(t, err, types.ErrNoSuchObject)
}

func TestHeadObjectById(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadBucketById(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadObjectById(ctx, &types.QueryHeadObjectByIdRequest{
ObjectId: "xxx",
})
require.ErrorContains(t, err, "invalid object id")

// bucket not exist
_, err = k.HeadObjectById(ctx, &types.QueryHeadObjectByIdRequest{
ObjectId: "1",
})
require.ErrorIs(t, err, types.ErrNoSuchObject)
}

func TestListBuckets(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.ListBuckets(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.ListBuckets(ctx, &types.QueryListBucketsRequest{
Pagination: &query.PageRequest{
Limit: types.MaxPaginationLimit + 1,
},
})
require.ErrorContains(t, err, "exceed pagination limit")
}

func TestListObjects(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.ListObjects(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.ListObjects(ctx, &types.QueryListObjectsRequest{
Pagination: &query.PageRequest{
Limit: types.MaxPaginationLimit + 1,
},
})
require.ErrorContains(t, err, "exceed pagination limit")
}

func TestListObjectsByBucketId(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.ListObjectsByBucketId(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.ListObjectsByBucketId(ctx, &types.QueryListObjectsByBucketIdRequest{
Pagination: &query.PageRequest{
Limit: types.MaxPaginationLimit + 1,
},
})
require.ErrorContains(t, err, "exceed pagination limit")

_, err = k.ListObjectsByBucketId(ctx, &types.QueryListObjectsByBucketIdRequest{
BucketId: "xxx",
})
require.ErrorContains(t, err, "invalid bucket id")

_, err = k.ListObjectsByBucketId(ctx, &types.QueryListObjectsByBucketIdRequest{
BucketId: "0",
})
require.ErrorIs(t, err, types.ErrNoSuchBucket)
}

func TestQueryPolicyForAccount(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.QueryPolicyForAccount(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.QueryPolicyForAccount(ctx, &types.QueryPolicyForAccountRequest{
PrincipalAddress: "xxxx",
})
require.ErrorContains(t, err, "invalid address hex length")

_, err = k.QueryPolicyForAccount(ctx, &types.QueryPolicyForAccountRequest{
PrincipalAddress: sample.RandAccAddressHex(),
Resource: "xxx",
})
require.ErrorContains(t, err, "regex match error")
}

func TestQueryPolicyForGroup(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.QueryPolicyForGroup(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.QueryPolicyForGroup(ctx, &types.QueryPolicyForGroupRequest{
PrincipalGroupId: "xxx",
})
require.ErrorContains(t, err, "invalid group id")

_, err = k.QueryPolicyForGroup(ctx, &types.QueryPolicyForGroupRequest{
PrincipalGroupId: "10",
Resource: "xxx",
})
require.ErrorContains(t, err, "regex match error")
}

func TestVerifyPermission(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.VerifyPermission(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.VerifyPermission(ctx, &types.QueryVerifyPermissionRequest{
Operator: "xxx",
})
require.ErrorContains(t, err, "invalid operator address")

_, err = k.VerifyPermission(ctx, &types.QueryVerifyPermissionRequest{
Operator: sample.RandAccAddressHex(),
BucketName: "",
})
require.ErrorContains(t, err, "No bucket specified")

_, err = k.VerifyPermission(ctx, &types.QueryVerifyPermissionRequest{
Operator: sample.RandAccAddressHex(),
BucketName: "bucket",
})
require.ErrorIs(t, err, types.ErrNoSuchBucket)
}

func TestHeadGroup(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.HeadGroup(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadGroup(ctx, &types.QueryHeadGroupRequest{
GroupOwner: "xxx",
})
require.ErrorContains(t, err, "invalid address hex length")

_, err = k.HeadGroup(ctx, &types.QueryHeadGroupRequest{
GroupOwner: sample.RandAccAddressHex(),
GroupName: "group",
})
require.ErrorIs(t, err, types.ErrNoSuchGroup)
}

func TestListGroup(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.ListGroup(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.ListGroup(ctx, &types.QueryListGroupRequest{
Pagination: &query.PageRequest{
Limit: types.MaxPaginationLimit + 1,
},
})
require.ErrorContains(t, err, "exceed pagination limit")

_, err = k.ListGroup(ctx, &types.QueryListGroupRequest{
GroupOwner: "xxx",
})
require.ErrorContains(t, err, "invalid address hex length")
}

func TestHeadGroupMember(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.ListGroup(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.HeadGroupMember(ctx, &types.QueryHeadGroupMemberRequest{
Member: "xxx",
})
require.ErrorContains(t, err, "invalid address hex length")

_, err = k.HeadGroupMember(ctx, &types.QueryHeadGroupMemberRequest{
Member: sample.RandAccAddressHex(),
GroupOwner: "xxx",
})
require.ErrorContains(t, err, "invalid address hex length")

_, err = k.HeadGroupMember(ctx, &types.QueryHeadGroupMemberRequest{
Member: sample.RandAccAddressHex(),
GroupOwner: sample.RandAccAddressHex(),
GroupName: "group",
})
require.ErrorIs(t, err, types.ErrNoSuchGroup)
}

func TestQueryPolicyById(t *testing.T) {
// invalid argument
k, ctx := makeKeeper(t)
_, err := k.QueryPolicyById(ctx, nil)
require.ErrorContains(t, err, "invalid request")

_, err = k.QueryPolicyById(ctx, &types.QueryPolicyByIdRequest{
PolicyId: "xxx",
})
require.ErrorContains(t, err, "invalid policy id")
}
6 changes: 5 additions & 1 deletion x/storage/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (k Keeper) HeadGroupNFT(goCtx context.Context, req *types.QueryNFTRequest)
ctx := sdk.UnwrapSDKContext(goCtx)
groupInfo, found := k.GetGroupInfoById(ctx, id)
if !found {
return nil, types.ErrNoSuchObject
return nil, types.ErrNoSuchGroup
}
return &types.QueryGroupNFTResponse{
MetaData: groupInfo.ToNFTMetadata(),
Expand Down Expand Up @@ -443,6 +443,10 @@ func (k Keeper) HeadGroupMember(goCtx context.Context, req *types.QueryHeadGroup

func (k Keeper) QueryPolicyById(goCtx context.Context, req *types.QueryPolicyByIdRequest) (*types.
QueryPolicyByIdResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
policyId, err := math.ParseUint(req.PolicyId)
if err != nil {
Expand Down

0 comments on commit cce7d87

Please sign in to comment.