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 event checking to TestInstantiateContract2 #74

Merged
merged 7 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -14,6 +14,7 @@
* [\#73](https://github.com/Finschia/wasmd/pull/73) test: add the check for expPaginationTotal
* [\#72](https://github.com/Finschia/wasmd/pull/72) add pagination next key test in ContractHistory
* [\#75](https://github.com/Finschia/wasmd/pull/75) test: add the test case for InactiveContract
* [\#74](https://github.com/Finschia/wasmd/pull/74) add event checking to TestInstantiateContract2

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

func TestInstantiateContract2(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
salt string
expErr bool
}{
"address can instantiate a contract when permission is everybody": {
addr: myAddress.String(),
permission: &types.AllowEverybody,
salt: "salt1",
expErr: false,
},
"address cannot instantiate a contract when permission is nobody": {
addr: myAddress.String(),
permission: &types.AllowNobody,
salt: "salt2",
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 = wasmContract
m.Sender = sender.String()
m.InstantiatePermission = spec.permission
})

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

// when
msgInstantiate := &types.MsgInstantiateContract2{
Sender: spec.addr,
Admin: myAddress.String(),
CodeID: result.CodeID,
Label: "test",
Msg: []byte(`{}`),
Funds: sdk.Coins{},
Salt: []byte(spec.salt),
FixMsg: true,
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgInstantiate)(xCtx, msgInstantiate)

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

var instantiateResponse types.MsgInstantiateContractResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateResponse))

// 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.Equal(t, instantiateResponse.Address, 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)
})
}
}