Skip to content

Commit

Permalink
Error early when params not specified for card functions
Browse files Browse the repository at this point in the history
Card-related API calls are a bit of an unusual case because we need to
have an instantiated params struct in order to determine the correct URL
to which to make the request. Previously, if a `nil` was passed to any
of these functions, we'd panic as we tried to access one of its members.

This patch just makes the params requirement a little more explicit by
returning an error if any of card functions is called with `nil` params.

Fixes #483.
  • Loading branch information
brandur committed Oct 23, 2017
1 parent 035c852 commit 848cfb6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
28 changes: 25 additions & 3 deletions card/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func New(params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) New(params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

body := &form.Values{}

// Note that we call this special append method instead of the standard one
Expand Down Expand Up @@ -71,6 +75,10 @@ func Get(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Get(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var body *form.Values
var commonParams *stripe.Params

Expand Down Expand Up @@ -103,6 +111,10 @@ func Update(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Update(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

body := &form.Values{}
form.AppendTo(body, params)

Expand All @@ -129,6 +141,10 @@ func Del(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Del(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var body *form.Values
var commonParams *stripe.Params

Expand Down Expand Up @@ -166,14 +182,20 @@ func (c Client) List(params *stripe.CardListParams) *Iter {
var lp *stripe.ListParams
var p *stripe.Params

form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
if params != nil {
form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.CardList{}
var err error

if params == nil {
return nil, list.ListMeta, errors.New("params should not be nil")
}

if len(params.Account) > 0 {
err = c.B.Call("GET", fmt.Sprintf("/accounts/%v/external_accounts", params.Account), c.Key, b, p, list)
} else if len(params.Customer) > 0 {
Expand Down
28 changes: 27 additions & 1 deletion card/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func TestCardDel(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardDel_RequiresParams(t *testing.T) {
_, err := Del("card_123", nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardGet(t *testing.T) {
card, err := Get("card_123", &stripe.CardParams{
Customer: "cus_123",
Expand All @@ -24,7 +29,12 @@ func TestCardGet(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardListByCustomer(t *testing.T) {
func TestCardGet_RequiresParams(t *testing.T) {
_, err := Get("card_123", nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardList_ByCustomer(t *testing.T) {
i := List(&stripe.CardListParams{Customer: "cus_123"})

// Verify that we can get at least one card
Expand All @@ -33,6 +43,12 @@ func TestCardListByCustomer(t *testing.T) {
assert.NotNil(t, i.Card())
}

func TestCardList_RequiresParam(t *testing.T) {
i := List(nil)
assert.False(t, i.Next())
assert.Error(t, i.Err(), "params should not be nil")
}

func TestCardNew(t *testing.T) {
card, err := New(&stripe.CardParams{
Customer: "cus_123",
Expand All @@ -42,6 +58,11 @@ func TestCardNew(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardNew_RequiresParams(t *testing.T) {
_, err := New(nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardUpdate(t *testing.T) {
card, err := Update("card_123", &stripe.CardParams{
Customer: "cus_123",
Expand All @@ -50,3 +71,8 @@ func TestCardUpdate(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, card)
}

func TestCardUpdate_RequiresParams(t *testing.T) {
_, err := Update("card_123", nil)
assert.Error(t, err, "params should not be nil")
}

0 comments on commit 848cfb6

Please sign in to comment.