add tests

This commit is contained in:
jhernandezb
2022-05-04 18:06:54 -06:00
parent 7c0763e57b
commit 01157fa914
2 changed files with 108 additions and 1 deletions

View File

@@ -749,3 +749,101 @@ func TestUnpinCodesProposal(t *testing.T) {
})
}
}
func TestUpdateInstantiateConfigProposal(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, "staking")
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
mock := wasmtesting.MockWasmer{
CreateFn: wasmtesting.NoOpCreateFn,
AnalyzeCodeFn: wasmtesting.WithoutIBCAnalyzeFn,
}
anyAddress, err := sdk.AccAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz")
require.NoError(t, err)
withAddressAccessConfig := types.AccessTypeOnlyAddress.With(anyAddress)
var (
nobody = StoreRandomContractWithAccessConfig(t, ctx, keepers, &mock, &types.AllowNobody)
everybody = StoreRandomContractWithAccessConfig(t, ctx, keepers, &mock, &types.AllowEverybody)
withAddress = StoreRandomContractWithAccessConfig(t, ctx, keepers, &mock, &withAddressAccessConfig)
)
type codeUpdate struct {
codeID uint64
AccessConfig types.AccessConfig
}
specs := map[string]struct {
codeUpdates []codeUpdate
expErr bool
}{
"update one": {
codeUpdates: []codeUpdate{
{codeID: nobody.CodeID, AccessConfig: types.AllowEverybody},
},
},
"update multiple": {
codeUpdates: []codeUpdate{
{codeID: everybody.CodeID, AccessConfig: types.AllowNobody},
{codeID: nobody.CodeID, AccessConfig: withAddressAccessConfig},
{codeID: withAddress.CodeID, AccessConfig: types.AllowEverybody},
},
},
"update same code id": {
codeUpdates: []codeUpdate{
{codeID: everybody.CodeID, AccessConfig: types.AllowNobody},
{codeID: everybody.CodeID, AccessConfig: types.AllowEverybody},
},
expErr: true,
},
"update non existing code id": {
codeUpdates: []codeUpdate{
{codeID: 100, AccessConfig: types.AllowNobody},
{codeID: everybody.CodeID, AccessConfig: types.AllowEverybody},
},
expErr: true,
},
"update empty list": {
codeUpdates: make([]codeUpdate, 0),
expErr: true,
},
}
parentCtx := ctx
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
ctx, _ := parentCtx.CacheContext()
updates := make([]types.CodeAccessConfigUpdate, 0)
for _, cu := range spec.codeUpdates {
updates = append(updates, types.CodeAccessConfigUpdate{
CodeID: cu.codeID,
InstantiatePermission: cu.AccessConfig,
})
}
proposal := types.UpdateInstantiateConfigProposal{
Title: "Foo",
Description: "Bar",
CodeUpdates: updates,
}
// when stored
storedProposal, gotErr := govKeeper.SubmitProposal(ctx, &proposal)
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
// and proposal execute
handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute())
gotErr = handler(ctx, storedProposal.GetContent())
require.NoError(t, gotErr)
// then
for i := range spec.codeUpdates {
c := wasmKeeper.GetCodeInfo(ctx, spec.codeUpdates[i].codeID)
require.Equal(t, spec.codeUpdates[i].AccessConfig, c.InstantiateConfig)
}
})
}
}

View File

@@ -586,16 +586,25 @@ func SeedNewContractInstance(t testing.TB, ctx sdk.Context, keepers TestKeepers,
// StoreRandomContract sets the mock wasmerEngine in keeper and calls store
func StoreRandomContract(t testing.TB, ctx sdk.Context, keepers TestKeepers, mock types.WasmerEngine) ExampleContract {
return StoreRandomContractWithAccessConfig(t, ctx, keepers, mock, nil)
}
func StoreRandomContractWithAccessConfig(
t testing.TB, ctx sdk.Context,
keepers TestKeepers,
mock types.WasmerEngine,
cfg *types.AccessConfig) ExampleContract {
t.Helper()
anyAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000))
creator, _, creatorAddr := keyPubAddr()
fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, creatorAddr, anyAmount)
keepers.WasmKeeper.wasmVM = mock
wasmCode := append(wasmIdent, rand.Bytes(10)...) //nolint:gocritic
codeID, err := keepers.ContractKeeper.Create(ctx, creatorAddr, wasmCode, nil)
codeID, err := keepers.ContractKeeper.Create(ctx, creatorAddr, wasmCode, cfg)
require.NoError(t, err)
exampleContract := ExampleContract{InitialAmount: anyAmount, Creator: creator, CreatorAddr: creatorAddr, CodeID: codeID}
return exampleContract
}
type HackatomExampleInstance struct {