Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
transfers: validate amount in creation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed May 12, 2020
1 parent a07e005 commit b2e450c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/transfers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/moov-io/paygate/pkg/client"
"github.com/moov-io/paygate/pkg/customers"
"github.com/moov-io/paygate/pkg/customers/accounts"
"github.com/moov-io/paygate/pkg/model"
"github.com/moov-io/paygate/pkg/tenants"
"github.com/moov-io/paygate/pkg/transfers/fundflow"
"github.com/moov-io/paygate/pkg/transfers/pipeline"
Expand Down Expand Up @@ -144,6 +145,11 @@ func CreateUserTransfer(
return
}

if err := validateAmount(req.Amount); err != nil {
responder.Problem(err)
return
}

transfer := &client.Transfer{
TransferID: base.ID(),
Amount: req.Amount,
Expand Down Expand Up @@ -205,6 +211,14 @@ func CreateUserTransfer(
}
}

func validateAmount(raw string) error {
var amt model.Amount
if err := amt.FromString(raw); err != nil {
return fmt.Errorf("unable to parse '%s': %v", raw, err)
}
return nil
}

func getFundflowSource(client customers.Client, src client.Source) (fundflow.Source, error) {
var source fundflow.Source

Expand Down
23 changes: 23 additions & 0 deletions pkg/transfers/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ func TestRouter__createUserTransfer(t *testing.T) {
}
}

func TestRouter__createUserTransfersInvalidAmount(t *testing.T) {
customersClient := mockCustomersClient()

r := mux.NewRouter()
router := NewRouter(log.NewNopLogger(), repoWithTransfer, tenantRepo, customersClient, mockDecryptor, mockStrategy, fakePublisher)
router.RegisterRoutes(r)

c := testclient.New(t, r)

opts := client.CreateTransfer{
Amount: "USD YY.44",
}
xfer, resp, err := c.TransfersApi.AddTransfer(context.TODO(), "userID", opts, nil)
if err == nil {
t.Error("expected error")
}
defer resp.Body.Close()

if xfer.TransferID != "" {
t.Errorf("unexpected transfer: %#v", xfer)
}
}

func TestRouter__getUserTransfer(t *testing.T) {
customersClient := mockCustomersClient()

Expand Down

0 comments on commit b2e450c

Please sign in to comment.