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 @@ -20,6 +20,7 @@
* [\#71](https://github.com/Finschia/wasmd/pull/71) add test cases in ContractsByCode
* [\#82](https://github.com/Finschia/wasmd/pull/82) add test case to QueryInactiveContracts
* [\#87](https://github.com/Finschia/wasmd/pull/87) add an integration test for TestExecuteContract
* [\#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
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 @@ -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,107 @@ 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)
})
}
}

func createMsgEvent(sender sdk.AccAddress) abci.Event {
// msg event
return abci.Event{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("module"),
Value: []byte("wasm"),
},
{
Key: []byte("sender"),
Value: []byte(sender.String()),
},
},
}
}