Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add an integration test for TestExecuteContract #87

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [\#78](https://github.com/Finschia/wasmd/pull/78) add the check for TestMigrateContract
* [\#69](https://github.com/Finschia/wasmd/pull/69) refactor: refactor test cases for Params
* [\#71](https://github.com/Finschia/wasmd/pull/71) add test cases in ContractsByCode
* [\#87](https://github.com/Finschia/wasmd/pull/87) add an integration test for TestExecuteContract

### Bug Fixes
* [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field
Expand Down
103 changes: 103 additions & 0 deletions x/wasm/keeper/msg_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,106 @@ func TestMigrateContract(t *testing.T) {
})
}
}

func TestExecuteContract(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()
)

specs := map[string]struct {
addr string
expErr bool
}{
"adress can execute a contract": {
addr: myAddress.String(),
expErr: false,
},
"other address cannot execute a contract": {
addr: otherAddr.String(),
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
xCtx, _ := ctx.CacheContext()
// setup
_, _, sender := testdata.KeyTestPubAddr()
msg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) {
m.WASMByteCode = hackatomContract
m.Sender = sender.String()
})
170210 marked this conversation as resolved.
Show resolved Hide resolved

// store code
rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(xCtx, msg)
require.NoError(t, err)
var storeCodeResponse types.MsgStoreCodeResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &storeCodeResponse))

// instantiate contract
initMsg := keeper.HackatomExampleInitMsg{
Verifier: myAddress,
Beneficiary: otherAddr,
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
msgInstantiate := &types.MsgInstantiateContract{
Sender: spec.addr,
170210 marked this conversation as resolved.
Show resolved Hide resolved
Admin: myAddress.String(),
CodeID: storeCodeResponse.CodeID,
Label: "test",
Msg: initMsgBz,
Funds: sdk.Coins{},
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgInstantiate)(xCtx, msgInstantiate)
require.NoError(t, err)
var instantiateResponse types.MsgInstantiateContractResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateResponse))
170210 marked this conversation as resolved.
Show resolved Hide resolved

msgExecuteContract := &types.MsgExecuteContract{
Sender: spec.addr,
Msg: []byte(`{"release":{}}`),
Contract: instantiateResponse.Address,
Funds: sdk.Coins{},
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgExecuteContract)(xCtx, msgExecuteContract)

// then
if spec.expErr {
require.Error(t, err)
return
}

// check event
events := rsp.Events
assert.Equal(t, 4, 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, "execute", events[1].Type)
assert.Equal(t, 1, len(events[1].Attributes))
assert.Equal(t, "_contract_address", string(events[1].Attributes[0].Key))
assert.Equal(t, instantiateResponse.Address, string(events[1].Attributes[0].Value))
assert.Equal(t, "wasm", events[2].Type)
assert.Equal(t, 3, len(events[2].Attributes))
assert.Equal(t, "_contract_address", string(events[2].Attributes[0].Key))
assert.Equal(t, instantiateResponse.Address, string(events[2].Attributes[0].Value))
assert.Equal(t, "action", string(events[2].Attributes[1].Key))
assert.Equal(t, "release", string(events[2].Attributes[1].Value))
assert.Equal(t, "destination", string(events[2].Attributes[2].Key))
assert.Equal(t, "wasm-hackatom", events[3].Type)
assert.Equal(t, "_contract_address", string(events[3].Attributes[0].Key))
assert.Equal(t, instantiateResponse.Address, string(events[3].Attributes[0].Value))
assert.Equal(t, "action", string(events[3].Attributes[1].Key))
assert.Equal(t, "release", string(events[3].Attributes[1].Value))

require.NoError(t, err)
})
}
}