Skip to content

Commit

Permalink
test: add an integration test for TestExecuteContract (#87)
Browse files Browse the repository at this point in the history
* test: add TestExecuteContract

* fix: add new line

* docs: add CHANGELOG

* fix: reflect comment

- change sender address
- move non-execute parts outside the loop
- change to use MsgExecuteContractFixture
  • Loading branch information
da1suk8 committed Aug 23, 2023
1 parent f36b389 commit f1e3820
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [\#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
* [\#82](https://github.com/Finschia/wasmd/pull/82) add test case to QueryInactiveContracts
* [\#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
105 changes: 105 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,108 @@ 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()
)

// setup
_, _, sender := testdata.KeyTestPubAddr()
msg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) {
m.WASMByteCode = hackatomContract
m.Sender = sender.String()
})

// store code
rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(ctx, 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.MsgInstantiateContractFixture(func(m *types.MsgInstantiateContract) {
m.Sender = sender.String()
m.Admin = myAddress.String()
m.CodeID = storeCodeResponse.CodeID
m.Label = "test"
m.Msg = initMsgBz
m.Funds = sdk.Coins{}
})
rsp, err = wasmApp.MsgServiceRouter().Handler(msgInstantiate)(ctx, msgInstantiate)
require.NoError(t, err)
var instantiateResponse types.MsgInstantiateContractResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateResponse))

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

// when
msgExecuteContract := types.MsgExecuteContractFixture(func(m *types.MsgExecuteContract) {
m.Sender = spec.addr
m.Msg = []byte(`{"release":{}}`)
m.Contract = instantiateResponse.Address
m.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)
})
}
}

0 comments on commit f1e3820

Please sign in to comment.