Skip to content

Commit

Permalink
feat: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleem1314 committed Aug 12, 2022
1 parent eb6ae5c commit 7b4c9ea
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 4 additions & 1 deletion x/ecocredit/server/marketplace/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
14 changes: 10 additions & 4 deletions x/ecocredit/server/marketplace/msg_remove_allowed_denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand Down
66 changes: 66 additions & 0 deletions x/ecocredit/server/marketplace/msg_remove_allowed_denom_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7b4c9ea

Please sign in to comment.