Skip to content

Commit

Permalink
cleanup static cash decorator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
husio committed Mar 7, 2019
1 parent d322940 commit 6c73a6c
Showing 1 changed file with 59 additions and 77 deletions.
136 changes: 59 additions & 77 deletions x/cash/staticfee_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cash

import (
"fmt"
"testing"

"github.com/iov-one/weave"
Expand All @@ -10,7 +9,6 @@ import (
"github.com/iov-one/weave/gconf"
"github.com/iov-one/weave/orm"
"github.com/iov-one/weave/store"
"github.com/iov-one/weave/x"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -38,20 +36,6 @@ func (f *feeTx) Unmarshal([]byte) error {
return errors.ErrInternal.New("TODO: not implemented")
}

type okHandler struct{}

var _ weave.Handler = okHandler{}

func (okHandler) Check(weave.Context, weave.KVStore,
weave.Tx) (weave.CheckResult, error) {
return weave.CheckResult{}, nil
}

func (okHandler) Deliver(weave.Context, weave.KVStore,
weave.Tx) (weave.DeliverResult, error) {
return weave.DeliverResult{}, nil
}

func must(obj orm.Object, err error) orm.Object {
if err != nil {
panic(err)
Expand All @@ -60,87 +44,85 @@ func must(obj orm.Object, err error) orm.Object {
}

func TestFees(t *testing.T) {
var helpers x.TestHelpers

cash := coin.NewCoin(50, 0, "FOO")
min := coin.NewCoin(0, 1234, "FOO")
perm := weave.NewCondition("sigs", "ed25519", []byte{1, 2, 3})
perm2 := weave.NewCondition("sigs", "ed25519", []byte{3, 4, 5})
perm3 := weave.NewCondition("custom", "type", []byte{0xAB})

cases := []struct {
cases := map[string]struct {
signers []weave.Condition
initState []orm.Object
fee *FeeInfo
min coin.Coin
expect checkErr
}{
// no fee given, nothing expected
0: {nil, nil, nil, coin.Coin{}, noErr},
// no fee given, something expected
1: {nil, nil, nil, min, errors.ErrInsufficientAmount.Is},
// no signer given
2: {nil, nil, &FeeInfo{Fees: &min}, min, errors.ErrInvalidInput.Is},
// use default signer, but not enough money
3: {
[]weave.Condition{perm},
nil,
&FeeInfo{Fees: &min},
min,
errors.ErrEmpty.Is,
"no fee given, nothing expected": {
min: coin.Coin{},
expect: noErr,
},
"no fee given, something expected": {
min: min,
expect: errors.ErrInsufficientAmount.Is,
},
"no signer given": {
fee: &FeeInfo{Fees: &min},
min: min,
expect: errors.ErrInvalidInput.Is,
},
"use default signer, but not enough money": {
signers: []weave.Condition{perm},
fee: &FeeInfo{Fees: &min},
min: min,
expect: errors.ErrEmpty.Is,
},
// signer can cover min, but not pledge
4: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm.Address(), &min))},
&FeeInfo{Fees: &cash},
min,
errors.ErrInsufficientAmount.Is,
"signer can cover min, but not pledge": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm.Address(), &min))},
fee: &FeeInfo{Fees: &cash},
min: min,
expect: errors.ErrInsufficientAmount.Is,
},
// all proper
5: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm.Address(), &cash))},
&FeeInfo{Fees: &min},
min,
noErr,
"all proper": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm.Address(), &cash))},
fee: &FeeInfo{Fees: &min},
min: min,
expect: noErr,
},
// trying to pay from wrong account
6: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm2.Address(), &cash))},
&FeeInfo{Payer: perm2.Address(), Fees: &min},
min,
errors.ErrUnauthorized.Is,
"trying to pay from wrong account": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm2.Address(), &cash))},
fee: &FeeInfo{Payer: perm2.Address(), Fees: &min},
min: min,
expect: errors.ErrUnauthorized.Is,
},
// can pay in any fee
7: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm.Address(), &cash))},
&FeeInfo{Fees: &min},
coin.NewCoin(0, 1000, ""),
noErr,
"can pay in any fee": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm.Address(), &cash))},
fee: &FeeInfo{Fees: &min},
min: coin.NewCoin(0, 1000, ""),
expect: noErr,
},
// wrong currency checked
8: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm.Address(), &cash))},
&FeeInfo{Fees: &min},
coin.NewCoin(0, 1000, "NOT"),
coin.ErrInvalidCurrency.Is,
"wrong currency checked": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm.Address(), &cash))},
fee: &FeeInfo{Fees: &min},
min: coin.NewCoin(0, 1000, "NOT"),
expect: coin.ErrInvalidCurrency.Is,
},
// has the cash, but didn't offer enough fees
9: {
[]weave.Condition{perm},
[]orm.Object{must(WalletWith(perm.Address(), &cash))},
&FeeInfo{Fees: &min},
coin.NewCoin(0, 45000, "FOO"),
errors.ErrInsufficientAmount.Is,
"has the cash, but didn't offer enough fees": {
signers: []weave.Condition{perm},
initState: []orm.Object{must(WalletWith(perm.Address(), &cash))},
fee: &FeeInfo{Fees: &min},
min: coin.NewCoin(0, 45000, "FOO"),
expect: errors.ErrInsufficientAmount.Is,
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
auth := helpers.Authenticate(tc.signers...)
controller := NewController(NewBucket())
h := NewFeeDecorator(auth, controller)
Expand All @@ -158,9 +140,9 @@ func TestFees(t *testing.T) {

tx := &feeTx{tc.fee}

_, err := h.Check(nil, kv, tx, okHandler{})
_, err := h.Check(nil, kv, tx, helpers.CountingHandler())
assert.True(t, tc.expect(err), "%+v", err)
_, err = h.Deliver(nil, kv, tx, okHandler{})
_, err = h.Deliver(nil, kv, tx, helpers.CountingHandler())
assert.True(t, tc.expect(err), "%+v", err)
})
}
Expand Down

0 comments on commit 6c73a6c

Please sign in to comment.