Skip to content

Commit

Permalink
add proposals tests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Ivanov committed Jun 25, 2024
1 parent c85a265 commit 6224b42
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 38 deletions.
12 changes: 6 additions & 6 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ type Haqq struct {
LiquidVestingKeeper liquidvestingkeeper.Keeper

// Haqq keepers
CoinomicsKeeper coinomicskeeper.Keeper
DaoKeeper daokeeper.Keeper
ShariaOracleKeeper shariahoraclekeeper.Keeper
CoinomicsKeeper coinomicskeeper.Keeper
DaoKeeper daokeeper.Keeper
ShariahOracleKeeper shariahoraclekeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -525,7 +525,7 @@ func NewHaqq(
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(&app.UpgradeKeeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(erc20types.RouterKey, erc20.NewErc20ProposalHandler(&app.Erc20Keeper)).
AddRoute(shariahoracletypes.RouterKey, shariahoracle.NewShariahOracleProposalHandler(&app.ShariaOracleKeeper))
AddRoute(shariahoracletypes.RouterKey, shariahoracle.NewShariahOracleProposalHandler(&app.ShariahOracleKeeper))
govConfig := govtypes.Config{
MaxMetadataLen: 10000,
}
Expand Down Expand Up @@ -574,7 +574,7 @@ func NewHaqq(
appCodec, keys[daotypes.StoreKey], app.AccountKeeper, app.BankKeeper, authAddr,
)

app.ShariaOracleKeeper = shariahoraclekeeper.NewKeeper(
app.ShariahOracleKeeper = shariahoraclekeeper.NewKeeper(
keys[shariahoracletypes.StoreKey], appCodec, app.GetSubspace(shariahoracletypes.ModuleName),
app.EvmKeeper, app.AccountKeeper,
)
Expand Down Expand Up @@ -728,7 +728,7 @@ func NewHaqq(
// Haqq app modules
coinomics.NewAppModule(app.CoinomicsKeeper, app.AccountKeeper, app.StakingKeeper),
dao.NewAppModule(appCodec, app.DaoKeeper, app.GetSubspace(daotypes.ModuleName)),
shariahoracle.NewAppModule(appCodec, app.ShariaOracleKeeper, app.AccountKeeper),
shariahoracle.NewAppModule(appCodec, app.ShariahOracleKeeper, app.AccountKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
10 changes: 5 additions & 5 deletions x/shariahoracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func (suite *GenesisTestSuite) TestShariahOracleInitGenesis() {
for _, tc := range testCases {

suite.Require().NotPanics(func() {
shariahoracle.InitGenesis(suite.ctx, suite.app.ShariaOracleKeeper, suite.app.AccountKeeper, tc.genesisState)
shariahoracle.InitGenesis(suite.ctx, suite.app.ShariahOracleKeeper, suite.app.AccountKeeper, tc.genesisState)
})
params := suite.app.ShariaOracleKeeper.GetParams(suite.ctx)
params := suite.app.ShariahOracleKeeper.GetParams(suite.ctx)

suite.Require().Equal(tc.genesisState.Params, params)
}
Expand All @@ -104,10 +104,10 @@ func (suite *GenesisTestSuite) TestShariahOracleExportGenesis() {
}

for _, tc := range testGenCases {
shariahoracle.InitGenesis(suite.ctx, suite.app.ShariaOracleKeeper, suite.app.AccountKeeper, tc.genesisState)
shariahoracle.InitGenesis(suite.ctx, suite.app.ShariahOracleKeeper, suite.app.AccountKeeper, tc.genesisState)
suite.Require().NotPanics(func() {
genesisExported := shariahoracle.ExportGenesis(suite.ctx, suite.app.ShariaOracleKeeper)
params := suite.app.ShariaOracleKeeper.GetParams(suite.ctx)
genesisExported := shariahoracle.ExportGenesis(suite.ctx, suite.app.ShariahOracleKeeper)
params := suite.app.ShariahOracleKeeper.GetParams(suite.ctx)
suite.Require().Equal(genesisExported.Params, params)
})
}
Expand Down
8 changes: 4 additions & 4 deletions x/shariahoracle/keeper/cac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
utiltx "github.com/haqq-network/haqq/testutil/tx"
evmtypes "github.com/haqq-network/haqq/x/evm/types"
"github.com/haqq-network/haqq/x/shariahoracle/keeper"
shariahoracletypes "github.com/haqq-network/haqq/x/shariahoracle/types"
"github.com/haqq-network/haqq/x/shariahoracle/types"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -48,17 +48,17 @@ func (suite *KeeperTestSuite) TestDoesAddressHaveCAC() {
for _, tc := range testCases {
suite.SetupTest() // reset
mockEVMKeeper = &MockEVMKeeper{}
suite.app.ShariaOracleKeeper = keeper.NewKeeper(
suite.app.ShariahOracleKeeper = keeper.NewKeeper(
suite.app.GetKey("shariahoracle"),
suite.app.AppCodec(),
suite.app.GetSubspace(shariahoracletypes.ModuleName),
suite.app.GetSubspace(types.ModuleName),
mockEVMKeeper,
suite.app.AccountKeeper,
)

tc.malleate()

doesHave, err := suite.app.ShariaOracleKeeper.DoesAddressHaveCAC(suite.ctx, utiltx.GenerateAddress().String())
doesHave, err := suite.app.ShariahOracleKeeper.DoesAddressHaveCAC(suite.ctx, utiltx.GenerateAddress().String())
if tc.res {
suite.Require().Equal(tc.expRes, doesHave)
} else {
Expand Down
4 changes: 2 additions & 2 deletions x/shariahoracle/keeper/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (suite *KeeperTestSuite) TestCallEVM() {
suite.Require().NoError(err)
account := utiltx.GenerateAddress()

res, err := suite.app.ShariaOracleKeeper.CallEVM(suite.ctx, erc20, types.ModuleAddress, contract, true, tc.method, account)
res, err := suite.app.ShariahOracleKeeper.CallEVM(suite.ctx, erc20, types.ModuleAddress, contract, true, tc.method, account)
if tc.expPass {
suite.Require().IsTypef(&evmtypes.MsgEthereumTxResponse{}, res, tc.name)
suite.Require().NoError(err)
Expand Down Expand Up @@ -136,7 +136,7 @@ func (suite *KeeperTestSuite) TestCallEVMWithData() {

data, contract := tc.malleate()

res, err := suite.app.ShariaOracleKeeper.CallEVMWithData(suite.ctx, tc.from, contract, data, true)
res, err := suite.app.ShariahOracleKeeper.CallEVMWithData(suite.ctx, tc.from, contract, data, true)
if tc.expPass {
suite.Require().IsTypef(&evmtypes.MsgEthereumTxResponse{}, res, tc.name)
suite.Require().NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions x/shariahoracle/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func (suite *KeeperTestSuite) TestParams() {
params := suite.app.ShariaOracleKeeper.GetParams(suite.ctx)
suite.app.ShariaOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
params := suite.app.ShariahOracleKeeper.GetParams(suite.ctx)
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck

testCases := []struct {
name string
Expand All @@ -22,7 +22,7 @@ func (suite *KeeperTestSuite) TestParams() {
return types.DefaultParams()
},
func() interface{} {
return suite.app.ShariaOracleKeeper.GetParams(suite.ctx)
return suite.app.ShariahOracleKeeper.GetParams(suite.ctx)
},
true,
},
Expand Down
246 changes: 246 additions & 0 deletions x/shariahoracle/keeper/proposals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package keeper_test

import (
"fmt"

"github.com/haqq-network/haqq/contracts"
utiltx "github.com/haqq-network/haqq/testutil/tx"
evmtypes "github.com/haqq-network/haqq/x/evm/types"
"github.com/haqq-network/haqq/x/shariahoracle/keeper"
"github.com/haqq-network/haqq/x/shariahoracle/types"
"github.com/stretchr/testify/mock"
)

func (suite *KeeperTestSuite) TestMintCAC() { //nolint:govet // we can copy locks here because it is a test

testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"ok",
func() {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
},
true,
},
{
"fail - CAC already issued",
func() {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
err = suite.app.ShariahOracleKeeper.MintCAC(suite.ctx, types.ModuleAddress.String())
suite.Require().NoError(err)
},
false,
},
{
"fail - force fail evm",
func() {
mockEVMKeeper := &MockEVMKeeper{}

suite.app.ShariahOracleKeeper = keeper.NewKeeper(
suite.app.GetKey("shariahoracle"), suite.app.AppCodec(),
suite.app.GetSubspace(types.ModuleName),
mockEVMKeeper,
suite.app.AccountKeeper,
)

mockEVMKeeper.On("EstimateGas", mock.Anything, mock.Anything).Return(&evmtypes.EstimateGasResponse{Gas: uint64(200)}, nil)
mockEVMKeeper.On("ApplyMessage", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("forced ApplyMessage error"))
},
false,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest() // reset

tc.malleate()

err := suite.app.ShariahOracleKeeper.MintCAC(suite.ctx, types.ModuleAddress.String())
suite.Commit()

if tc.expPass {
suite.Require().NoError(err, tc.name)
minted, err := suite.app.ShariahOracleKeeper.DoesAddressHaveCAC(suite.ctx, types.ModuleAddress.String())
suite.Require().NoError(err)
suite.Require().True(minted)
} else {
suite.Require().Error(err, tc.name)
}
})
}
}

func (suite *KeeperTestSuite) TestBurnCAC() { //nolint:govet // we can copy locks here because it is a test

testCases := []struct {
name string
malleate func()
expPass bool
}{
{
"ok",
func() {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
err = suite.app.ShariahOracleKeeper.MintCAC(suite.ctx, types.ModuleAddress.String())
suite.Require().NoError(err)
},
true,
},
{
"fail - CAC not issued",
func() {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
},
false,
},
{
"fail - force fail evm",
func() {
mockEVMKeeper := &MockEVMKeeper{}

suite.app.ShariahOracleKeeper = keeper.NewKeeper(
suite.app.GetKey("shariahoracle"), suite.app.AppCodec(),
suite.app.GetSubspace(types.ModuleName),
mockEVMKeeper,
suite.app.AccountKeeper,
)

mockEVMKeeper.On("EstimateGas", mock.Anything, mock.Anything).Return(&evmtypes.EstimateGasResponse{Gas: uint64(200)}, nil)
mockEVMKeeper.On("ApplyMessage", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("forced ApplyMessage error"))
},
false,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest() // reset

tc.malleate()

err := suite.app.ShariahOracleKeeper.BurnCAC(suite.ctx, types.ModuleAddress.String())
suite.Commit()

if tc.expPass {
suite.Require().NoError(err, tc.name)
minted, err := suite.app.ShariahOracleKeeper.DoesAddressHaveCAC(suite.ctx, types.ModuleAddress.String())
suite.Require().NoError(err)
suite.Require().False(minted)
} else {
suite.Require().Error(err, tc.name)
}
})
}
}

func (suite *KeeperTestSuite) TestUpdateCACContract() { //nolint:govet // we can copy locks here because it is a test

testCases := []struct {
name string
malleate func() string
expPass bool
}{
{
"ok - successful update",
func() string {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
newImplementationAddress, err := suite.PrepareProxyUpgrade(contracts.CommunityApprovalCertificatesContract)
suite.Require().NoError(err)
return newImplementationAddress.String()
},
true,
},
{
"fail - new implementation not deployed",
func() string {
cacAddr, err := suite.DeployUUPSContract(contracts.CommunityApprovalCertificatesContract,
CACModuleAddress,
CACModuleAddress,
CACModuleAddress,
CACContractBaseURI,
)
suite.Require().NoError(err)
params := types.NewParams(cacAddr.String())
suite.app.ShariahOracleKeeper.SetParams(suite.ctx, params) //nolint:errcheck
return utiltx.GenerateAddress().String()
},
false,
},
{
"fail - force fail evm",
func() string {
mockEVMKeeper := &MockEVMKeeper{}

suite.app.ShariahOracleKeeper = keeper.NewKeeper(
suite.app.GetKey("shariahoracle"), suite.app.AppCodec(),
suite.app.GetSubspace(types.ModuleName),
mockEVMKeeper,
suite.app.AccountKeeper,
)

mockEVMKeeper.On("EstimateGas", mock.Anything, mock.Anything).Return(&evmtypes.EstimateGasResponse{Gas: uint64(200)}, nil)
mockEVMKeeper.On("ApplyMessage", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("forced ApplyMessage error"))
return ""
},
false,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest() // reset

newImplementationAddress := tc.malleate()

err := suite.app.ShariahOracleKeeper.UpdateCACContract(suite.ctx, newImplementationAddress)
suite.Commit()

if tc.expPass {
suite.Require().NoError(err, tc.name)
} else {
suite.Require().Error(err)
}
})
}
}
Loading

0 comments on commit 6224b42

Please sign in to comment.