github.com/stripe/stripe-go/v76@v76.25.0/card_test.go (about)

     1  package stripe
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	assert "github.com/stretchr/testify/require"
     8  	"github.com/stripe/stripe-go/v76/form"
     9  )
    10  
    11  func TestCardListParams_AppendTo(t *testing.T) {
    12  	// Adds `object` for account (this will hit the external accounts endpoint)
    13  	{
    14  		params := &CardListParams{Account: String("acct_123")}
    15  		body := &form.Values{}
    16  		form.AppendTo(body, params)
    17  		t.Logf("body = %+v", body)
    18  		assert.Equal(t, []string{"card"}, body.Get("object"))
    19  	}
    20  
    21  	// Adds `object` for customer (this will hit the sources endpoint)
    22  	{
    23  		params := &CardListParams{Customer: String("cus_123")}
    24  		body := &form.Values{}
    25  		form.AppendTo(body, params)
    26  		t.Logf("body = %+v", body)
    27  		assert.Equal(t, []string{"card"}, body.Get("object"))
    28  	}
    29  }
    30  
    31  func TestCard_UnmarshalJSON(t *testing.T) {
    32  	// Unmarshals from a JSON string
    33  	{
    34  		var v Card
    35  		err := json.Unmarshal([]byte(`"card_123"`), &v)
    36  		assert.NoError(t, err)
    37  		assert.Equal(t, "card_123", v.ID)
    38  	}
    39  
    40  	// Unmarshals from a JSON object
    41  	{
    42  		v := Card{ID: "card_123"}
    43  		data, err := json.Marshal(&v)
    44  		assert.NoError(t, err)
    45  
    46  		err = json.Unmarshal(data, &v)
    47  		assert.NoError(t, err)
    48  		assert.Equal(t, "card_123", v.ID)
    49  	}
    50  }
    51  
    52  func TestCardParams_AppendToAsCardSourceOrExternalAccount(t *testing.T) {
    53  	// We should add more tests for all the various corner cases here ...
    54  
    55  	// Includes number and object
    56  	{
    57  		params := &CardParams{Number: String("1234")}
    58  		body := &form.Values{}
    59  		params.AppendToAsCardSourceOrExternalAccount(body, nil)
    60  		t.Logf("body = %+v", body)
    61  		assert.Equal(t, []string{"1234"}, body.Get("source[number]"))
    62  		assert.Equal(t, []string{"card"}, body.Get("source[object]"))
    63  	}
    64  
    65  	// Includes Params
    66  	{
    67  		params := &CardParams{
    68  			Params: Params{
    69  				Metadata: map[string]string{
    70  					"foo": "bar",
    71  				},
    72  			},
    73  		}
    74  		body := &form.Values{}
    75  		params.AppendToAsCardSourceOrExternalAccount(body, nil)
    76  		t.Logf("body = %+v", body)
    77  		assert.Equal(t, []string{"bar"}, body.Get("metadata[foo]"))
    78  	}
    79  
    80  	// It takes key parts for deeper embedding
    81  	{
    82  		params := &CardParams{Number: String("1234")}
    83  		body := &form.Values{}
    84  		params.AppendToAsCardSourceOrExternalAccount(body, []string{"prefix1", "prefix2"})
    85  		t.Logf("body = %+v", body)
    86  		assert.Equal(t, []string{"1234"}, body.Get("prefix1[prefix2][source][number]"))
    87  	}
    88  }