Skip to content

Commit

Permalink
Params tests (#98)
Browse files Browse the repository at this point in the history
* Test: add IsAllowedSender test

* Test: test for validateAllowList

* Tidy: readability of params_test.go

* Refactor: change WhiteList to AllowList

* Refactor: correct and tidy code

* tidy: remove unnecessary file

* tidy: run goimports

* Tidy: call goimports on all x/airdrop

* Tidy: remove junk code from genesis.go
  • Loading branch information
Josumner authored Jul 15, 2021
1 parent 9b3f86e commit 90b1433
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 36 deletions.
6 changes: 3 additions & 3 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

// AnyUnpacker is an interface which allows safely unpacking types packed
// in Any's against a whitelist of registered types
// in Any's against a allowlist of registered types
type AnyUnpacker interface {
// UnpackAny unpacks the value in any to the interface pointer passed in as
// iface. Note that the type in any must have been registered in the
// underlying whitelist registry as a concrete type for that interface
// underlying allowlist registry as a concrete type for that interface
// Ex:
// var msg sdk.Msg
// err := cdc.UnpackAny(any, &msg)
Expand Down Expand Up @@ -67,7 +67,7 @@ type InterfaceRegistry interface {

// UnpackInterfacesMessage is meant to extend protobuf types (which implement
// proto.Message) to support a post-deserialization phase which unpacks
// types packed within Any's using the whitelist provided by AnyUnpacker
// types packed within Any's using the allowlist provided by AnyUnpacker
type UnpackInterfacesMessage interface {
// UnpackInterfaces is implemented in order to unpack values packed within
// Any's using the AnyUnpacker. It should generally be implemented as
Expand Down
3 changes: 2 additions & 1 deletion x/airdrop/abci.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package airdrop

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/airdrop/keeper"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"time"
)

func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
Expand Down
3 changes: 2 additions & 1 deletion x/airdrop/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cli

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"github.com/spf13/cobra"
"strings"
)

func GetQueryCmd() *cobra.Command {
Expand Down
3 changes: 2 additions & 1 deletion x/airdrop/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cli

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -10,7 +12,6 @@ import (
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"github.com/spf13/cobra"
"strings"
)

func GetTxCmd() *cobra.Command {
Expand Down
1 change: 1 addition & 0 deletions x/airdrop/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down
3 changes: 2 additions & 1 deletion x/airdrop/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package keeper_test
import (
gocontext "context"
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"testing"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion x/airdrop/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (k Keeper) AddFund(ctx sdk.Context, sender sdk.AccAddress, fund types.Fund)
if !params.IsAllowedSender(sender) {
return sdkerrors.Wrapf(
sdkerrors.ErrConflict,
"Non-whitelist sender %s", sender.String(),
"Non-allowlist sender %s", sender.String(),
)
}

Expand Down
5 changes: 3 additions & 2 deletions x/airdrop/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package keeper_test

import (
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/suite"
Expand Down
1 change: 1 addition & 0 deletions x/airdrop/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
)
Expand Down
6 changes: 3 additions & 3 deletions x/airdrop/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
)

func (k Keeper) GetWhiteListClients(ctx sdk.Context) []string {
func (k Keeper) GetAllowListClients(ctx sdk.Context) []string {
var res []string
k.paramSpace.GetIfExists(ctx, types.KeyWhiteList, &res)
k.paramSpace.Get(ctx, types.KeyAllowList, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetWhiteListClients(ctx)...)
return types.NewParams(k.GetAllowListClients(ctx)...)
}

// SetParams sets the total set of ibc-transfer parameters.
Expand Down
1 change: 1 addition & 0 deletions x/airdrop/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down
7 changes: 4 additions & 3 deletions x/airdrop/types/airdrop.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions x/airdrop/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func DefaultGenesisState() *GenesisState {
Expand All @@ -14,14 +15,11 @@ func NewGenesisState(params Params, funds []ActiveFund) *GenesisState {
}

func (gs *GenesisState) Validate() error {
whiteListAddresses := []sdk.AccAddress{}

for _, address := range gs.Params.AllowList {
accAddress, err := sdk.AccAddressFromBech32(strings.TrimSpace(address))
_, err := sdk.AccAddressFromBech32(strings.TrimSpace(address))
if err != nil {
return err
}
whiteListAddresses = append(whiteListAddresses, accAddress)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions x/airdrop/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions x/airdrop/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package types

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"strings"
)

var (
// KeyWhiteList is store's key for WhiteList Params
KeyWhiteList = []byte("AllowList")
// KeyAllowList is store's key for AllowList Params
KeyAllowList = []byte("AllowList")
)

func NewParams(whiteListClients ...string) Params {
func NewParams(allowListClients ...string) Params {
return Params{
AllowList: whiteListClients,
AllowList: allowListClients,
}
}

Expand All @@ -24,11 +25,11 @@ func ParamKeyTable() paramtypes.KeyTable {

func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyWhiteList, &p.AllowList, validateWhiteList),
paramtypes.NewParamSetPair(KeyAllowList, &p.AllowList, validateAllowList),
}
}

func validateWhiteList(i interface{}) error {
func validateAllowList(i interface{}) error {
clients, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
Expand Down
55 changes: 55 additions & 0 deletions x/airdrop/types/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types/time"
)

var (
allowedAddress = sdk.AccAddress([]byte("allowed_____________"))
blockedAddress1 = sdk.AccAddress([]byte("blocked_____________"))
blockedAddress2 = sdk.AccAddress([]byte(""))
)

type ParamsTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
}

func (s *ParamsTestSuite) SetupTest() {
app := simapp.Setup(false)
s.app = app
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{
Time: time.Now(),
Height: 10,
})
}

func (s *ParamsTestSuite) TestIsAllowedSender() {
correct := types.NewParams(allowedAddress.String()) // New set of parameters with a new AllowList
s.Require().True(correct.IsAllowedSender(allowedAddress)) // Address is in AllowList and has correct format
s.Require().False(correct.IsAllowedSender(blockedAddress1)) // Address is not in AllowList and has correct format
s.Require().False(correct.IsAllowedSender(blockedAddress2)) // Address is not in AllowList and has incorrect format
}

func (s *ParamsTestSuite) TestValidateAllowList() {
correct := types.NewParams(allowedAddress.String()) // Allow list contains address with correct format
incorrect := types.NewParams(allowedAddress.String(), blockedAddress2.String()) // Allow list contains address with incorrect format
for _, paramPairs := range correct.ParamSetPairs() {
s.Require().NoError(paramPairs.ValidatorFn(correct.AllowList))
}
for _, paramPairs := range incorrect.ParamSetPairs() {
s.Require().Error(paramPairs.ValidatorFn(incorrect.AllowList))
}
}

func TestParamsTestSuite(t *testing.T) {
suite.Run(t, new(ParamsTestSuite))
}
7 changes: 4 additions & 3 deletions x/airdrop/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions x/airdrop/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90b1433

Please sign in to comment.