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

     1  package file
     2  
     3  //
     4  // This test is unlike other tests: it makes calls to the live Stripe API
     5  // servers. This is because file uploads operate under a different domain from
     6  // the standard api.stripe.com and stripe-mock does not yet support them.
     7  //
     8  // I've nicened this file up a bit for now, and it's not making enough requests
     9  // to cause bad intermittency problems in the test suite, but long term it
    10  // would be nice if we could change these tests to hit a local target so that
    11  // the entire suite can run offline (and more quickly).
    12  //
    13  
    14  import (
    15  	"bytes"
    16  	"os"
    17  	"testing"
    18  
    19  	assert "github.com/stretchr/testify/require"
    20  	stripe "github.com/stripe/stripe-go/v76"
    21  	"github.com/stripe/stripe-go/v76/form"
    22  	_ "github.com/stripe/stripe-go/v76/testing"
    23  )
    24  
    25  const (
    26  	expectedSize int64 = 734
    27  	expectedType       = "pdf"
    28  )
    29  
    30  func TestFileGet(t *testing.T) {
    31  	file, err := Get("file_123", nil)
    32  	assert.Nil(t, err)
    33  	assert.NotNil(t, file)
    34  }
    35  
    36  func TestFileList(t *testing.T) {
    37  	i := List(&stripe.FileListParams{})
    38  
    39  	// Verify that we can get at least one file
    40  	assert.True(t, i.Next())
    41  	assert.Nil(t, i.Err())
    42  	assert.NotNil(t, i.File())
    43  	assert.NotNil(t, i.FileList())
    44  }
    45  
    46  type testBackend struct {
    47  	calledMultipart bool
    48  }
    49  
    50  func (b *testBackend) Call(method, path, key string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {
    51  	return nil
    52  }
    53  func (b *testBackend) CallStreaming(method, path, key string, params stripe.ParamsContainer, v stripe.StreamingLastResponseSetter) error {
    54  	return nil
    55  }
    56  func (b *testBackend) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v stripe.LastResponseSetter) error {
    57  	return nil
    58  }
    59  func (b *testBackend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v stripe.LastResponseSetter) error {
    60  	b.calledMultipart = true
    61  	return nil
    62  }
    63  func (b *testBackend) SetMaxNetworkRetries(maxNetworkRetries int64) {}
    64  func TestFileBackend(t *testing.T) {
    65  	orig := stripe.GetBackend(stripe.UploadsBackend)
    66  	b := &testBackend{calledMultipart: false}
    67  	stripe.SetBackend(stripe.UploadsBackend, b)
    68  	fileParams := &stripe.FileParams{}
    69  	New(fileParams)
    70  	assert.Equal(t, b.calledMultipart, true)
    71  	stripe.SetBackend(stripe.UploadsBackend, orig)
    72  }
    73  func TestFileNew(t *testing.T) {
    74  	f, err := os.Open("test_data.pdf")
    75  	if err != nil {
    76  		t.Errorf("Unable to open test file %v\n", err)
    77  	}
    78  
    79  	fileParams := &stripe.FileParams{
    80  		Purpose:    stripe.String(string(stripe.FilePurposeDisputeEvidence)),
    81  		FileReader: f,
    82  		Filename:   stripe.String(f.Name()),
    83  		FileLinkData: &stripe.FileFileLinkDataParams{
    84  			Params: stripe.Params{
    85  				Metadata: map[string]string{
    86  					"foo": "bar",
    87  				},
    88  			},
    89  			Create: stripe.Bool(true),
    90  		},
    91  	}
    92  
    93  	file, err := New(fileParams)
    94  	assert.NoError(t, err)
    95  	assert.NotNil(t, file)
    96  }