From 8eca926fb45918ba25a2e0c1b02e7d25f79a8522 Mon Sep 17 00:00:00 2001 From: Daisuke Iuchi <42408108+da1suk8@users.noreply.github.com> Date: Wed, 16 Aug 2023 11:45:05 +0900 Subject: [PATCH] test: add event checking to TestInstantiateContract (#70) * test: add event checking to TestInstantiateContract * docs: add CHANGELOG --- CHANGELOG.md | 1 + x/wasm/keeper/msg_server_integration_test.go | 80 ++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6536d88ec6..9fcce35622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [\#65](https://github.com/Finschia/wasmd/pull/65) add test cases for empty request in each function * [\#66](https://github.com/Finschia/wasmd/pull/66) add test cases for invalid pagination key in some functions * [\#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 ### 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 5f1709193a..02a380d374 100644 --- a/x/wasm/keeper/msg_server_integration_test.go +++ b/x/wasm/keeper/msg_server_integration_test.go @@ -5,12 +5,14 @@ import ( _ "embed" "encoding/hex" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/Finschia/finschia-sdk/testutil/testdata" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/wasmd/app" "github.com/Finschia/wasmd/x/wasm/types" @@ -60,3 +62,81 @@ func TestStoreCode(t *testing.T) { assert.Equal(t, sender.String(), info.Creator) assert.Equal(t, types.DefaultParams().InstantiateDefaultPermission.With(sender), info.InstantiateConfig) } + +func TestInstantiateContract(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) + ) + + specs := map[string]struct { + addr string + permission *types.AccessConfig + expErr bool + }{ + "address can instantiate a contract when permission is everybody": { + addr: myAddress.String(), + permission: &types.AllowEverybody, + expErr: false, + }, + "address cannot instantiate a contract when permission is nobody": { + addr: myAddress.String(), + permission: &types.AllowNobody, + expErr: true, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + // setup + _, _, sender := testdata.KeyTestPubAddr() + msg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) { + m.WASMByteCode = wasmContract + m.Sender = sender.String() + m.InstantiatePermission = spec.permission + }) + + // store code + rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(ctx, msg) + require.NoError(t, err) + var result types.MsgStoreCodeResponse + require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &result)) + + // when + msgInstantiate := &types.MsgInstantiateContract{ + Sender: spec.addr, + Admin: myAddress.String(), + CodeID: result.CodeID, + Label: "test", + Msg: []byte(`{}`), + Funds: sdk.Coins{}, + } + rsp, err = wasmApp.MsgServiceRouter().Handler(msgInstantiate)(ctx, msgInstantiate) + + //then + if spec.expErr { + require.Error(t, err) + return + } + + // check event + events := rsp.Events + assert.Equal(t, 2, len(events)) + assert.Equal(t, "message", events[0].Type) + assert.Equal(t, 2, len(events[0].Attributes)) + assert.Equal(t, "module", string(events[0].Attributes[0].Key)) + assert.Equal(t, "wasm", string(events[0].Attributes[0].Value)) + assert.Equal(t, "sender", string(events[0].Attributes[1].Key)) + assert.Equal(t, myAddress.String(), string(events[0].Attributes[1].Value)) + assert.Equal(t, "instantiate", events[1].Type) + assert.Equal(t, 2, len(events[1].Attributes)) + assert.Equal(t, "_contract_address", string(events[1].Attributes[0].Key)) + assert.Contains(t, string(rsp.Data), string(events[1].Attributes[0].Value)) + assert.Equal(t, "code_id", string(events[1].Attributes[1].Key)) + assert.Equal(t, "1", string(events[1].Attributes[1].Value)) + + require.NoError(t, err) + }) + } +}