Skip to content

Commit

Permalink
TestExecutePrivilege
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Aug 15, 2024
1 parent 57b6340 commit b5f5670
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 45 deletions.
74 changes: 74 additions & 0 deletions x/asset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package keeper_test

import (
"context"
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

Expand Down Expand Up @@ -39,3 +43,73 @@ func (suite *KeeperTestSuite) SetupTest() {
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func init() {
proto.RegisterType((*MockPrivilegeMsg)(nil), "MockPrivilegeMsg")
}

// MockPrivilegeMsg defines a mock type PrivilegeMsg
type MockPrivilegeMsg struct {
privName string
}

var _ proto.Message = &MockPrivilegeMsg{}

func (m *MockPrivilegeMsg) ValidateBasic() error {
if m.privName == "" {
return fmt.Errorf("empty")
}
return nil
}

func (m *MockPrivilegeMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{userAddr1}
}

func (m *MockPrivilegeMsg) Reset() { *m = MockPrivilegeMsg{} }
func (m *MockPrivilegeMsg) String() string { return proto.CompactTextString(m) }
func (m *MockPrivilegeMsg) ProtoMessage() {}

func (m *MockPrivilegeMsg) NeedPrivilege() string {
return m.privName
}
func (m *MockPrivilegeMsg) XXX_MessageName() string {
return "MockPrivilegeMsg"
}

func (m *MockPrivilegeMsg) XXX_Unmarshal(b []byte) error {
return nil
}

// // MockPrivilegeI defines a mock type PrivilegeI
type MockPrivilegeI struct {
privName string
}

var _ types.PrivilegeI = MockPrivilegeI{}

func (m MockPrivilegeI) Name() string {
return m.privName
}

func (m MockPrivilegeI) RegisterInterfaces() {

}

func (m MockPrivilegeI) MsgHandler() types.MsgHandler {
return func(context context.Context, privMsg proto.Message) (proto.Message, error) {
return nil, nil
}
}

func (m MockPrivilegeI) QueryHandler() types.QueryHandler {
return func(context context.Context, privQuery proto.Message) (proto.Message, error) {
return nil, nil
}
}

func (m MockPrivilegeI) CLI() *cobra.Command {
return &cobra.Command{
Use: "mock",
}
}
161 changes: 116 additions & 45 deletions x/asset/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"slices"
"strings"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/libs/rand"

"github.com/realiotech/realio-network/x/asset/keeper"
Expand Down Expand Up @@ -426,48 +428,117 @@ func (s *KeeperTestSuite) TestDisablePrivilege() {
}
}

// func (s *KeeperTestSuite) TestExecutePrivilege() {

// tests := []struct {
// name string
// expectPass bool
// setup func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege
// }{
// {
// name: "success",
// expectPass: true,
// setup: func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege {
// description := ""

// lowerCaseSymbol := strings.ToLower(symbol)
// tokenId := fmt.Sprintf("%s/%s/%s", types.ModuleName, creatorAddr.String(), lowerCaseSymbol)
// token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
// k.SetToken(ctx, tokenId, token)

// tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
// k.SetTokenManagement(ctx, tokenId, tokenManage)

// k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr1)

// var privilegeMsg any.Any
// // p, e := any.NewAnyWithValue(privilegeMsg)
// // privilege, err := anypb.New(privilegeMsg)
// // s.Require().NoError(err)
// return &types.MsgExecutePrivilege{
// Address: userAddr1.String(),
// TokenId: tokenId,
// PrivilegeMsg: &privilegeMsg,
// }
// },
// },
// }

// for _, test := range tests {
// s.Run(test.name, func() {
// s.SetupTest()
// msg := test.setup(*s.assetKeeper, s.ctx)

// s.msgServer.ExecutePrivilege(s.ctx, msg)
// })
// }
// }
func (s *KeeperTestSuite) TestExecutePrivilege() {

tests := []struct {
name string
expectPass bool
setup func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege
}{
{
name: "success",
expectPass: true,
setup: func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege {
description := ""
privName := creatorAddr.String()

lowerCaseSymbol := strings.ToLower(symbol)
tokenId := fmt.Sprintf("%s/%s/%s", types.ModuleName, creatorAddr.String(), lowerCaseSymbol)
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr1)

err := k.AddPrivilege(MockPrivilegeI{
privName: privName,
})
s.Require().NoError(err)
var newMockMsg proto.Message = &MockPrivilegeMsg{
privName: privName,
}

privilegeMsg, err := codectypes.NewAnyWithValue(newMockMsg)
s.Require().NoError(err)

return &types.MsgExecutePrivilege{
Address: userAddr1.String(),
TokenId: tokenId,
PrivilegeMsg: privilegeMsg,
}
},
},
{
name: "token not exists",
expectPass: false,
setup: func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege {
privName := creatorAddr.String()

lowerCaseSymbol := strings.ToLower(symbol)
tokenId := fmt.Sprintf("%s/%s/%s", types.ModuleName, creatorAddr.String(), lowerCaseSymbol)

var newMockMsg proto.Message = &MockPrivilegeMsg{
privName: privName,
}

privilegeMsg, err := codectypes.NewAnyWithValue(newMockMsg)
s.Require().NoError(err)

return &types.MsgExecutePrivilege{
Address: userAddr1.String(),
TokenId: tokenId,
PrivilegeMsg: privilegeMsg,
}
},
},
{
name: "privilege name is not registered yet",
expectPass: false,
setup: func(k keeper.Keeper, ctx sdk.Context) *types.MsgExecutePrivilege {
description := ""
privName := creatorAddr.String()

lowerCaseSymbol := strings.ToLower(symbol)
tokenId := fmt.Sprintf("%s/%s/%s", types.ModuleName, creatorAddr.String(), lowerCaseSymbol)
token := types.NewToken(tokenId, strings.ToLower(name), lowerCaseSymbol, 2, description)
k.SetToken(ctx, tokenId, token)

tokenManage := types.NewTokenManagement(managerAddr.String(), true, []string{})
k.SetTokenManagement(ctx, tokenId, tokenManage)

k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr1)
k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr2)
k.SetTokenPrivilegeAccount(ctx, tokenId, creatorAddr.String(), userAddr3)

var newMockMsg proto.Message = &MockPrivilegeMsg{
privName: privName,
}

privilegeMsg, err := codectypes.NewAnyWithValue(newMockMsg)
s.Require().NoError(err)

return &types.MsgExecutePrivilege{
Address: userAddr1.String(),
TokenId: tokenId,
PrivilegeMsg: privilegeMsg,
}
},
},
}

for _, test := range tests {
s.Run(test.name, func() {
s.SetupTest()
msg := test.setup(*s.assetKeeper, s.ctx)

_, err := s.msgServer.ExecutePrivilege(s.ctx, msg)
if test.expectPass {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
})
}
}

0 comments on commit b5f5670

Please sign in to comment.