Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more unittest for MsgGrantPermission and MsgRevokePermission of x/token #1129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ostracon) [\#1099](https://github.com/Finschia/finschia-sdk/pull/1099) Remove libsodium vrf library.
* (refactor) [\#1114](https://github.com/Finschia/finschia-sdk/pull/1114) Check statistics and balance on x/collection mint and burn operations
* (x/token) [\#1128](https://github.com/Finschia/finschia-sdk/pull/1128) add more unittest for MsgIssue of x/token
* (x/token) [\#1129](https://github.com/Finschia/finschia-sdk/pull/1129) add more unittest for `MsgGrantPermission` and `MsgRevokePermission` of x/token

### Bug Fixes
* (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu)
Expand Down
9 changes: 8 additions & 1 deletion x/token/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type KeeperTestSuite struct {
customer sdk.AccAddress
stranger sdk.AccAddress

contractID string
contractID string
unmintableContractId string

balance sdk.Int
}
Expand Down Expand Up @@ -122,6 +123,12 @@ func (s *KeeperTestSuite) SetupTest() {
notTokenContractID := app.ClassKeeper.NewID(s.ctx)
err = keeper.ValidateLegacyContract(s.keeper, s.ctx, notTokenContractID)
s.Require().ErrorIs(err, token.ErrTokenNotExist)

s.unmintableContractId = s.keeper.Issue(s.ctx, token.Contract{
Name: "Unmintable",
Symbol: "UMT",
Mintable: false,
}, s.vendor, s.vendor, s.balance)
}

func TestKeeperTestSuite(t *testing.T) {
Expand Down
288 changes: 257 additions & 31 deletions x/token/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,92 @@ func (s *KeeperTestSuite) TestMsgIssue() {
mintable: true,
amount: sdk.NewInt(10),
events: sdk.Events{
sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}},
sdk.Event{
Type: "lbm.token.v1.EventIssued",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false},
{Key: []uint8("decimals"), Value: []uint8("0"), Index: false},
{Key: []uint8("meta"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("mintable"), Value: []uint8("true"), Index: false},
{Key: []uint8("name"), Value: []uint8("\"test\""), Index: false},
{Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false},
{Key: []uint8("uri"), Value: []uint8("\"\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false},
{Key: []uint8("granter"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false},
{Key: []uint8("granter"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false},
{Key: []uint8("granter"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventMinted",
Attributes: []abci.EventAttribute{
{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false},
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false},
{Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false},
},
},
},
},
"mintable false": {
mintable: false,
amount: sdk.NewInt(10),
events: sdk.Events{
sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}},
sdk.Event{
Type: "lbm.token.v1.EventIssued",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false},
{Key: []uint8("decimals"), Value: []uint8("0"), Index: false},
{Key: []uint8("meta"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("mintable"), Value: []uint8("false"), Index: false},
{Key: []uint8("name"), Value: []uint8("\"test\""), Index: false},
{Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false},
{Key: []uint8("uri"), Value: []uint8("\"\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false},
{Key: []uint8("granter"), Value: []uint8("\"\""), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false},
},
},
sdk.Event{
Type: "lbm.token.v1.EventMinted",
Attributes: []abci.EventAttribute{
{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false},
{Key: []uint8("contract_id"), Value: []uint8("\"ca8bfd79\""), Index: false},
{Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false},
{Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false},
},
},
},
},
}
Expand Down Expand Up @@ -336,27 +408,99 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() {
err error
events sdk.Events
}{
"valid request": {
contractID: s.contractID,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionModify.String(),
events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x65}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x7a, 0x77, 0x30, 0x38, 0x70, 0x36, 0x74, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x72}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x71, 0x61, 0x32, 0x78, 0x7a, 0x66, 0x78, 0x22}, Index: false}, {Key: []uint8{0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e}, Value: []uint8{0x22, 0x50, 0x45, 0x52, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4f, 0x4e, 0x5f, 0x4d, 0x4f, 0x44, 0x49, 0x46, 0x59, 0x22}, Index: false}}}},
},
"contract not found": {
contractID: "fee1dead",
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionModify.String(),
err: class.ErrContractNotExist,
},
"granter has no permission": {
"contract has no permission - MINT": {
contractID: s.unmintableContractId,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionMint.String(),
err: token.ErrTokenNoPermission,
},
"contract has no permission - BURN": {
contractID: s.unmintableContractId,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionBurn.String(),
err: token.ErrTokenNoPermission,
},
"granter has no permission - MINT": {
contractID: s.contractID,
granter: s.customer,
grantee: s.operator,
grantee: s.stranger,
permission: token.LegacyPermissionMint.String(),
err: token.ErrTokenNoPermission,
},
"granter has no permission - BURN": {
contractID: s.contractID,
granter: s.customer,
grantee: s.stranger,
permission: token.LegacyPermissionBurn.String(),
err: token.ErrTokenNoPermission,
},
"granter has no permission - MODIFY": {
contractID: s.contractID,
granter: s.customer,
grantee: s.stranger,
permission: token.LegacyPermissionModify.String(),
err: token.ErrTokenNoPermission,
},
"valid request - MINT": {
contractID: s.contractID,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionMint.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false},
{Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false},
},
},
},
},
"valid request - BURN": {
contractID: s.contractID,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionBurn.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false},
{Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false},
},
},
},
},
"valid request - MODIFY": {
contractID: s.contractID,
granter: s.vendor,
grantee: s.operator,
permission: token.LegacyPermissionModify.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventGranted",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator.String())), Index: false},
{Key: []uint8("granter"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor.String())), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false},
},
},
},
},
}

for name, tc := range testCases {
Expand All @@ -376,10 +520,21 @@ func (s *KeeperTestSuite) TestMsgGrantPermission() {
}

s.Require().NotNil(res)
s.Require().Equal(tc.events, ctx.EventManager().Events())

if s.deterministic {
s.Require().Equal(tc.events, ctx.EventManager().Events())
}
// check to grant permission
per, err := s.queryServer.GranteeGrants(sdk.WrapSDKContext(ctx), &token.QueryGranteeGrantsRequest{
ContractId: tc.contractID,
Grantee: tc.grantee.String(),
Pagination: nil,
})
s.Require().NoError(err)
s.Require().NotNil(per)
expectPermission := token.Grant{
Grantee: tc.grantee.String(),
Permission: token.Permission(token.LegacyPermissionFromString(tc.permission)),
}
s.Require().Contains(per.Grants, expectPermission)
})
}
}
Expand All @@ -392,24 +547,84 @@ func (s *KeeperTestSuite) TestMsgRevokePermission() {
err error
events sdk.Events
}{
"valid request": {
contractID: s.contractID,
from: s.operator,
permission: token.LegacyPermissionMint.String(),
events: sdk.Events{sdk.Event{Type: "lbm.token.v1.EventRenounced", Attributes: []abci.EventAttribute{{Key: []uint8{0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x5f, 0x69, 0x64}, Value: []uint8{0x22, 0x39, 0x62, 0x65, 0x31, 0x37, 0x31, 0x36, 0x35, 0x22}, Index: false}, {Key: []uint8{0x67, 0x72, 0x61, 0x6e, 0x74, 0x65, 0x65}, Value: []uint8{0x22, 0x6c, 0x69, 0x6e, 0x6b, 0x31, 0x76, 0x39, 0x6a, 0x78, 0x67, 0x75, 0x6e, 0x39, 0x77, 0x64, 0x65, 0x6e, 0x7a, 0x77, 0x30, 0x38, 0x70, 0x36, 0x74, 0x22}, Index: false}, {Key: []uint8{0x70, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e}, Value: []uint8{0x22, 0x50, 0x45, 0x52, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4f, 0x4e, 0x5f, 0x4d, 0x49, 0x4e, 0x54, 0x22}, Index: false}}}},
},
"contract not found": {
contractID: "fee1dead",
from: s.operator,
permission: token.LegacyPermissionMint.String(),
err: class.ErrContractNotExist,
},
"not granted yet": {
contractID: s.contractID,
"contract has no permission - MINT": {
contractID: s.unmintableContractId,
from: s.operator,
permission: token.LegacyPermissionMint.String(),
err: token.ErrTokenNoPermission,
},
"contract has no permission - BURN": {
contractID: s.unmintableContractId,
from: s.operator,
permission: token.LegacyPermissionBurn.String(),
err: token.ErrTokenNoPermission,
},
"grantee has no permission - MINT": {
contractID: s.contractID,
from: s.customer,
permission: token.LegacyPermissionMint.String(),
err: token.ErrTokenNoPermission,
},
"grantee has no permission - BURN": {
contractID: s.contractID,
from: s.customer,
permission: token.LegacyPermissionBurn.String(),
err: token.ErrTokenNoPermission,
},
"grantee has no permission - MODIFY": {
contractID: s.contractID,
from: s.customer,
permission: token.LegacyPermissionModify.String(),
err: token.ErrTokenNoPermission,
},
"valid request - revoke MINT": {
contractID: s.contractID,
from: s.operator,
permission: token.LegacyPermissionMint.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventRenounced",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false},
},
}},
},
"valid request - revoke BURN": {
contractID: s.contractID,
from: s.operator,
permission: token.LegacyPermissionBurn.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventRenounced",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.operator)), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false},
},
}},
},
"valid request - revoke MODIFY": {
contractID: s.contractID,
from: s.vendor,
permission: token.LegacyPermissionModify.String(),
events: sdk.Events{
sdk.Event{
Type: "lbm.token.v1.EventRenounced",
Attributes: []abci.EventAttribute{
{Key: []uint8("contract_id"), Value: []uint8("\"9be17165\""), Index: false},
{Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", s.vendor)), Index: false},
{Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false},
},
}},
},
}

for name, tc := range testCases {
Expand All @@ -428,10 +643,21 @@ func (s *KeeperTestSuite) TestMsgRevokePermission() {
}

s.Require().NotNil(res)
s.Require().Equal(tc.events, ctx.EventManager().Events())

if s.deterministic {
s.Require().Equal(tc.events, ctx.EventManager().Events())
}
// check to remove permission
per, err := s.queryServer.GranteeGrants(sdk.WrapSDKContext(ctx), &token.QueryGranteeGrantsRequest{
ContractId: tc.contractID,
Grantee: tc.from.String(),
Pagination: nil,
})
s.Require().NoError(err)
s.Require().NotNil(per)
expectPermission := token.Grant{
Grantee: tc.from.String(),
Permission: token.Permission(token.LegacyPermissionFromString(tc.permission)),
}
s.Require().NotContains(per.Grants, expectPermission)
})
}
}
Expand Down
Loading