From 8109e74868a661d555dcf9a9d8dc7fae28d8d723 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 16 Aug 2023 16:08:34 +0900 Subject: [PATCH 1/8] test: add an integration test for ClearAdmin Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 110 +++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index 02a380d374..dadec79f82 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -4,11 +4,13 @@ import ( "crypto/sha256" _ "embed" "encoding/hex" + "reflect" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + types1 "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/Finschia/finschia-sdk/testutil/testdata" @@ -140,3 +142,111 @@ func TestInstantiateContract(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 + storeMsg := &types.MsgStoreCode{ + Sender: myAddress.String(), + WASMByteCode: wasmContract, + InstantiatePermission: &types.AllowEverybody, + } + 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.MsgInstantiateContract{ + Sender: myAddress.String(), + Admin: myAddress.String(), + CodeID: codeID, + Label: "test", + Msg: []byte(`{}`), + 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 []types1.Event + }{ + "admin can clear admin": { + addr: myAddress.String(), + expErr: false, + expEvents: createExpEvents(myAddress, + []types1.Event{ + { + Type: "update_contract_admin", + Attributes: []types1.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.True(t, reflect.DeepEqual(spec.expEvents, rsp.Events)) + }) + } +} + +func createExpEvents(sender sdk.AccAddress, specEvent []types1.Event) []types1.Event { + // origin event + expEvents := []types1.Event{ + { + Type: "message", + Attributes: []types1.EventAttribute{ + { + Key: []byte("module"), + Value: []byte("wasm"), + }, + { + Key: []byte("sender"), + Value: []byte(sender.String()), + }, + }, + }, + } + return append(expEvents, specEvent...) +} From 9422f98de0c865b7b7319121cfc6662c7d66cce5 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 16 Aug 2023 17:14:33 +0900 Subject: [PATCH 2/8] chore: add this PR to CHANGELOG Signed-off-by: 170210 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b086c5181f..de1f0fb05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [\#64](https://github.com/Finschia/wasmd/pull/64) test: add test cases to confirm output for PinnedCodes * [\#70](https://github.com/Finschia/wasmd/pull/70) add event checking to TestInstantiateContract * [\#73](https://github.com/Finschia/wasmd/pull/73) test: add the check for expPaginationTotal +* [\#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 From a560d96451c3428635512f1058c6ac9ee5dbf298 Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 21 Aug 2023 14:49:39 +0900 Subject: [PATCH 3/8] fixup: fix for comment Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index 9f0d28ca52..b5c9f04002 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -4,7 +4,6 @@ import ( "crypto/sha256" _ "embed" "encoding/hex" - "reflect" "testing" "time" @@ -227,7 +226,7 @@ func TestClearAdmin(t *testing.T) { return } require.NoError(t, err) - assert.True(t, reflect.DeepEqual(spec.expEvents, rsp.Events)) + assert.Equal(t, spec.expEvents, rsp.Events) }) } } From 5ee0cbb75db2607f91cb591715585ca220594bc0 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 22 Aug 2023 17:09:13 +0900 Subject: [PATCH 4/8] fixup: use fixture func Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 26 ++++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index 0f47f530be..9b5db4e974 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -345,25 +345,25 @@ func TestClearAdmin(t *testing.T) { ) // setup - storeMsg := &types.MsgStoreCode{ - Sender: myAddress.String(), - WASMByteCode: wasmContract, - InstantiatePermission: &types.AllowEverybody, - } + _, _, 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.MsgInstantiateContract{ - Sender: myAddress.String(), - Admin: myAddress.String(), - CodeID: codeID, - Label: "test", - Msg: []byte(`{}`), - Funds: sdk.Coins{}, - } + 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) From c6b7b20238d52f4f7d0cbc0bd375cce99fa362df Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 22 Aug 2023 17:11:11 +0900 Subject: [PATCH 5/8] fixup: change import name Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index 9b5db4e974..1bfd780716 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - types1 "github.com/tendermint/tendermint/abci/types" + abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/Finschia/finschia-sdk/testutil/testdata" @@ -374,16 +374,16 @@ func TestClearAdmin(t *testing.T) { specs := map[string]struct { addr string expErr bool - expEvents []types1.Event + expEvents []abci.Event }{ "admin can clear admin": { addr: myAddress.String(), expErr: false, expEvents: createExpEvents(myAddress, - []types1.Event{ + []abci.Event{ { Type: "update_contract_admin", - Attributes: []types1.EventAttribute{ + Attributes: []abci.EventAttribute{ { Key: []byte("_contract_address"), Value: []byte(contractAddress), @@ -423,12 +423,12 @@ func TestClearAdmin(t *testing.T) { } } -func createExpEvents(sender sdk.AccAddress, specEvent []types1.Event) []types1.Event { +func createExpEvents(sender sdk.AccAddress, specEvent []abci.Event) []abci.Event { // origin event - expEvents := []types1.Event{ + expEvents := []abci.Event{ { Type: "message", - Attributes: []types1.EventAttribute{ + Attributes: []abci.EventAttribute{ { Key: []byte("module"), Value: []byte("wasm"), From 6aa08d7b313951eca3ecaf97ad9c81a2915bfb72 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 22 Aug 2023 19:02:42 +0900 Subject: [PATCH 6/8] fixup: modify createMsgEvent function Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 54 +++++++++----------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index 1bfd780716..d70ddbd1ca 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -379,23 +379,22 @@ func TestClearAdmin(t *testing.T) { "admin can clear admin": { addr: myAddress.String(), expErr: false, - expEvents: createExpEvents(myAddress, - []abci.Event{ - { - Type: "update_contract_admin", - Attributes: []abci.EventAttribute{ - { - Key: []byte("_contract_address"), - Value: []byte(contractAddress), - }, - { - Key: []byte("new_admin_address"), - Value: []byte{}, - }, + 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(), @@ -423,22 +422,19 @@ func TestClearAdmin(t *testing.T) { } } -func createExpEvents(sender sdk.AccAddress, specEvent []abci.Event) []abci.Event { - // origin event - expEvents := []abci.Event{ - { - Type: "message", - Attributes: []abci.EventAttribute{ - { - Key: []byte("module"), - Value: []byte("wasm"), - }, - { - Key: []byte("sender"), - Value: []byte(sender.String()), - }, +func createMsgEvent(sender sdk.AccAddress) abci.Event { + // msg event + return abci.Event{ + Type: "message", + Attributes: []abci.EventAttribute{ + { + Key: []byte("module"), + Value: []byte("wasm"), + }, + { + Key: []byte("sender"), + Value: []byte(sender.String()), }, }, } - return append(expEvents, specEvent...) } From e4d13241b9d5d4a2bd0f8fb188ec9ad68da3c7f7 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 23 Aug 2023 16:16:42 +0900 Subject: [PATCH 7/8] fixup: move utility function into test_fixtures Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 19 +------------------ x/wasm/types/test_fixtures.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index c0c1942f61..cf2681462e 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -485,7 +485,7 @@ func TestClearAdmin(t *testing.T) { addr: myAddress.String(), expErr: false, expEvents: []abci.Event{ - createMsgEvent(myAddress), + types.CreateMsgEvent(myAddress), { Type: "update_contract_admin", Attributes: []abci.EventAttribute{ @@ -526,20 +526,3 @@ func TestClearAdmin(t *testing.T) { }) } } - -func createMsgEvent(sender sdk.AccAddress) abci.Event { - // msg event - return abci.Event{ - Type: "message", - Attributes: []abci.EventAttribute{ - { - Key: []byte("module"), - Value: []byte("wasm"), - }, - { - Key: []byte("sender"), - Value: []byte(sender.String()), - }, - }, - } -} diff --git a/x/wasm/types/test_fixtures.go b/x/wasm/types/test_fixtures.go index dce20e0db2..7c95a3aabc 100644 --- a/x/wasm/types/test_fixtures.go +++ b/x/wasm/types/test_fixtures.go @@ -7,6 +7,7 @@ import ( "math/rand" sdk "github.com/Finschia/finschia-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) func GenesisFixture(mutators ...func(*GenesisState)) GenesisState { @@ -329,3 +330,20 @@ func ClearAdminProposalFixture(mutators ...func(p *ClearAdminProposal)) *ClearAd } return p } + +// This function is utilized to generate the msg event for event checking in integration tests. +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()), + }, + }, + } +} From 0837a3126217aca6d68a4b05b52919d249d229b9 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 23 Aug 2023 19:14:14 +0900 Subject: [PATCH 8/8] fixup: revert utility function relocation Signed-off-by: 170210 --- x/wasm/keeper/msg_server_integration_test.go | 20 +++++++++++++++++++- x/wasm/types/test_fixtures.go | 18 ------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/x/wasm/keeper/msg_server_integration_test.go b/x/wasm/keeper/msg_server_integration_test.go index cf2681462e..2a0a1aedfb 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -485,7 +485,7 @@ func TestClearAdmin(t *testing.T) { addr: myAddress.String(), expErr: false, expEvents: []abci.Event{ - types.CreateMsgEvent(myAddress), + createMsgEvent(myAddress), { Type: "update_contract_admin", Attributes: []abci.EventAttribute{ @@ -526,3 +526,21 @@ func TestClearAdmin(t *testing.T) { }) } } + +// 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()), + }, + }, + } +} diff --git a/x/wasm/types/test_fixtures.go b/x/wasm/types/test_fixtures.go index 7c95a3aabc..dce20e0db2 100644 --- a/x/wasm/types/test_fixtures.go +++ b/x/wasm/types/test_fixtures.go @@ -7,7 +7,6 @@ import ( "math/rand" sdk "github.com/Finschia/finschia-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" ) func GenesisFixture(mutators ...func(*GenesisState)) GenesisState { @@ -330,20 +329,3 @@ func ClearAdminProposalFixture(mutators ...func(p *ClearAdminProposal)) *ClearAd } return p } - -// This function is utilized to generate the msg event for event checking in integration tests. -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()), - }, - }, - } -}