github.com/stripe/stripe-go/v76@v76.25.0/bankaccount_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 TestBankAccount_UnmarshalJSON(t *testing.T) {
    12  	// Unmarshals from a JSON string
    13  	{
    14  		var v BankAccount
    15  		err := json.Unmarshal([]byte(`"ba_123"`), &v)
    16  		assert.NoError(t, err)
    17  		assert.Equal(t, "ba_123", v.ID)
    18  	}
    19  
    20  	// Unmarshals from a JSON object
    21  	{
    22  		v := BankAccount{ID: "ba_123"}
    23  		data, err := json.Marshal(&v)
    24  		assert.NoError(t, err)
    25  
    26  		err = json.Unmarshal(data, &v)
    27  		assert.NoError(t, err)
    28  		assert.Equal(t, "ba_123", v.ID)
    29  	}
    30  }
    31  
    32  func TestBankAccountListParams_AppendTo(t *testing.T) {
    33  	// Adds `object` for account (this will hit the customer sources endpoint)
    34  	{
    35  		params := &BankAccountListParams{Account: String("acct_123")}
    36  		body := &form.Values{}
    37  		form.AppendTo(body, params)
    38  		t.Logf("body = %+v", body)
    39  		assert.Equal(t, []string{"bank_account"}, body.Get("object"))
    40  	}
    41  
    42  	// Adds `object` for customer (this will hit the external accounts endpoint)
    43  	{
    44  		params := &BankAccountListParams{Customer: String("cus_123")}
    45  		body := &form.Values{}
    46  		form.AppendTo(body, params)
    47  		t.Logf("body = %+v", body)
    48  		assert.Equal(t, []string{"bank_account"}, body.Get("object"))
    49  	}
    50  }
    51  
    52  func TestBankAccountParams_AppendToAsSourceOrExternalAccount(t *testing.T) {
    53  	// We should add more tests for all the various corner cases here ...
    54  
    55  	// Includes account_holder_name
    56  	{
    57  		params := &BankAccountParams{AccountHolderName: String("Tyrion")}
    58  		body := &form.Values{}
    59  		params.AppendToAsSourceOrExternalAccount(body)
    60  		t.Logf("body = %+v", body)
    61  		assert.Equal(t, []string{"Tyrion"}, body.Get("external_account[account_holder_name]"))
    62  	}
    63  
    64  	// Does not include account_holder_name if empty
    65  	{
    66  		params := &BankAccountParams{}
    67  		body := &form.Values{}
    68  		params.AppendToAsSourceOrExternalAccount(body)
    69  		t.Logf("body = %+v", body)
    70  		assert.Equal(t, []string(nil), body.Get("external_account[account_holder_name]"))
    71  	}
    72  
    73  	// Includes account_holder_name
    74  	{
    75  		params := &BankAccountParams{AccountHolderType: String(string(BankAccountAccountHolderTypeIndividual))}
    76  		body := &form.Values{}
    77  		params.AppendToAsSourceOrExternalAccount(body)
    78  		t.Logf("body = %+v", body)
    79  		assert.Equal(t, []string{"individual"}, body.Get("external_account[account_holder_type]"))
    80  	}
    81  
    82  	// Includes Params
    83  	{
    84  		params := &BankAccountParams{
    85  			Params: Params{
    86  				Metadata: map[string]string{
    87  					"foo": "bar",
    88  				},
    89  			},
    90  		}
    91  		body := &form.Values{}
    92  		params.AppendToAsSourceOrExternalAccount(body)
    93  		t.Logf("body = %+v", body)
    94  		assert.Equal(t, []string{"bar"}, body.Get("metadata[foo]"))
    95  	}
    96  
    97  	// Does not include account_holder_name if empty
    98  	{
    99  		params := &BankAccountParams{}
   100  		body := &form.Values{}
   101  		params.AppendToAsSourceOrExternalAccount(body)
   102  		t.Logf("body = %+v", body)
   103  		assert.Equal(t, []string(nil), body.Get("external_account[account_holder_type]"))
   104  	}
   105  }