From 025a70b3083dd1034e788fa9c96ff5f2649caf68 Mon Sep 17 00:00:00 2001 From: 170210 <85928898+170210@users.noreply.github.com> Date: Wed, 23 Aug 2023 19:36:39 +0900 Subject: [PATCH] test: add an integration test for ClearAdmin (#76) * test: add an integration test for ClearAdmin Signed-off-by: 170210 * chore: add this PR to CHANGELOG Signed-off-by: 170210 * fixup: fix for comment Signed-off-by: 170210 * fixup: use fixture func Signed-off-by: 170210 * fixup: change import name Signed-off-by: 170210 * fixup: modify createMsgEvent function Signed-off-by: 170210 * fixup: move utility function into test_fixtures Signed-off-by: 170210 * fixup: revert utility function relocation Signed-off-by: 170210 --------- Signed-off-by: 170210 --- CHANGELOG.md | 1 + x/wasm/keeper/msg_server_integration_test.go | 106 +++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c223eba866..646da12cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * [\#87](https://github.com/Finschia/wasmd/pull/87) add an integration test for TestExecuteContract * [\#79](https://github.com/Finschia/wasmd/pull/79) add an integration test for TestStoreAndInstantiateContract * [\#88](https://github.com/Finschia/wasmd/pull/88) add the test case for invalid address +* [\#76](https://github.com/Finschia/wasmd/pull/76) add an integration test for ClearAdmin ### Bug Fixes * [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index ec9c2f0bf1..2a0a1aedfb 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/Finschia/finschia-sdk/testutil/testdata" @@ -438,3 +439,108 @@ func TestExecuteContract(t *testing.T) { }) } } + +func TestClearAdmin(t *testing.T) { + wasmApp := app.Setup(false) + ctx := wasmApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()}) + + var ( + myAddress sdk.AccAddress = make([]byte, types.ContractAddrLen) + _, _, otherAddr = testdata.KeyTestPubAddr() + ) + + // setup + _, _, sender := testdata.KeyTestPubAddr() + storeMsg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) { + m.Sender = sender.String() + m.WASMByteCode = wasmContract + }) + rsp, err := wasmApp.MsgServiceRouter().Handler(storeMsg)(ctx, storeMsg) + require.NoError(t, err) + var storeCodeResult types.MsgStoreCodeResponse + require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &storeCodeResult)) + codeID := storeCodeResult.CodeID + + initMsg := types.MsgInstantiateContractFixture(func(m *types.MsgInstantiateContract) { + m.Sender = myAddress.String() + m.Admin = myAddress.String() + m.CodeID = codeID + m.Label = "test" + m.Msg = []byte(`{}`) + m.Funds = sdk.Coins{} + }) + rsp, err = wasmApp.MsgServiceRouter().Handler(initMsg)(ctx, initMsg) + require.NoError(t, err) + + var instantiateContractResult types.MsgInstantiateContractResponse + require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateContractResult)) + contractAddress := instantiateContractResult.Address + + specs := map[string]struct { + addr string + expErr bool + expEvents []abci.Event + }{ + "admin can clear admin": { + addr: myAddress.String(), + expErr: false, + expEvents: []abci.Event{ + createMsgEvent(myAddress), + { + Type: "update_contract_admin", + Attributes: []abci.EventAttribute{ + { + Key: []byte("_contract_address"), + Value: []byte(contractAddress), + }, + { + Key: []byte("new_admin_address"), + Value: []byte{}, + }, + }, + }, + }, + }, + "other address cannot clear admin": { + addr: otherAddr.String(), + expErr: true, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + xCtx, _ := ctx.CacheContext() + // when + msgClearAdmin := &types.MsgClearAdmin{ + Sender: spec.addr, + Contract: contractAddress, + } + rsp, err = wasmApp.MsgServiceRouter().Handler(msgClearAdmin)(xCtx, msgClearAdmin) + + // then + if spec.expErr { + require.Error(t, err) + return + } + require.NoError(t, err) + assert.Equal(t, spec.expEvents, rsp.Events) + }) + } +} + +// This function is utilized to generate the msg event for event checking in integration tests +// It will be deleted in release/v0.1.x +func createMsgEvent(sender sdk.AccAddress) abci.Event { + return abci.Event{ + Type: "message", + Attributes: []abci.EventAttribute{ + { + Key: []byte("module"), + Value: []byte("wasm"), + }, + { + Key: []byte("sender"), + Value: []byte(sender.String()), + }, + }, + } +}