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 ClearAdmin #76

Merged
merged 13 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 @@ -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()),
},
},
}
}