Skip to content

Commit

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

Signed-off-by: 170210 <[email protected]>

* chore: add this PR to CHANGELOG

Signed-off-by: 170210 <[email protected]>

* fixup: fix for comment

Signed-off-by: 170210 <[email protected]>

* fixup: use fixture func

Signed-off-by: 170210 <[email protected]>

* fixup: change import name

Signed-off-by: 170210 <[email protected]>

* fixup: modify createMsgEvent function

Signed-off-by: 170210 <[email protected]>

* fixup: move utility function into test_fixtures

Signed-off-by: 170210 <[email protected]>

* fixup: revert utility function relocation

Signed-off-by: 170210 <[email protected]>

---------

Signed-off-by: 170210 <[email protected]>
  • Loading branch information
170210 authored Aug 23, 2023
1 parent 36ce1d3 commit 025a70b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [\#87](https://github.com/Finschia/wasmd/pull/87) add an integration test for TestExecuteContract
* [\#79](https://github.com/Finschia/wasmd/pull/79) add an integration test for TestStoreAndInstantiateContract
* [\#88](https://github.com/Finschia/wasmd/pull/88) add the test case for invalid address
* [\#76](https://github.com/Finschia/wasmd/pull/76) add an integration test for ClearAdmin

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

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

"github.com/Finschia/finschia-sdk/testutil/testdata"
Expand Down Expand Up @@ -438,3 +439,108 @@ func TestExecuteContract(t *testing.T) {
})
}
}

func TestClearAdmin(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()
storeMsg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) {
m.Sender = sender.String()
m.WASMByteCode = wasmContract
})
rsp, err := wasmApp.MsgServiceRouter().Handler(storeMsg)(ctx, storeMsg)
require.NoError(t, err)
var storeCodeResult types.MsgStoreCodeResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &storeCodeResult))
codeID := storeCodeResult.CodeID

initMsg := types.MsgInstantiateContractFixture(func(m *types.MsgInstantiateContract) {
m.Sender = myAddress.String()
m.Admin = myAddress.String()
m.CodeID = codeID
m.Label = "test"
m.Msg = []byte(`{}`)
m.Funds = sdk.Coins{}
})
rsp, err = wasmApp.MsgServiceRouter().Handler(initMsg)(ctx, initMsg)
require.NoError(t, err)

var instantiateContractResult types.MsgInstantiateContractResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateContractResult))
contractAddress := instantiateContractResult.Address

specs := map[string]struct {
addr string
expErr bool
expEvents []abci.Event
}{
"admin can clear admin": {
addr: myAddress.String(),
expErr: false,
expEvents: []abci.Event{
createMsgEvent(myAddress),
{
Type: "update_contract_admin",
Attributes: []abci.EventAttribute{
{
Key: []byte("_contract_address"),
Value: []byte(contractAddress),
},
{
Key: []byte("new_admin_address"),
Value: []byte{},
},
},
},
},
},
"other address cannot clear admin": {
addr: otherAddr.String(),
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
xCtx, _ := ctx.CacheContext()
// when
msgClearAdmin := &types.MsgClearAdmin{
Sender: spec.addr,
Contract: contractAddress,
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgClearAdmin)(xCtx, msgClearAdmin)

// then
if spec.expErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, spec.expEvents, rsp.Events)
})
}
}

// This function is utilized to generate the msg event for event checking in integration tests
// It will be deleted in release/v0.1.x
func createMsgEvent(sender sdk.AccAddress) abci.Event {
return abci.Event{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("module"),
Value: []byte("wasm"),
},
{
Key: []byte("sender"),
Value: []byte(sender.String()),
},
},
}
}

0 comments on commit 025a70b

Please sign in to comment.