Skip to content

Commit

Permalink
test: add event checking to TestInstantiateContract (#70)
Browse files Browse the repository at this point in the history
* test: add event checking to TestInstantiateContract

* docs: add CHANGELOG
  • Loading branch information
da1suk8 authored Aug 16, 2023
1 parent 484e437 commit 8eca926
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions x/wasm/keeper/msg_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 8eca926

Please sign in to comment.