Test create-role and fix CheckTx

This commit is contained in:
Ethan Frey 2017-07-12 16:22:17 +02:00
parent bb61b9fca3
commit 7e7f124bc9
4 changed files with 67 additions and 5 deletions

View File

@ -28,8 +28,13 @@ func (Handler) Name() string {
// CheckTx verifies if the transaction is properly formated
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
_, err = checkTx(ctx, tx)
return res, err
var cr CreateRoleTx
cr, err = checkTx(ctx, tx)
if err != nil {
return
}
err = checkNoRole(store, cr.Role)
return
}
// DeliverTx tries to create a new role.

View File

@ -0,0 +1,50 @@
package roles_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/modules/roles"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
)
func TestCreateRole(t *testing.T) {
assert := assert.New(t)
a := basecoin.Actor{App: "foo", Address: []byte("bar")}
b := basecoin.Actor{ChainID: "eth", App: "foo", Address: []byte("bar")}
c := basecoin.Actor{App: "foo", Address: []byte("baz")}
d := basecoin.Actor{App: "si-ly", Address: []byte("bar")}
cases := []struct {
valid bool
role string
min uint32
sigs []basecoin.Actor
}{
{true, "awesome", 1, []basecoin.Actor{a}},
{true, "cool", 2, []basecoin.Actor{b, c, d}},
{false, "oops", 3, []basecoin.Actor{a, d}}, // too many
{false, "ugh", 0, []basecoin.Actor{a, d}}, // too few
{false, "phew", 1, []basecoin.Actor{}}, // none
{false, "cool", 1, []basecoin.Actor{c, d}}, // duplicate of existing one
}
h := roles.NewHandler()
ctx := stack.MockContext("role-chain", 123)
store := state.NewMemKVStore()
for i, tc := range cases {
tx := roles.NewCreateRoleTx([]byte(tc.role), tc.min, tc.sigs)
_, err := h.CheckTx(ctx, store, tx)
_, err2 := h.DeliverTx(ctx, store, tx)
if tc.valid {
assert.Nil(err, "%d/%s: %+v", i, tc.role, err)
assert.Nil(err2, "%d/%s: %+v", i, tc.role, err2)
} else {
assert.NotNil(err, "%d/%s", i, tc.role)
assert.NotNil(err2, "%d/%s", i, tc.role)
}
}
}

View File

@ -68,11 +68,18 @@ func loadRole(store state.KVStore, key []byte) (role Role, err error) {
return role, nil
}
// we only have create here, no update, since we don't allow update yet
func createRole(store state.KVStore, key []byte, role Role) error {
func checkNoRole(store state.KVStore, key []byte) error {
if _, err := loadRole(store, key); !IsNoRoleErr(err) {
return ErrRoleExists()
}
return nil
}
// we only have create here, no update, since we don't allow update yet
func createRole(store state.KVStore, key []byte, role Role) error {
if err := checkNoRole(store, key); err != nil {
return err
}
bin := wire.BinaryBytes(role)
store.Set(key, bin)
return nil // real stores can return error...

View File

@ -7,7 +7,7 @@ import (
"github.com/tendermint/basecoin/errors"
)
const (
var (
// MaxMembers it the maximum number of members in a Role. Used to avoid
// extremely large roles.
// Value is arbitrary, please adjust as needed