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 UpdateAdmin #68

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Improvements
* [\#63](https://github.com/Finschia/wasmd/pull/63) add event checking to TestStoreCode
* [\#65](https://github.com/Finschia/wasmd/pull/65) add test cases for empty request in each function
* [\#68](https://github.com/Finschia/wasmd/pull/68) add an integration test for UpdateAdmin

### Bug Fixes
* [\#52](https://github.com/Finschia/wasmd/pull/52) fix cli_test error of wasmplus and add cli_test ci
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 @@ -5,12 +5,15 @@ import (
_ "embed"
"encoding/hex"
"testing"
"time"

"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"
sdk "github.com/Finschia/finschia-sdk/types"

"github.com/Finschia/wasmd/app"
"github.com/Finschia/wasmd/x/wasm/types"
Expand Down Expand Up @@ -60,3 +63,106 @@ func TestStoreCode(t *testing.T) {
assert.Equal(t, sender.String(), info.Creator)
assert.Equal(t, types.DefaultParams().InstantiateDefaultPermission.With(sender), info.InstantiateConfig)
}

func TestUpdateAdmin(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()
_, _, newAdmin = testdata.KeyTestPubAddr()
)

// setup
storeMsg := &types.MsgStoreCode{
Sender: myAddress.String(),
WASMByteCode: wasmContract,
InstantiatePermission: &types.AllowEverybody,
}
loloicci marked this conversation as resolved.
Show resolved Hide resolved
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.MsgInstantiateContract{
loloicci marked this conversation as resolved.
Show resolved Hide resolved
Sender: myAddress.String(),
Admin: myAddress.String(),
CodeID: codeID,
Label: "test",
Msg: []byte(`{}`),
Funds: sdk.Coins{},
170210 marked this conversation as resolved.
Show resolved Hide resolved
}
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 update admin": {
addr: myAddress.String(),
expErr: false,
expEvents: []abci.Event{
{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("module"),
Value: []byte("wasm"),
},
{
Key: []byte("sender"),
Value: []byte(myAddress.String()),
},
},
},
{
Type: "update_contract_admin",
Attributes: []abci.EventAttribute{
{
Key: []byte("_contract_address"),
Value: []byte(contractAddress),
},
{
Key: []byte("new_admin_address"),
Value: []byte(newAdmin.String()),
},
},
},
},
},
"other address cannot update admin": {
addr: otherAddr.String(),
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
xCtx, _ := ctx.CacheContext()

// when
msgUpdateAdmin := &types.MsgUpdateAdmin{
Sender: spec.addr,
NewAdmin: newAdmin.String(),
Contract: contractAddress,
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgUpdateAdmin)(xCtx, msgUpdateAdmin)

// then
if spec.expErr {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, spec.expEvents, rsp.Events)
})
}
}