Skip to content

Commit

Permalink
add tests for decorator and keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitys7 committed Apr 24, 2024
1 parent edb2315 commit 86f3900
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
7 changes: 7 additions & 0 deletions testutil/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
hubgenkeeper "github.com/dymensionxyz/dymension-rdk/x/hub-genesis/keeper"
mintkeeper "github.com/dymensionxyz/dymension-rdk/x/mint/keeper"
seqkeeper "github.com/dymensionxyz/dymension-rdk/x/sequencers/keeper"
vestingkeeper "github.com/dymensionxyz/dymension-rdk/x/vesting/keeper"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

app "github.com/dymensionxyz/dymension-rdk/testutil/app"
Expand Down Expand Up @@ -44,3 +45,9 @@ func NewTestHubGenesisKeeperFromApp(app *app.App) (*hubgenkeeper.Keeper, sdk.Con
ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "rollapp-1", Time: time.Now().UTC()})
return k, ctx
}

func NewTestVestingKeeperFromApp(app *app.App) (*vestingkeeper.Keeper, sdk.Context) {
k := &app.VestingKeeper
ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "rollapp-1", Time: time.Now().UTC()})

Check warning

Code scanning / CodeQL

Calling the system time Warning test

Calling the system time may be a possible source of non-determinism
return k, ctx
}
34 changes: 34 additions & 0 deletions x/vesting/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package keeper_test

import (
"testing"

testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers"
"github.com/dymensionxyz/dymension-rdk/testutil/utils"
"github.com/dymensionxyz/dymension-rdk/x/vesting/types"
"github.com/stretchr/testify/require"
)

func TestParams(t *testing.T) {
// Setup the test environment
app := utils.Setup(t, false)
k, ctx := testkeepers.NewTestVestingKeeperFromApp(app)

// Set some initial parameters
initialParams := types.DefaultParams()
initialParams.AllowedAddresses = []string{"cosmos19crd4fwzm9qtf5ln5l3e2vmquhevjwprk8tgxp", "cosmos1gusne8eh37myphx09hgdsy85zpl2t0kzdvu3en"} // Example addresses
k.SetParams(ctx, initialParams)

// Retrieve the parameters
retrievedParams := k.GetParams(ctx)

// Assert that the retrieved parameters match the initial ones
require.Equal(t, initialParams, retrievedParams, "retrieved parameters should match the initial ones")

// Test setting and getting a different set of parameters
updatedParams := initialParams
updatedParams.AllowedAddresses = append(updatedParams.AllowedAddresses, "cosmos1s77x8wr2gzdhq8gt8c085vate0s23xu9u80wtx")
k.SetParams(ctx, updatedParams)
retrievedParams = k.GetParams(ctx)
require.Equal(t, updatedParams, retrievedParams, "retrieved parameters should match the updated ones")
}
152 changes: 152 additions & 0 deletions x/vesting/keeper/permissioned_decorator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package keeper_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/client"
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
sdkvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/stretchr/testify/suite"

"github.com/dymensionxyz/dymension-rdk/testutil/app"
"github.com/dymensionxyz/dymension-rdk/testutil/ibctest"
testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers"

"github.com/dymensionxyz/dymension-rdk/x/vesting/keeper"
"github.com/dymensionxyz/dymension-rdk/x/vesting/types"
)

const (
rollappDenom = "arax"
)

type VestingKeeperTestSuite struct {
ibctest.IBCTestUtilSuite

app *app.App
k *keeper.Keeper
ctx sdk.Context
clientCtx client.Context
txBuilder client.TxBuilder
fees sdk.Coins
}

func TestVestingKeeperTestSuite(t *testing.T) {
suite.Run(t, new(VestingKeeperTestSuite))
}

func (suite *VestingKeeperTestSuite) setupTest() {
coinAmount := sdk.NewCoin(rollappDenom, sdk.NewInt(20))
suite.fees = sdk.NewCoins(coinAmount)

suite.IBCTestUtilSuite.SetupTest(rollappDenom)
suite.app = suite.RollAppChain.App.(*app.App)
suite.k, suite.ctx = testkeepers.NewTestVestingKeeperFromApp(suite.app)
suite.clientCtx = client.Context{}.
WithTxConfig(suite.app.GetTxConfig()).
WithCodec(suite.app.AppCodec())

suite.txBuilder = suite.app.GetTxConfig().NewTxBuilder()
suite.txBuilder.SetFeeAmount(suite.fees)
suite.txBuilder.SetGasLimit(200000)
}

func (suite *VestingKeeperTestSuite) TestPermissionedVestingDecorator() {
suite.setupTest()

// Generate a permission account
_, _, addr0 := testdata.KeyTestPubAddr()
acc0, err := sdk.Bech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), addr0)
suite.Require().NoError(err)

suite.k.SetParams(suite.ctx, types.Params{
AllowedAddresses: []string{acc0},
})

_, _, addr1 := testdata.KeyTestPubAddr()
_, _, addr2 := testdata.KeyTestPubAddr()

type testcase struct {
name string
msgs []sdk.Msg
msgTypeURLs []string
isSimulate bool // if blank, is false
expectPass bool
}

tests := []testcase{
{
name: "permission account should success",
msgs: []sdk.Msg{
sdkvestingtypes.NewMsgCreateVestingAccount(addr0, addr1, sdk.NewCoins(), 10000000, false),
},
msgTypeURLs: []string{
sdk.MsgTypeURL(&sdkvestingtypes.MsgCreateVestingAccount{}),
},
expectPass: true,
},
{
name: "non permission account should return error",
msgs: []sdk.Msg{
sdkvestingtypes.NewMsgCreateVestingAccount(addr1, addr2, sdk.NewCoins(), 10000000, false),
},
msgTypeURLs: []string{
sdk.MsgTypeURL(&sdkvestingtypes.MsgCreateVestingAccount{}),
},
expectPass: false,
},
}

for _, tc := range tests {
tx := suite.CreateVestingMsgTxBuilder(tc.msgs)

pvd := keeper.NewPermissionedVestingDecorator(suite.app.VestingKeeper, tc.msgTypeURLs)
antehandlerPVD := sdk.ChainAnteDecorators(pvd)
_, err := antehandlerPVD(suite.ctx, tx, tc.isSimulate)
if tc.expectPass {

suite.Require().NoError(err, "test: %s", tc.name)
} else {
suite.Require().Error(err, "test: %s", tc.name)
}
}

}

func (suite *VestingKeeperTestSuite) CreateVestingMsgTxBuilder(msgs []sdk.Msg) authsigning.Tx {

// TxBuilder components reset for every test case
priv0, _, addr0 := testdata.KeyTestPubAddr()
acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0)
suite.app.AccountKeeper.SetAccount(suite.ctx, acc1)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0}, []uint64{0}, []uint64{0}
signerData := authsigning.SignerData{
ChainID: suite.ctx.ChainID(),
AccountNumber: accNums[0],
Sequence: accSeqs[0],
}

suite.txBuilder.SetMsgs(msgs...)

sigV2, _ := clienttx.SignWithPrivKey(
1,
signerData,
suite.txBuilder,
privs[0],
suite.clientCtx.TxConfig,
accSeqs[0],
)
suite.txBuilder.SetSignatures(sigV2)
suite.txBuilder.SetMemo("")

err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, suite.fees)
suite.Require().NoError(err)

tx := suite.txBuilder.GetTx()
return tx
}

0 comments on commit 86f3900

Please sign in to comment.