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 TestStoreAndInstantiateContract #79

Merged
merged 11 commits into from
Aug 23, 2023
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
* [\#79](https://github.com/Finschia/wasmd/pull/79) add an integration test for TestStoreAndInstantiateContract

### Bug Fixes
* [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field
Expand Down
90 changes: 90 additions & 0 deletions x/wasmplus/keeper/msg_server_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package keeper_test

import (
_ "embed"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

sdk "github.com/Finschia/finschia-sdk/types"

"github.com/Finschia/wasmd/appplus"

wasmtypes "github.com/Finschia/wasmd/x/wasm/types"
"github.com/Finschia/wasmd/x/wasmplus/types"
)

//go:embed testdata/reflect.wasm
var wasmContract []byte

func TestStoreAndInstantiateContract(t *testing.T) {
wasmApp := appplus.Setup(false)
ctx := wasmApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})

var (
myAddress sdk.AccAddress = make([]byte, wasmtypes.ContractAddrLen)
)

specs := map[string]struct {
addr string
permission *wasmtypes.AccessConfig
expErr bool
}{
"address can instantiate a contract when permission is everybody": {
addr: myAddress.String(),
permission: &wasmtypes.AllowEverybody,
expErr: false,
},
"address cannot instantiate a contract when permission is nobody": {
addr: myAddress.String(),
permission: &wasmtypes.AllowNobody,
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
xCtx, _ := ctx.CacheContext()
// when
msg := &types.MsgStoreCodeAndInstantiateContract{
Sender: spec.addr,
WASMByteCode: wasmContract,
InstantiatePermission: spec.permission,
Admin: myAddress.String(),
Label: "test",
Msg: []byte(`{}`),
Funds: sdk.Coins{},
}
rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(xCtx, msg)

//then
da1suk8 marked this conversation as resolved.
Show resolved Hide resolved
if spec.expErr {
require.Error(t, err)
return
}

// check event
events := rsp.Events
assert.Equal(t, 3, len(events))
assert.Equal(t, "store_code", events[0].Type)
assert.Equal(t, 2, len(events[0].Attributes))
assert.Equal(t, "code_checksum", string(events[0].Attributes[0].Key))
assert.Equal(t, "code_id", string(events[0].Attributes[1].Key))
assert.Equal(t, "1", string(events[0].Attributes[1].Value))
assert.Equal(t, "message", events[1].Type)
assert.Equal(t, 2, len(events[1].Attributes))
assert.Equal(t, "module", string(events[1].Attributes[0].Key))
assert.Equal(t, "wasm", string(events[1].Attributes[0].Value))
assert.Equal(t, "sender", string(events[1].Attributes[1].Key))
assert.Equal(t, "instantiate", events[2].Type)
assert.Equal(t, "_contract_address", string(events[2].Attributes[0].Key))
assert.Contains(t, string(rsp.Data), string(events[2].Attributes[0].Value))
da1suk8 marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, "code_id", string(events[2].Attributes[1].Key))
assert.Equal(t, "1", string(events[2].Attributes[1].Value))

require.NoError(t, err)
})
}
}
Binary file added x/wasmplus/keeper/testdata/reflect.wasm
Binary file not shown.