Skip to content

Commit

Permalink
Fix SendToModuleAccountTest (#8857)
Browse files Browse the repository at this point in the history
(cherry picked from commit 280ee4f)

# Conflicts:
#	x/bank/app_test.go
  • Loading branch information
jgimeno authored and mergify-bot committed Mar 11, 2021
1 parent 7ea2760 commit 47665de
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
)

type (
Expand Down Expand Up @@ -122,6 +121,7 @@ func TestSendNotEnoughBalance(t *testing.T) {
require.Equal(t, res2.GetSequence(), origSeq+1)
}

<<<<<<< HEAD
// A module account cannot be the recipient of bank sends unless it has been marked as such
func TestSendToModuleAcc(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -202,6 +202,8 @@ func TestSendToModuleAcc(t *testing.T) {
}
}

=======
>>>>>>> 280ee4f15... Fix SendToModuleAccountTest (#8857)
func TestMsgMultiSendWithAccounts(t *testing.T) {
acc := &authtypes.BaseAccount{
Address: addr1.String(),
Expand Down
68 changes: 66 additions & 2 deletions x/bank/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package bank_test

import (
"strings"
Expand All @@ -7,13 +7,20 @@ import (
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func TestInvalidMsg(t *testing.T) {
h := NewHandler(nil)
h := bank.NewHandler(nil)

res, err := h(sdk.NewContext(nil, tmproto.Header{}, false, nil), testdata.NewTestMsg())
require.Error(t, err)
Expand All @@ -22,3 +29,60 @@ func TestInvalidMsg(t *testing.T) {
_, _, log := sdkerrors.ABCIInfo(err, false)
require.True(t, strings.Contains(log, "unrecognized bank message type"))
}

// A module account cannot be the recipient of bank sends unless it has been marked as such
func TestSendToModuleAccount(t *testing.T) {
priv1 := secp256k1.GenPrivKey()
addr1 := sdk.AccAddress(priv1.PubKey().Address())
moduleAccAddr := authtypes.NewModuleAddress(stakingtypes.BondedPoolName)
coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}

tests := []struct {
name string
expectedError error
msg *types.MsgSend
}{
{
name: "not allowed module account",
msg: types.NewMsgSend(addr1, moduleAccAddr, coins),
expectedError: sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", moduleAccAddr),
},
{
name: "allowed module account",
msg: types.NewMsgSend(addr1, authtypes.NewModuleAddress(stakingtypes.ModuleName), coins),
expectedError: nil,
},
}

acc1 := &authtypes.BaseAccount{
Address: addr1.String(),
}
accs := authtypes.GenesisAccounts{acc1}
balances := []types.Balance{
{
Address: addr1.String(),
Coins: coins,
},
}

app := simapp.SetupWithGenesisAccounts(accs, balances...)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

app.BankKeeper = bankkeeper.NewBaseKeeper(
app.AppCodec(), app.GetKey(types.StoreKey), app.AccountKeeper, app.GetSubspace(types.ModuleName), map[string]bool{
moduleAccAddr.String(): true,
},
)
handler := bank.NewHandler(app.BankKeeper)

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := handler(ctx, tc.msg)
if tc.expectedError != nil {
require.EqualError(t, err, tc.expectedError.Error())
} else {
require.NoError(t, err)
}
})
}
}

0 comments on commit 47665de

Please sign in to comment.