Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle file objects like file_upload #477

Merged
merged 3 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
client_id = None
api_base = 'https://api.stripe.com'
connect_api_base = 'https://connect.stripe.com'
upload_api_base = 'https://uploads.stripe.com'
upload_api_base = 'https://files.stripe.com'
api_version = None
verify_ssl_certs = True
proxy = None
Expand Down
6 changes: 1 addition & 5 deletions stripe/api_resources/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class File(ListableAPIResource):
OBJECT_NAME = 'file'
OBJECT_NAME_ALT = 'file_upload'

@classmethod
def api_base(cls):
return stripe.upload_api_base

@classmethod
def class_url(cls):
return '/v1/files'
Expand All @@ -25,7 +21,7 @@ def class_url(cls):
def create(cls, api_key=None, api_version=None, stripe_account=None,
**params):
requestor = api_requestor.APIRequestor(
api_key, api_base=cls.api_base(), api_version=api_version,
api_key, api_base=stripe.upload_api_base, api_version=api_version,
account=stripe_account)
url = cls.class_url()
supplied_headers = {
Expand Down
13 changes: 12 additions & 1 deletion tests/api_resources/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import tempfile

import pytest

import stripe


TEST_RESOURCE_ID = 'file_123'


class TestFile(object):
@pytest.fixture
def setup_upload_api_base(self):
stripe.upload_api_base = stripe.api_base
stripe.api_base = None
yield
stripe.api_base = stripe.upload_api_base
stripe.upload_api_base = 'https://files.stripe.com'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with the framework here, but are you sure this doesn't need to be in a finally to guarantee that these values are set back in case of an exception?

Copy link
Contributor Author

@ob-stripe ob-stripe Sep 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested it, and it's not necessary. The framework already catches exceptions in tests and reports them as errors, and still executes the post-yield code.


def test_is_listable(self, request_mock):
resources = stripe.File.list()
request_mock.assert_requested(
Expand All @@ -26,14 +36,15 @@ def test_is_retrievable(self, request_mock):
)
assert isinstance(resource, stripe.File)

def test_is_creatable(self, request_mock):
def test_is_creatable(self, setup_upload_api_base, request_mock):
stripe.multipart_data_generator.MultipartDataGenerator\
._initialize_boundary = lambda self: 1234567890
test_file = tempfile.TemporaryFile()
resource = stripe.File.create(
purpose='dispute_evidence',
file=test_file
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
'post',
'/v1/files',
Expand Down
13 changes: 12 additions & 1 deletion tests/api_resources/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import tempfile

import pytest

import stripe


TEST_RESOURCE_ID = 'file_123'


class TestFileUpload(object):
@pytest.fixture
def setup_upload_api_base(self):
stripe.upload_api_base = stripe.api_base
stripe.api_base = None
yield
stripe.api_base = stripe.upload_api_base
stripe.upload_api_base = 'https://files.stripe.com'

def test_is_listable(self, request_mock):
resources = stripe.FileUpload.list()
request_mock.assert_requested(
Expand All @@ -26,14 +36,15 @@ def test_is_retrievable(self, request_mock):
)
assert isinstance(resource, stripe.FileUpload)

def test_is_creatable(self, request_mock):
def test_is_creatable(self, setup_upload_api_base, request_mock):
stripe.multipart_data_generator.MultipartDataGenerator\
._initialize_boundary = lambda self: 1234567890
test_file = tempfile.TemporaryFile()
resource = stripe.FileUpload.create(
purpose='dispute_evidence',
file=test_file
)
request_mock.assert_api_base(stripe.upload_api_base)
request_mock.assert_requested(
'post',
'/v1/files',
Expand Down
3 changes: 0 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ def setup_stripe():
'api_key': stripe.api_key,
'client_id': stripe.client_id,
'default_http_client': stripe.default_http_client,
'upload_api_base': stripe.upload_api_base,
}
http_client = stripe.http_client.new_default_http_client()
stripe.api_base = 'http://localhost:%s' % MOCK_PORT
stripe.api_key = 'sk_test_123'
stripe.client_id = 'ca_123'
stripe.default_http_client = http_client
stripe.upload_api_base = 'http://localhost:%s' % MOCK_PORT
yield
http_client.close()
stripe.api_base = orig_attrs['api_base']
stripe.api_key = orig_attrs['api_key']
stripe.client_id = orig_attrs['client_id']
stripe.default_http_client = orig_attrs['default_http_client']
stripe.upload_api_base = orig_attrs['upload_api_base']


@pytest.fixture
Expand Down
19 changes: 19 additions & 0 deletions tests/request_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ def stub_request(self, method, url, rbody={}, rcode=200, rheaders={}):
self._stub_request_handler.register(method, url, rbody, rcode,
rheaders)

def assert_api_base(self, expected_api_base):
# Note that this method only checks that an API base was provided
# as a keyword argument in APIRequestor's constructor, not as a
# positional argument.

if 'api_base' not in self.constructor_patcher.call_args[1]:
msg = ("Expected APIRequestor to have been constructed with "
"api_base='%s'. No API base was provided." %
expected_api_base)
raise AssertionError(msg)

actual_api_base = \
self.constructor_patcher.call_args[1]['api_base']
if actual_api_base != expected_api_base:
msg = ("Expected APIRequestor to have been constructed with "
"api_base='%s'. Constructed with api_base='%s' "
"instead." % (expected_api_base, actual_api_base))
raise AssertionError(msg)

def assert_api_version(self, expected_api_version):
# Note that this method only checks that an API version was provided
# as a keyword argument in APIRequestor's constructor, not as a
Expand Down