cosmos-sdk/x/auth/keeper/keeper_test.go

149 lines
4.1 KiB
Go
Raw Normal View History

2019-08-15 08:19:16 -07:00
package keeper_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
const (
holder = "holder"
multiPerm = "multiple permissions account"
randomPerm = "random permission"
)
var (
multiPermAcc = types.NewEmptyModuleAccount(multiPerm, types.Burner, types.Minter, types.Staking)
randomPermAcc = types.NewEmptyModuleAccount(randomPerm, "random")
)
type KeeperTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
queryClient types.QueryClient
}
func (suite *KeeperTestSuite) SetupTest() {
suite.app, suite.ctx = createTestApp(true)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.AccountKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
func TestAccountMapperGetSet(t *testing.T) {
2019-08-15 08:19:16 -07:00
app, ctx := createTestApp(true)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
addr := sdk.AccAddress([]byte("some---------address"))
// no account before its created
2019-08-15 08:19:16 -07:00
acc := app.AccountKeeper.GetAccount(ctx, addr)
require.Nil(t, acc)
// create account and check default values
2019-08-15 08:19:16 -07:00
acc = app.AccountKeeper.NewAccountWithAddress(ctx, addr)
require.NotNil(t, acc)
require.Equal(t, addr, acc.GetAddress())
require.EqualValues(t, nil, acc.GetPubKey())
require.EqualValues(t, 0, acc.GetSequence())
// NewAccount doesn't call Set, so it's still nil
2019-08-15 08:19:16 -07:00
require.Nil(t, app.AccountKeeper.GetAccount(ctx, addr))
// set some values on the account and save it
newSequence := uint64(20)
err := acc.SetSequence(newSequence)
require.NoError(t, err)
2019-08-15 08:19:16 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
// check the new values
2019-08-15 08:19:16 -07:00
acc = app.AccountKeeper.GetAccount(ctx, addr)
require.NotNil(t, acc)
require.Equal(t, newSequence, acc.GetSequence())
}
func TestAccountMapperRemoveAccount(t *testing.T) {
2019-08-15 08:19:16 -07:00
app, ctx := createTestApp(true)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
addr1 := sdk.AccAddress([]byte("addr1---------------"))
addr2 := sdk.AccAddress([]byte("addr2---------------"))
// create accounts
2019-08-15 08:19:16 -07:00
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2)
accSeq1 := uint64(20)
accSeq2 := uint64(40)
err := acc1.SetSequence(accSeq1)
require.NoError(t, err)
err = acc2.SetSequence(accSeq2)
require.NoError(t, err)
2019-08-15 08:19:16 -07:00
app.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc2)
2019-08-15 08:19:16 -07:00
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.NotNil(t, acc1)
require.Equal(t, accSeq1, acc1.GetSequence())
// remove one account
2019-08-15 08:19:16 -07:00
app.AccountKeeper.RemoveAccount(ctx, acc1)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.Nil(t, acc1)
2019-08-15 08:19:16 -07:00
acc2 = app.AccountKeeper.GetAccount(ctx, addr2)
require.NotNil(t, acc2)
require.Equal(t, accSeq2, acc2.GetSequence())
}
2019-08-15 08:19:16 -07:00
func TestGetSetParams(t *testing.T) {
app, ctx := createTestApp(true)
params := types.DefaultParams()
2019-08-15 08:19:16 -07:00
app.AccountKeeper.SetParams(ctx, params)
2019-08-15 08:19:16 -07:00
actualParams := app.AccountKeeper.GetParams(ctx)
require.Equal(t, params, actualParams)
}
func TestSupply_ValidatePermissions(t *testing.T) {
app, _ := createTestApp(true)
// add module accounts to supply keeper
maccPerms := simapp.GetMaccPerms()
maccPerms[holder] = nil
maccPerms[types.Burner] = []string{types.Burner}
maccPerms[types.Minter] = []string{types.Minter}
maccPerms[multiPerm] = []string{types.Burner, types.Minter, types.Staking}
maccPerms[randomPerm] = []string{"random"}
cdc := simapp.MakeTestEncodingConfig().Marshaler
keeper := keeper.NewAccountKeeper(
cdc, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName),
types.ProtoBaseAccount, maccPerms,
)
err := keeper.ValidatePermissions(multiPermAcc)
require.NoError(t, err)
err = keeper.ValidatePermissions(randomPermAcc)
require.NoError(t, err)
// unregistered permissions
otherAcc := types.NewEmptyModuleAccount("other", "other")
err = app.AccountKeeper.ValidatePermissions(otherAcc)
require.Error(t, err)
}