cosmos-sdk/x/auth/keeper/keeper_test.go
Andrei Ivasko 59640fb858
feat!: Add bech32 prefix to authkeeper (#9759)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes **Step1** from #9690

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

- Add auth keeper methods and gRPC queries:
  - the `NewAccountKeeper` should take a `string bech32Prefix` argument
  - ` auth AccountKeeper` implements `Codec` interface that contains 2 methods:  `ConvertAddressStringToBytes` and `ConvertAddressBytesToString` 
  - Add the 3 following gRPC queries:
    - Get bech32 prefix
    - `AddressStringToBytes` (converts `AccountAddr` string to `AccountAddr` bytes)
    - `AddressBytesToString` ( converts `AccountAddr` bytes to `AccountAddr` str)
  - Add the corresponding keeper methods
  - Add tests
---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-08-25 11:17:18 +00:00

149 lines
4.1 KiB
Go

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(suite.T(), 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) {
app, ctx := createTestApp(t, true)
addr := sdk.AccAddress([]byte("some---------address"))
// no account before its created
acc := app.AccountKeeper.GetAccount(ctx, addr)
require.Nil(t, acc)
// create account and check default values
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
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)
app.AccountKeeper.SetAccount(ctx, acc)
// check the new values
acc = app.AccountKeeper.GetAccount(ctx, addr)
require.NotNil(t, acc)
require.Equal(t, newSequence, acc.GetSequence())
}
func TestAccountMapperRemoveAccount(t *testing.T) {
app, ctx := createTestApp(t, true)
addr1 := sdk.AccAddress([]byte("addr1---------------"))
addr2 := sdk.AccAddress([]byte("addr2---------------"))
// create accounts
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)
app.AccountKeeper.SetAccount(ctx, acc1)
app.AccountKeeper.SetAccount(ctx, acc2)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.NotNil(t, acc1)
require.Equal(t, accSeq1, acc1.GetSequence())
// remove one account
app.AccountKeeper.RemoveAccount(ctx, acc1)
acc1 = app.AccountKeeper.GetAccount(ctx, addr1)
require.Nil(t, acc1)
acc2 = app.AccountKeeper.GetAccount(ctx, addr2)
require.NotNil(t, acc2)
require.Equal(t, accSeq2, acc2.GetSequence())
}
func TestGetSetParams(t *testing.T) {
app, ctx := createTestApp(t, true)
params := types.DefaultParams()
app.AccountKeeper.SetParams(ctx, params)
actualParams := app.AccountKeeper.GetParams(ctx)
require.Equal(t, params, actualParams)
}
func TestSupply_ValidatePermissions(t *testing.T) {
app, _ := createTestApp(t, 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().Codec
keeper := keeper.NewAccountKeeper(
cdc, app.GetKey(types.StoreKey), app.GetSubspace(types.ModuleName),
types.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix,
)
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)
}