From 7b4c9eafc5a89438bd888a39be86141aae2cb765 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 12 Aug 2022 15:51:43 +0530 Subject: [PATCH] feat: add integration tests --- .../features/msg_remove_allowed_denom.feature | 70 +++++++++++++++++++ x/ecocredit/server/marketplace/keeper_test.go | 5 +- .../marketplace/msg_remove_allowed_denom.go | 14 ++-- .../msg_remove_allowed_denom_test.go | 66 +++++++++++++++++ 4 files changed, 150 insertions(+), 5 deletions(-) diff --git a/x/ecocredit/server/marketplace/features/msg_remove_allowed_denom.feature b/x/ecocredit/server/marketplace/features/msg_remove_allowed_denom.feature index e69de29bb2..efb1c480d0 100644 --- a/x/ecocredit/server/marketplace/features/msg_remove_allowed_denom.feature +++ b/x/ecocredit/server/marketplace/features/msg_remove_allowed_denom.feature @@ -0,0 +1,70 @@ +Feature: Msg/RemoveAllowedDenom + + An allowed denom can be removed: + - when the authority is a governance account address + - when the allowed denom exist + - the denom is removed + + Rule: The authority must be governance account address + + Scenario: The authority is a governance account address + Given an allowed denom with properties + """ + { + "authority":"regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68", + "bank_denom":"uregen", + "display_denom":"REGEN", + "exponent":6 + } + """ + When alice attempts to remove a bank denom with properties + """ + { + "authority":"regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68", + "denom":"uregen" + } + """ + Then expect no error + And expect bank denom is removed "uregen" + + Scenario: The authority is not a governance account address + When alice attempts to remove a bank denom with properties + """ + { + "authority":"regen1fua8speyxgempgy06gpfs0p4z32zznkqakm57s", + "denom":"uregen" + } + """ + Then expect error contains "expected gov account as only signer for proposal message" + + Rule: The bank denom exist + + Scenario: The bank denom exist + Given an allowed denom with properties + """ + { + "authority":"regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68", + "bank_denom":"uregen", + "display_denom":"REGEN", + "exponent":6 + } + """ + When alice attempts to remove a bank denom with properties + """ + { + "authority":"regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68", + "denom":"uregen" + } + """ + Then expect no error + And expect bank denom is removed "uregen" + + Scenario: The bank denom does not exist + When alice attempts to remove a bank denom with properties + """ + { + "authority":"regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68", + "denom":"uregen" + } + """ + Then expect the error "allowed denom uregen not found: not found" diff --git a/x/ecocredit/server/marketplace/keeper_test.go b/x/ecocredit/server/marketplace/keeper_test.go index 4cb8fc0ab3..58650159a3 100644 --- a/x/ecocredit/server/marketplace/keeper_test.go +++ b/x/ecocredit/server/marketplace/keeper_test.go @@ -74,7 +74,10 @@ func setupBase(t gocuke.TestingT, numAddresses int) *baseSuite { assert.NilError(t, err) s.bankKeeper = mocks.NewMockBankKeeper(s.ctrl) s.paramsKeeper = mocks.NewMockParamKeeper(s.ctrl) - s.k = NewKeeper(s.marketStore, s.coreStore, s.bankKeeper, s.paramsKeeper, sdk.AccAddress("")) + + authority, err := sdk.AccAddressFromBech32("regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68") + assert.NilError(s.t, err) + s.k = NewKeeper(s.marketStore, s.coreStore, s.bankKeeper, s.paramsKeeper, authority) // set test accounts for i := 0; i < numAddresses; i++ { diff --git a/x/ecocredit/server/marketplace/msg_remove_allowed_denom.go b/x/ecocredit/server/marketplace/msg_remove_allowed_denom.go index d8119529a5..3db5b86a54 100644 --- a/x/ecocredit/server/marketplace/msg_remove_allowed_denom.go +++ b/x/ecocredit/server/marketplace/msg_remove_allowed_denom.go @@ -3,9 +3,10 @@ package marketplace import ( "context" + "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - marketplacev1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1" "github.com/regen-network/regen-ledger/x/ecocredit/marketplace" ) @@ -15,9 +16,14 @@ func (k Keeper) RemoveAllowedDenom(ctx context.Context, req *marketplace.MsgRemo return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority: expected %s, got %s", k.authority, req.Authority) } - if err := k.stateStore.AllowedDenomTable().Delete(ctx, &marketplacev1.AllowedDenom{ - BankDenom: req.Denom, - }); err != nil { + allowedDenom, err := k.stateStore.AllowedDenomTable().Get(ctx, req.Denom) + if err != nil { + if ormerrors.NotFound.Is(err) { + return nil, sdkerrors.ErrNotFound.Wrapf("allowed denom %s not found", req.Denom) + } + } + + if err := k.stateStore.AllowedDenomTable().Delete(ctx, allowedDenom); err != nil { return nil, err } diff --git a/x/ecocredit/server/marketplace/msg_remove_allowed_denom_test.go b/x/ecocredit/server/marketplace/msg_remove_allowed_denom_test.go index d529db933c..a9d0df2bb2 100644 --- a/x/ecocredit/server/marketplace/msg_remove_allowed_denom_test.go +++ b/x/ecocredit/server/marketplace/msg_remove_allowed_denom_test.go @@ -1 +1,67 @@ package marketplace + +import ( + "encoding/json" + "testing" + + "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" + "github.com/regen-network/gocuke" + "github.com/stretchr/testify/require" + + api "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1" + "github.com/regen-network/regen-ledger/x/ecocredit/marketplace" +) + +type removeAllowedDenomSuite struct { + *baseSuite + err error +} + +func TestRemoveAllowedDenom(t *testing.T) { + gocuke.NewRunner(t, &removeAllowedDenomSuite{}).Path("./features/msg_remove_allowed_denom.feature").Run() +} + +func (s *removeAllowedDenomSuite) Before(t gocuke.TestingT) { + s.baseSuite = setupBase(t, 1) +} + +func (s *removeAllowedDenomSuite) AnAllowedDenomWithProperties(a gocuke.DocString) { + var msg *marketplace.AllowedDenom + + err := json.Unmarshal([]byte(a.Content), &msg) + require.NoError(s.t, err) + + err = s.k.stateStore.AllowedDenomTable().Insert(s.ctx, &api.AllowedDenom{ + BankDenom: msg.BankDenom, + DisplayDenom: msg.DisplayDenom, + Exponent: msg.Exponent, + }) + require.NoError(s.t, err) +} + +func (s *removeAllowedDenomSuite) AliceAttemptsToRemoveABankDenomWithProperties(a gocuke.DocString) { + var msg *marketplace.MsgRemoveAllowedDenom + + err := json.Unmarshal([]byte(a.Content), &msg) + require.NoError(s.t, err) + + _, s.err = s.k.RemoveAllowedDenom(s.ctx, msg) +} + +func (s *removeAllowedDenomSuite) ExpectNoError() { + require.NoError(s.t, s.err) +} + +func (s *removeAllowedDenomSuite) ExpectBankDenomIsRemoved(denom string) { + _, err := s.marketStore.AllowedDenomTable().Get(s.ctx, denom) + require.Error(s.t, err) + require.ErrorIs(s.t, err, ormerrors.NotFound) +} + +func (s *removeAllowedDenomSuite) ExpectTheError(a string) { + require.EqualError(s.t, s.err, a) +} + +func (s *removeAllowedDenomSuite) ExpectErrorContains(a string) { + require.ErrorContains(s.t, s.err, a) +}