8000 Merge branch 'master' of github.com:balanced/balanced-python · balanced/balanced-python@7dfcb80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dfcb80

Browse files
author
Andrew
committed
Merge branch 'master' of github.com:balanced/balanced-python
2 parents 9487102 + fb14ff2 commit 7dfcb80

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

balanced/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.8.6'
1+
__version__ = '0.8.7'
22
from collections import defaultdict
33
import contextlib
44

balanced/http_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ def wrapped():
3333
try:
3434
raise_for_status(allow_redirects=False)
3535
except requests.HTTPError, exc:
36-
3736
if exc.response.status_code in REDIRECT_STATI:
3837
redirection = HTTPError('%s' % exc)
3938
redirection.status_code = exc.response.status_code
4039
redirection.response = exc.response
4140
raise redirection
42-
4341
deserialized = http_client.deserialize(
4442
response_instance
4543
)
@@ -50,8 +48,8 @@ def wrapped():
5048
error_msg = '{name}: {code}: {msg} {extra}'.format(
5149
name=deserialized['status'],
5250
code=deserialized['status_code'],
53-
msg=deserialized['description'],
54-
extra=extra,
51+
msg=deserialized['description'].encode('utf8'),
52+
extra=extra.encode('utf8'),
5553
)
5654
http_error = HTTPError(error_msg)
5755
for error, value in response_instance.deserialized.iteritems():

tests/acceptance_suite.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,26 @@ def test_00_merchant_expectations(self):
5858
mps = balanced.Marketplace.query.all()
5959
self.assertEqual(len(mps), 1)
6060

61+
def _create_buyer_account(self):
62+
mp = balanced.Marketplace.query.one()
63+
card_payload = dict(self.us_card_payload)
64+
card = balanced.Card(**card_payload).save()
65+
card_uri = card.uri
66+
buyer = mp.create_buyer(
67+
email_address='albert@einstein.com',
68+
card_uri=card_uri,
69+
meta={'foo': 'bar'},
70+
)
71+
return buyer
72+
6173
def _find_buyer_account(self):
6274
mp = balanced.Marketplace.query.one()
6375
accounts = list(mp.accounts)
6476
filtered_accounts = [
6577
account for account in accounts if account.roles == ['buyer']
6678
]
6779
if not filtered_accounts:
68-
card_payload = dict(self.us_card_payload)
69-
card = balanced.Card(**card_payload).save()
70-
card_uri = card.uri
71-
buyer = mp.create_buyer(
72-
email_address='albert@einstein.com',
73-
card_uri=card_uri,
74-
meta={'foo': 'bar'},
75-
)
80+
buyer = self._create_buyer_account()
7681
else:
7782
buyer = filtered_accounts[0]
7883
return buyer
@@ -94,7 +99,7 @@ def test_valid_non_us_address_no_postal_code(self):
9499
balanced.Card(**card_payload).save()
95100

96101
def test_valid_us_address(self):
97-
buyer = self._find_buyer_account()
102+
buyer = self._create_buyer_account()
98103
self.assertTrue(buyer.id.startswith('AC'), buyer.id)
99104
self.assertEqual(buyer.roles, ['buyer'])
100105
self.assertDictEqual(buyer.meta, {'foo': 'bar'})
@@ -233,15 +238,15 @@ def test_transactions_invalid_funding_sources(self):
233238
the_exception = exc.exception
234239
self.assertEqual(the_exception.status_code, 409)
235240
self.assertEqual(the_exception.category_code,
236-
'funding-source-not-valid')
241+
'bad-funding-info')
237242

238243
with self.assertRaises(requests.HTTPError) as exc:
239244
# ... and explicitly
240245
buyer.debit(7000, source_uri=card.uri)
241246
the_exception = exc.exception
242247
self.assertEqual(the_exception.status_code, 409)
243248
self.assertEqual(the_exception.category_code,
244-
'funding-source-not-valid')
249+
'bad-funding-info')
245250

246251
with self.assertRaises(requests.HTTPError) as exc:
247252
buyer.credit(8000)
@@ -356,14 +361,14 @@ def test_zzz_remove_owner_merchant_account_bank_account(self):
356361
owner.debit(600)
357362
the_exception = exc.exception
358363
self.assertEqual(the_exception.status_code, 409)
359-
self.assertEqual('funding-source-not-valid',
364+
self.assertEqual('bad-funding-info',
360365
the_exception.category_code)
361366

362367
with self.assertRaises(requests.HTTPError) as exc:
363368
owner.credit(900)
364369
the_exception = exc.exception
365370
self.assertEqual(the_exception.status_code, 409)
366-
self.assertEqual('funding-destination-not-valid',
371+
self.assertEqual('bad-funding-info',
367372
the_exception.category_code)
368373

369374

tests/test_client.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# -*- coding: utf-8 -*-
12
import unittest
23

34
import balanced
5+
from balanced.http_client import wrap_raise_for_status
46
import mock
57

68

79
class TestConfig(unittest.TestCase):
8-
910
def test_default_config(self):
1011
config = balanced.config.__class__()
1112
# this is here because it tests taht if you add anything new
@@ -14,7 +15,7 @@ def test_default_config(self):
1415
self.assertItemsEqual(
1516
config.__dict__.keys(),
1617
['api_key_secret', 'api_version', 'root_uri', 'requests']
17-
)
18+
)
1819
self.assertEqual(config.root_uri, 'https://api.balancedpayments.com')
1920
self.assertEqual(config.api_version, '1')
2021
self.assertEqual(config.api_key_secret, None)
@@ -23,26 +24,25 @@ def test_default_config(self):
2324

2425

2526
class TestClient(unittest.TestCase):
26-
2727
def test_http_operations(self):
2828
ops = ['get', 'post', 'put', 'delete']
2929
for op in ops:
3030
request = getattr(balanced.http_client, op)(
3131
'hithere',
3232
return_response=False
33-
)
33+
)
3434
self.assertEqual(request.method, op.upper())
3535
self.assertEqual(
3636
request.url, 'https://api.balancedpayments.com/v1/hithere'
37-
)
37+
)
3838

3939
def test_client_reference_config(self):
4040
the_config = balanced.config
4141
self.assertIsNone(balanced.http_client.config.api_key_secret)
4242
the_config.api_key_secret = 'khalkhalash'
4343
self.assertEqual(
4444
balanced.http_client.config.api_key_secret, 'khalkhalash'
45-
)
45+
)
4646

4747
def test_client_key_switch(self):
4848
the_config = balanced.config
@@ -53,12 +53,11 @@ def test_client_key_switch(self):
5353

5454

5555
class TestHTTPClient(unittest.TestCase):
56-
5756
def test_deserialization(self):
5857
resp = mock.Mock()
5958
resp.headers = {
6059
'Content-Type': 'text/html',
61-
}
60+
}
6261
resp.content = 'Unhandled Exception'
6362
client = balanced.HTTPClient()
6463
with self.assertRaises(balanced.exc.BalancedError):
@@ -67,3 +66,44 @@ def test_deserialization(self):
6766
resp.content = '{"hi": "world"}'
6867
deserialized = client.deserialize(resp)
6968
self.assertItemsEqual(deserialized, {u'hi': u'world'})
69+
70+
def test_deserialization_unicode(self):
71+
resp = mock.Mock()
72+
resp.headers = {
73+
'Content-Type': 'text/html',
74+
}
75+
resp.content = 'Unhandled Exception'
76+
client = balanced.HTTPClient()
77+
resp.headers['Content-Type'] = 'application/json'
78+
resp.content = ('{"\\uc800\\uac74 \\ub610 \\ubb50\\uc57c": "second", '
79+
'"third": "\\u06a9\\u0647 \\u0686\\u0647 '
80+
'\\u06a9\\u062b\\u0627\\u0641\\u062a\\u06cc"}')
81+
deserialized = client.deserialize(resp)
82+
self.assertItemsEqual(deserialized, {
83+
u'third': (u'\u06a9\u0647 \u0686\u0647 '
84+
u'\u06a9\u062b\u0627\u0641\u062a\u06cc'),
85+
u'\uc800\uac74 \ub610 \ubb50\uc57c': u'second'})
86+
87+
def test_wrap_raise_for_status(self):
88+
api_response = {'additional': ('Valid email address formats may be '
89+
'found at http://tools.ietf.org/html'
90+
'/rfc2822#section-3.4'),
91+
'description': (u'"s\xf8ren.kierkegaard216@yahoo.web" '
92+
u'must be a valid email address as '
93+
u'specified by rfc2822 for email_add'),
94+
'status': 'Bad Request',
95+
'status_code': 400}
96+
client = mock.Mock()
97+
client.deserialize.return_value = api_response
98+
ex = balanced.exc.HTTPError('Ooops')
99+
setattr(ex, 'response', mock.Mock())
100+
ex.response.status_code = 400
101+
response = mock.Mock()
102+
response.raise_for_status.side_effect = ex
103+
104+
wrapped = wrap_raise_for_status(client)
105+
wrapped(response)
106+
107+
with self.assertRaises(balanced.exc.HTTPError) as ex:
108+
response.raise_for_status()
109+
self.assertEqual(ex.exception.description, api_response['description'])

0 commit comments

Comments
 (0)
0