2019-08-15 08:19:16 -07:00
|
|
|
package keeper_test
|
2019-07-29 08:27:01 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-24 12:04:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2019-08-15 08:19:16 -07:00
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
2019-07-29 08:27:01 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQueryAccount(t *testing.T) {
|
2019-08-15 08:19:16 -07:00
|
|
|
app, ctx := createTestApp(true)
|
2020-08-10 12:41:21 -07:00
|
|
|
legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())
|
2019-07-29 08:27:01 -07:00
|
|
|
|
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: "",
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2019-08-15 08:19:16 -07:00
|
|
|
path := []string{types.QueryAccount}
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keep.NewQuerier(app.AccountKeeper, legacyQuerierCdc.LegacyAmino)
|
2019-07-29 08:27:01 -07:00
|
|
|
|
2019-08-15 08:19:16 -07:00
|
|
|
bz, err := querier(ctx, []string{"other"}, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, bz)
|
|
|
|
|
|
|
|
req = abci.RequestQuery{
|
|
|
|
Path: fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
2019-08-15 08:19:16 -07:00
|
|
|
res, err := querier(ctx, path, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
req.Data = legacyQuerierCdc.MustMarshalJSON(&types.QueryAccountRequest{Address: ""})
|
2019-08-15 08:19:16 -07:00
|
|
|
res, err = querier(ctx, path, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
_, _, addr := testdata.KeyTestPubAddr()
|
2020-09-25 03:25:37 -07:00
|
|
|
req.Data = legacyQuerierCdc.MustMarshalJSON(&types.QueryAccountRequest{Address: addr.String()})
|
2019-08-15 08:19:16 -07:00
|
|
|
res, err = querier(ctx, path, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
|
2019-08-15 08:19:16 -07:00
|
|
|
app.AccountKeeper.SetAccount(ctx, app.AccountKeeper.NewAccountWithAddress(ctx, addr))
|
|
|
|
res, err = querier(ctx, path, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2019-08-15 08:19:16 -07:00
|
|
|
res, err = querier(ctx, path, req)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
var account types.AccountI
|
2020-08-26 02:39:38 -07:00
|
|
|
err2 := legacyQuerierCdc.LegacyAmino.UnmarshalJSON(res, &account)
|
2019-07-29 08:27:01 -07:00
|
|
|
require.Nil(t, err2)
|
|
|
|
}
|