cosmos-sdk/x/auth/types/account_test.go

212 lines
5.2 KiB
Go
Raw Normal View History

package types_test
import (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
refactor: Implementing sigs.k8s.io YAML to remove .proto yaml annotations (#9780) ## Description Draft of: #9705 Started off with changing codec `MarshalYaml` function to directly go from JSON to yaml using the new library. Replaced the only usage of UnmarshalYaml per request. --- ### 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) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [x] reviewed state machine logic - [x] reviewed API design and naming - [x] reviewed documentation is accurate - [x] reviewed tests and test coverage - [x] manually tested (if applicable)
2021-09-24 07:37:34 -07:00
"sigs.k8s.io/yaml"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
refactor(test)!: refactor `simapp.Setup` function (#9938) <!-- 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 ref: #8961 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Following up on [#9697](https://github.com/cosmos/cosmos-sdk/pull/9697#pullrequestreview-727295733), this PR is the first step for the #8961. --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] 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) - [ ] 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` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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-16 17:52:06 -07:00
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
2018-03-02 01:24:07 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
func TestBaseAddressPubKey(t *testing.T) {
_, pub1, addr1 := testdata.KeyTestPubAddr()
_, pub2, addr2 := testdata.KeyTestPubAddr()
acc := types.NewBaseAccountWithAddress(addr1)
2018-03-17 13:20:24 -07:00
// check the address (set) and pubkey (not set)
require.EqualValues(t, addr1, acc.GetAddress())
require.EqualValues(t, nil, acc.GetPubKey())
2018-01-17 16:42:05 -08:00
2018-03-17 13:53:27 -07:00
// can't override address
2018-03-17 13:20:24 -07:00
err := acc.SetAddress(addr2)
require.NotNil(t, err)
require.EqualValues(t, addr1, acc.GetAddress())
2018-03-17 13:20:24 -07:00
// set the pubkey
err = acc.SetPubKey(pub1)
require.Nil(t, err)
require.Equal(t, pub1, acc.GetPubKey())
2018-05-06 22:13:32 -07:00
// can override pubkey
2018-03-17 13:20:24 -07:00
err = acc.SetPubKey(pub2)
require.Nil(t, err)
require.Equal(t, pub2, acc.GetPubKey())
2018-03-17 13:20:24 -07:00
//------------------------------------
// can set address on empty account
acc2 := types.BaseAccount{}
2018-03-17 13:20:24 -07:00
err = acc2.SetAddress(addr2)
require.Nil(t, err)
require.EqualValues(t, addr2, acc2.GetAddress())
2018-03-17 13:20:24 -07:00
}
Put AccountSequence in SignerInfo (#6997) * WIP test the grounds * Update ADR020 * Fix compile errors * Fix ADR * Make ante tests pass * Fix remaining ante handler tests * Simplify code * Fix x/bank app_test * Fix tests * Remove useless accSeq from signerdata * Fix test * Update simapp/helpers/test_helpers.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update simapp/helpers/test_helpers.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update x/auth/client/cli/tx_multisign.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Address rewview * Update x/auth/ante/sigverify.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update x/auth/ante/sigverify_test.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update x/auth/tx/builder_test.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update x/auth/tx/builder_test.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update x/auth/tx/direct_test.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * Update x/auth/tx/builder_test.go Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> * AccSeq -> Seq * Address reviews * Better variable naming * Fix variable assign * Remove old SetSignerInfo * Fix test * proto-gen * Make proto-gen * Reput gw comment * Add Changelog * Update x/bank/app_test.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update x/bank/app_test.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com>
2020-08-21 07:20:47 -07:00
func TestBaseSequence(t *testing.T) {
_, _, addr := testdata.KeyTestPubAddr()
acc := types.NewBaseAccountWithAddress(addr)
seq := uint64(7)
2018-03-17 13:20:24 -07:00
err := acc.SetSequence(seq)
require.Nil(t, err)
require.Equal(t, seq, acc.GetSequence())
2018-03-17 13:20:24 -07:00
}
func TestBaseAccountMarshal(t *testing.T) {
refactor(test)!: refactor `simapp.Setup` function (#9938) <!-- 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 ref: #8961 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Following up on [#9697](https://github.com/cosmos/cosmos-sdk/pull/9697#pullrequestreview-727295733), this PR is the first step for the #8961. --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] 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) - [ ] 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` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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-16 17:52:06 -07:00
app := simapp.Setup(t, false)
_, pub, addr := testdata.KeyTestPubAddr()
acc := types.NewBaseAccountWithAddress(addr)
seq := uint64(7)
2018-03-17 13:20:24 -07:00
// set everything on the account
err := acc.SetPubKey(pub)
require.Nil(t, err)
2018-03-17 13:20:24 -07:00
err = acc.SetSequence(seq)
require.Nil(t, err)
2018-03-17 13:20:24 -07:00
bz, err := app.AccountKeeper.MarshalAccount(acc)
require.Nil(t, err)
acc2, err := app.AccountKeeper.UnmarshalAccount(bz)
require.Nil(t, err)
require.Equal(t, acc, acc2)
2018-03-17 13:20:24 -07:00
// error on bad bytes
_, err = app.AccountKeeper.UnmarshalAccount(bz[:len(bz)/2])
require.NotNil(t, err)
2018-03-17 13:20:24 -07:00
}
func TestGenesisAccountValidate(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
baseAcc := types.NewBaseAccount(addr, pubkey, 0, 0)
tests := []struct {
name string
acc types.GenesisAccount
2020-02-20 08:15:22 -08:00
expErr bool
}{
{
"valid base account",
baseAcc,
2020-02-20 08:15:22 -08:00
false,
},
{
"invalid base valid account",
types.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0),
2020-02-20 08:15:22 -08:00
true,
},
}
for _, tt := range tests {
2019-10-17 06:47:35 -07:00
tt := tt
t.Run(tt.name, func(t *testing.T) {
2020-02-20 08:15:22 -08:00
require.Equal(t, tt.expErr, tt.acc.Validate() != nil)
})
}
}
func TestModuleAccountMarshalYAML(t *testing.T) {
name := "test"
moduleAcc := types.NewEmptyModuleAccount(name, types.Minter, types.Burner, types.Staking)
bs, err := yaml.Marshal(moduleAcc)
require.NoError(t, err)
refactor: Implementing sigs.k8s.io YAML to remove .proto yaml annotations (#9780) ## Description Draft of: #9705 Started off with changing codec `MarshalYaml` function to directly go from JSON to yaml using the new library. Replaced the only usage of UnmarshalYaml per request. --- ### 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) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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... - [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [x] reviewed state machine logic - [x] reviewed API design and naming - [x] reviewed documentation is accurate - [x] reviewed tests and test coverage - [x] manually tested (if applicable)
2021-09-24 07:37:34 -07:00
want := "account_number: 0\naddress: cosmos1n7rdpqvgf37ktx30a2sv2kkszk3m7ncmg5drhe\nname: test\npermissions:\n- minter\n- burner\n- staking\npublic_key: \"\"\nsequence: 0\n"
require.Equal(t, want, string(bs))
}
func TestHasPermissions(t *testing.T) {
name := "test"
macc := types.NewEmptyModuleAccount(name, types.Staking, types.Minter, types.Burner)
cases := []struct {
permission string
expectHas bool
}{
{types.Staking, true},
{types.Minter, true},
{types.Burner, true},
{"other", false},
}
for i, tc := range cases {
hasPerm := macc.HasPermission(tc.permission)
if tc.expectHas {
require.True(t, hasPerm, "test case #%d", i)
} else {
require.False(t, hasPerm, "test case #%d", i)
}
}
}
func TestValidate(t *testing.T) {
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
baseAcc := types.NewBaseAccount(addr, nil, 0, 0)
tests := []struct {
name string
acc types.GenesisAccount
expErr error
}{
{
"valid module account",
types.NewEmptyModuleAccount("test"),
nil,
},
{
"invalid name and address pair",
types.NewModuleAccount(baseAcc, "test"),
fmt.Errorf("address %s cannot be derived from the module name 'test'", addr),
},
{
"empty module account name",
types.NewModuleAccount(baseAcc, " "),
errors.New("module account name cannot be blank"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err := tt.acc.Validate()
require.Equal(t, tt.expErr, err)
})
}
}
func TestModuleAccountJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
baseAcc := types.NewBaseAccount(addr, nil, 10, 50)
acc := types.NewModuleAccount(baseAcc, "test", "burner")
bz, err := json.Marshal(acc)
require.NoError(t, err)
bz1, err := acc.MarshalJSON()
require.NoError(t, err)
require.Equal(t, string(bz1), string(bz))
var a types.ModuleAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.Equal(t, acc.String(), a.String())
}
func TestGenesisAccountsContains(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
acc := types.NewBaseAccount(addr, secp256k1.GenPrivKey().PubKey(), 0, 0)
genAccounts := types.GenesisAccounts{}
require.False(t, genAccounts.Contains(acc.GetAddress()))
genAccounts = append(genAccounts, acc)
require.True(t, genAccounts.Contains(acc.GetAddress()))
}