mirror of
https://github.com/gooba42/adventureland.git
synced 2026-10-14 18:39:27 -05:00
241 lines
5.6 KiB
Python
Executable File
241 lines
5.6 KiB
Python
Executable File
import datetime
|
|
import os
|
|
import random
|
|
import re
|
|
import string
|
|
import sys
|
|
import unittest2
|
|
|
|
if 1/2 != 0: str=str
|
|
|
|
from mock import patch, Mock
|
|
|
|
import libraries.stripe3
|
|
|
|
NOW = datetime.datetime.now()
|
|
|
|
DUMMY_CARD = {
|
|
'number': '4242424242424242',
|
|
'exp_month': NOW.month,
|
|
'exp_year': NOW.year + 4
|
|
}
|
|
DUMMY_DEBIT_CARD = {
|
|
'number': '4000056655665556',
|
|
'exp_month': NOW.month,
|
|
'exp_year': NOW.year + 4
|
|
}
|
|
DUMMY_CHARGE = {
|
|
'amount': 100,
|
|
'currency': 'usd',
|
|
'card': DUMMY_CARD
|
|
}
|
|
|
|
DUMMY_DISPUTE = {
|
|
'status': 'needs_response',
|
|
'currency': 'usd',
|
|
'metadata': {}
|
|
}
|
|
|
|
DUMMY_PLAN = {
|
|
'amount': 2000,
|
|
'interval': 'month',
|
|
'name': 'Amazing Gold Plan',
|
|
'currency': 'usd',
|
|
'id': ('stripe-test-gold-' +
|
|
''.join(random.choice(string.ascii_lowercase) for x in range(10)))
|
|
}
|
|
|
|
DUMMY_COUPON = {
|
|
'percent_off': 25,
|
|
'duration': 'repeating',
|
|
'duration_in_months': 5,
|
|
'metadata': {}
|
|
}
|
|
|
|
DUMMY_RECIPIENT = {
|
|
'name': 'John Doe',
|
|
'type': 'individual'
|
|
}
|
|
|
|
DUMMY_TRANSFER = {
|
|
'amount': 400,
|
|
'currency': 'usd',
|
|
'recipient': 'self'
|
|
}
|
|
|
|
DUMMY_INVOICE_ITEM = {
|
|
'amount': 456,
|
|
'currency': 'usd',
|
|
}
|
|
|
|
SAMPLE_INVOICE = libraries.stripe3.util.json.loads("""
|
|
{
|
|
"amount_due": 1305,
|
|
"attempt_count": 0,
|
|
"attempted": true,
|
|
"charge": "ch_wajkQ5aDTzFs5v",
|
|
"closed": true,
|
|
"customer": "cus_osllUe2f1BzrRT",
|
|
"date": 1338238728,
|
|
"discount": null,
|
|
"ending_balance": 0,
|
|
"id": "in_t9mHb2hpK7mml1",
|
|
"livemode": false,
|
|
"next_payment_attempt": null,
|
|
"object": "invoice",
|
|
"paid": true,
|
|
"period_end": 1338238728,
|
|
"period_start": 1338238716,
|
|
"starting_balance": -8695,
|
|
"subtotal": 10000,
|
|
"total": 10000,
|
|
"lines": {
|
|
"invoiceitems": [],
|
|
"prorations": [],
|
|
"subscriptions": [
|
|
{
|
|
"plan": {
|
|
"interval": "month",
|
|
"object": "plan",
|
|
"identifier": "expensive",
|
|
"currency": "usd",
|
|
"livemode": false,
|
|
"amount": 10000,
|
|
"name": "Expensive Plan",
|
|
"trial_period_days": null,
|
|
"id": "expensive"
|
|
},
|
|
"period": {
|
|
"end": 1340917128,
|
|
"start": 1338238728
|
|
},
|
|
"amount": 10000
|
|
}
|
|
]
|
|
}
|
|
}
|
|
""")
|
|
|
|
|
|
class StripeTestCase(unittest2.TestCase):
|
|
RESTORE_ATTRIBUTES = ('api_version', 'api_key')
|
|
|
|
def setUp(self):
|
|
super(StripeTestCase, self).setUp()
|
|
|
|
self._stripe_original_attributes = {}
|
|
|
|
for attr in self.RESTORE_ATTRIBUTES:
|
|
self._stripe_original_attributes[attr] = getattr(stripe, attr)
|
|
|
|
api_base = os.environ.get('STRIPE_API_BASE')
|
|
if api_base:
|
|
libraries.stripe3.api_base = api_base
|
|
libraries.stripe3.api_key = os.environ.get(
|
|
'STRIPE_API_KEY', 'tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I')
|
|
|
|
def tearDown(self):
|
|
super(StripeTestCase, self).tearDown()
|
|
|
|
for attr in self.RESTORE_ATTRIBUTES:
|
|
setattr(stripe, attr, self._stripe_original_attributes[attr])
|
|
|
|
# Python < 2.7 compatibility
|
|
def assertRaisesRegexp(self, exception, regexp, callable, *args, **kwargs):
|
|
try:
|
|
callable(*args, **kwargs)
|
|
except exception as err:
|
|
if regexp is None:
|
|
return True
|
|
|
|
if isinstance(regexp, str):
|
|
regexp = re.compile(regexp)
|
|
if not regexp.search(str(err)):
|
|
raise self.failureException('"%s" does not match "%s"' %
|
|
(regexp.pattern, str(err)))
|
|
else:
|
|
raise self.failureException(
|
|
'%s was not raised' % (exception.__name__,))
|
|
|
|
|
|
class StripeUnitTestCase(StripeTestCase):
|
|
REQUEST_LIBRARIES = ['urlfetch', 'requests', 'pycurl']
|
|
|
|
if sys.version_info >= (3, 0):
|
|
REQUEST_LIBRARIES.append('urllib.request')
|
|
else:
|
|
REQUEST_LIBRARIES.append('urllib2')
|
|
|
|
def setUp(self):
|
|
super(StripeUnitTestCase, self).setUp()
|
|
|
|
self.request_patchers = {}
|
|
self.request_mocks = {}
|
|
for lib in self.REQUEST_LIBRARIES:
|
|
patcher = patch("stripe.http_client.%s" % (lib,))
|
|
|
|
self.request_mocks[lib] = patcher.start()
|
|
self.request_patchers[lib] = patcher
|
|
|
|
def tearDown(self):
|
|
super(StripeUnitTestCase, self).tearDown()
|
|
|
|
for patcher in self.request_patchers.values():
|
|
patcher.stop()
|
|
|
|
|
|
class StripeApiTestCase(StripeTestCase):
|
|
|
|
def setUp(self):
|
|
super(StripeApiTestCase, self).setUp()
|
|
|
|
self.requestor_patcher = patch('stripe.api_requestor.APIRequestor')
|
|
requestor_class_mock = self.requestor_patcher.start()
|
|
self.requestor_mock = requestor_class_mock.return_value
|
|
|
|
def tearDown(self):
|
|
super(StripeApiTestCase, self).tearDown()
|
|
|
|
self.requestor_patcher.stop()
|
|
|
|
def mock_response(self, res):
|
|
self.requestor_mock.request = Mock(return_value=(res, 'reskey'))
|
|
|
|
|
|
class StripeResourceTest(StripeApiTestCase):
|
|
|
|
def setUp(self):
|
|
super(StripeResourceTest, self).setUp()
|
|
self.mock_response({})
|
|
|
|
|
|
class MyResource(stripe.resource.APIResource):
|
|
pass
|
|
|
|
|
|
class MySingleton(stripe.resource.SingletonAPIResource):
|
|
pass
|
|
|
|
|
|
class MyListable(stripe.resource.ListableAPIResource):
|
|
pass
|
|
|
|
|
|
class MyCreatable(stripe.resource.CreateableAPIResource):
|
|
pass
|
|
|
|
|
|
class MyUpdateable(stripe.resource.UpdateableAPIResource):
|
|
pass
|
|
|
|
|
|
class MyDeletable(stripe.resource.DeletableAPIResource):
|
|
pass
|
|
|
|
|
|
class MyComposite(stripe.resource.ListableAPIResource,
|
|
libraries.stripe3.resource.CreateableAPIResource,
|
|
libraries.stripe3.resource.UpdateableAPIResource,
|
|
libraries.stripe3.resource.DeletableAPIResource):
|
|
pass
|