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 TestMigrateContract #78

Merged
merged 9 commits into from
Aug 18, 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
* [\#78](https://github.com/Finschia/wasmd/pull/78) test: add the check for TestMigrateContract
da1suk8 marked this conversation as resolved.
Show resolved Hide resolved

### 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 @@ -4,6 +4,7 @@ import (
"crypto/sha256"
_ "embed"
"encoding/hex"
"encoding/json"
"testing"
"time"

Expand All @@ -15,12 +16,16 @@ import (
sdk "github.com/Finschia/finschia-sdk/types"

"github.com/Finschia/wasmd/app"
"github.com/Finschia/wasmd/x/wasm/keeper"
"github.com/Finschia/wasmd/x/wasm/types"
)

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

//go:embed testdata/hackatom.wasm
var hackatomContract []byte

func TestStoreCode(t *testing.T) {
wasmApp := app.Setup(false)
ctx := wasmApp.BaseApp.NewContext(false, tmproto.Header{})
Expand Down Expand Up @@ -141,3 +146,103 @@ func TestInstantiateContract(t *testing.T) {
})
}
}

func TestMigrateContract(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
}{
"admin can migrate a contract": {
addr: myAddress.String(),
expErr: false,
},
"other address cannot migrate 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()
})

// 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: sender,
Beneficiary: myAddress,
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

msgInstantiate := &types.MsgInstantiateContract{
Sender: sender.String(),
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))

// when
migMsg := struct {
Verifier sdk.AccAddress `json:"verifier"`
}{Verifier: myAddress}
migMsgBz, err := json.Marshal(migMsg)
require.NoError(t, err)
msgMigrateContract := &types.MsgMigrateContract{
Sender: spec.addr,
Msg: migMsgBz,
Contract: instantiateResponse.Address,
CodeID: storeCodeResponse.CodeID,
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgMigrateContract)(xCtx, msgMigrateContract)

// 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, "migrate", events[1].Type)
assert.Equal(t, 2, len(events[1].Attributes))
assert.Equal(t, "code_id", string(events[1].Attributes[0].Key))
assert.Equal(t, "1", string(events[1].Attributes[0].Value))
assert.Equal(t, "_contract_address", string(events[1].Attributes[1].Key))
assert.Equal(t, instantiateResponse.Address, string(events[1].Attributes[1].Value))

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