github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/dex/handler_test.go (about)

     1  //go:build ignore
     2  
     3  package dex
     4  
     5  import (
     6  	"testing"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    10  	"github.com/fibonacci-chain/fbc/x/dex/types"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func getMockTestCaseEvn(t *testing.T) (mApp *mockApp,
    15  	tkKeeper *mockTokenKeeper, spKeeper *mockSupplyKeeper, dexKeeper *mockDexKeeper, testContext sdk.Context) {
    16  	fakeTokenKeeper := newMockTokenKeeper()
    17  	fakeSupplyKeeper := newMockSupplyKeeper()
    18  
    19  	mApp, mockDexKeeper, err := newMockApp(fakeTokenKeeper, fakeSupplyKeeper, 10)
    20  	require.True(t, err == nil)
    21  
    22  	mApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: 2}})
    23  	ctx := mApp.BaseApp.NewContext(false, abci.Header{})
    24  
    25  	return mApp, fakeTokenKeeper, fakeSupplyKeeper, mockDexKeeper, ctx
    26  }
    27  
    28  func TestHandler_HandleMsgBad(t *testing.T) {
    29  	mApp, _, _, _, ctx := getMockTestCaseEvn(t)
    30  	handlerFunctor := NewHandler(mApp.dexKeeper)
    31  
    32  	_, err := handlerFunctor(ctx, sdk.NewTestMsg())
    33  	require.NotNil(t, err)
    34  }
    35  
    36  func TestHandler_handleMsgTransferOwnership(t *testing.T) {
    37  	mApp, _, spKeeper, mDexKeeper, ctx := getMockTestCaseEvn(t)
    38  
    39  	tokenPair := GetBuiltInTokenPair()
    40  	err := mDexKeeper.SaveTokenPair(ctx, tokenPair)
    41  	require.Nil(t, err)
    42  	handlerFunctor := NewHandler(mApp.dexKeeper)
    43  	to := mApp.GenesisAccounts[0].GetAddress()
    44  
    45  	// successful case
    46  	msgTransferOwnership := types.NewMsgTransferOwnership(tokenPair.Owner, to, tokenPair.Name())
    47  	spKeeper.behaveEvil = false
    48  	handlerFunctor(ctx, msgTransferOwnership)
    49  
    50  	// fail case : failed to TransferOwnership because product is not exist
    51  	msgFailedTransferOwnership := types.NewMsgTransferOwnership(tokenPair.Owner, to, "no-product")
    52  	spKeeper.behaveEvil = false
    53  	handlerFunctor(ctx, msgFailedTransferOwnership)
    54  
    55  	// fail case : failed to SendCoinsFromModuleToAccount return error
    56  	msgFailedTransferOwnership = types.NewMsgTransferOwnership(tokenPair.Owner, to, tokenPair.Name())
    57  	spKeeper.behaveEvil = true
    58  	handlerFunctor(ctx, msgFailedTransferOwnership)
    59  
    60  	// fail case : failed to TransferOwnership because the address is not the owner of product
    61  	msgFailedTransferOwnership = types.NewMsgTransferOwnership(to, to, tokenPair.Name())
    62  	spKeeper.behaveEvil = false
    63  	handlerFunctor(ctx, msgFailedTransferOwnership)
    64  
    65  	// confirm ownership successful case
    66  	msgConfirmOwnership := types.NewMsgConfirmOwnership(to, tokenPair.Name())
    67  	spKeeper.behaveEvil = false
    68  	handlerFunctor(ctx, msgConfirmOwnership)
    69  
    70  	// fail case : failed to ConfirmOwnership because the address is not the new owner of product
    71  	msgTransferOwnership = types.NewMsgTransferOwnership(to, tokenPair.Owner, tokenPair.Name())
    72  	spKeeper.behaveEvil = false
    73  	handlerFunctor(ctx, msgTransferOwnership)
    74  	msgFailedConfirmOwnership := types.NewMsgConfirmOwnership(to, tokenPair.Name())
    75  	spKeeper.behaveEvil = false
    76  	handlerFunctor(ctx, msgFailedConfirmOwnership)
    77  
    78  	// fail case : failed to ConfirmOwnership because there is not transfer-ownership list to confirm
    79  	mDexKeeper.DeleteConfirmOwnership(ctx, tokenPair.Name())
    80  	msgFailedConfirmOwnership = types.NewMsgConfirmOwnership(tokenPair.Owner, tokenPair.Name())
    81  	spKeeper.behaveEvil = false
    82  	handlerFunctor(ctx, msgFailedConfirmOwnership)
    83  
    84  	// fail case : failed to ConfirmOwnership because the product is not exist
    85  	mDexKeeper.DeleteTokenPairByName(ctx, tokenPair.Owner, tokenPair.Name())
    86  	msgFailedConfirmOwnership = types.NewMsgConfirmOwnership(tokenPair.Owner, tokenPair.Name())
    87  	spKeeper.behaveEvil = false
    88  	handlerFunctor(ctx, msgFailedConfirmOwnership)
    89  }