Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Move test clean up into tearDown
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Sep 1, 2016
1 parent aa77b2f commit 7748698
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions tests/py/test_billing_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@

class TestsWithBillingHarness(BillingHarness):

def setUp(self):
super(TestsWithBillingHarness, self).setUp()
self.hold = None

def tearDown(self):
if self.hold:
cancel_card_hold(self.hold)
super(TestsWithBillingHarness, self).tearDown()


# cch - create_card_hold

def test_cch_success(self):
hold, error = create_card_hold(self.db, self.obama, D('1.00'))
assert isinstance(hold, braintree.Transaction)
assert hold.status == 'authorized'
assert hold.amount == D('10.00')
self.hold, error = create_card_hold(self.db, self.obama, D('1.00'))
assert isinstance(self.hold, braintree.Transaction)
assert self.hold.status == 'authorized'
assert self.hold.amount == D('10.00')
assert error == ''
assert self.obama.balance == P('obama').balance == 0

Expand All @@ -41,8 +51,8 @@ def test_cch_for_suspicious_raises_NotWhitelisted(self):
@mock.patch('braintree.Transaction.sale')
def test_cch_failure(self, btsale):
btsale.side_effect = Foobar
hold, error = create_card_hold(self.db, self.obama, D('1.00'))
assert hold is None
self.hold, error = create_card_hold(self.db, self.obama, D('1.00'))
assert self.hold is None
assert error == "Foobar()"
exchange = self.db.one("SELECT * FROM exchanges")
assert exchange
Expand All @@ -64,8 +74,8 @@ def test_cch_bad_card(self):

# https://developers.braintreepayments.com/ios+python/reference/general/testing#test-amounts
# $2002 is upcharged to $2062, which corresponds to 'Invalid Tax Amount'
hold, error = create_card_hold(self.db, bob, D('2002.00'))
assert hold is None
self.hold, error = create_card_hold(self.db, bob, D('2002.00'))
assert self.hold is None
assert error.startswith('Invalid Tax Amount')

def test_cch_multiple_cards(self):
Expand All @@ -80,75 +90,60 @@ def test_cch_multiple_cards(self):
assert result.is_success
ExchangeRoute.insert(bob, 'braintree-cc', result.payment_method.token)

hold, error = create_card_hold(self.db, bob, D('100.00'))
self.hold, error = create_card_hold(self.db, bob, D('100.00'))
assert error == ''

# Clean up
cancel_card_hold(hold)

def test_cch_no_card(self):
bob = self.make_participant('bob', is_suspicious=False)
hold, error = create_card_hold(self.db, bob, D('10.00'))
self.hold, error = create_card_hold(self.db, bob, D('10.00'))
assert error == 'No credit card'

def test_cch_invalidated_card(self):
bob = self.make_participant('bob', is_suspicious=False)
ExchangeRoute.insert(bob, 'braintree-cc', 'foo', error='invalidated')
hold, error = create_card_hold(self.db, bob, D('10.00'))
self.hold, error = create_card_hold(self.db, bob, D('10.00'))
assert error == 'No credit card'


# capch - capture_card_hold

def test_capch_full_amount(self):
hold, error = create_card_hold(self.db, self.obama, D('20.00'))
self.hold, error = create_card_hold(self.db, self.obama, D('20.00'))
assert error == '' # sanity check
assert hold.status == 'authorized'
assert self.hold.status == 'authorized'

capture_card_hold(self.db, self.obama, D('20.00'), hold)
hold = braintree.Transaction.find(hold.id)
capture_card_hold(self.db, self.obama, D('20.00'), self.hold)
hold = braintree.Transaction.find(self.hold.id)
assert self.obama.balance == P('obama').balance == D('20.00')
assert self.obama.get_credit_card_error() == ''
assert hold.status == 'submitted_for_settlement'

# Clean up
cancel_card_hold(hold)

def test_capch_partial_amount(self):
hold, error = create_card_hold(self.db, self.obama, D('20.00'))
self.hold, error = create_card_hold(self.db, self.obama, D('20.00'))
assert error == '' # sanity check

capture_card_hold(self.db, self.obama, D('15.00'), hold)
capture_card_hold(self.db, self.obama, D('15.00'), self.hold)
assert self.obama.balance == P('obama').balance == D('15.00')
assert self.obama.get_credit_card_error() == ''

# Clean up
cancel_card_hold(hold)

def test_capch_too_high_amount(self):
hold, error = create_card_hold(self.db, self.obama, D('20.00'))
self.hold, error = create_card_hold(self.db, self.obama, D('20.00'))
assert error == '' # sanity check

with self.assertRaises(Exception):
# How do I check the exception's msg here?
capture_card_hold(self.db, self.obama, D('20.01'), hold)
capture_card_hold(self.db, self.obama, D('20.01'), self.hold)

assert self.obama.balance == P('obama').balance == 0

# Clean up
cancel_card_hold(hold)

def test_capch_amount_under_minimum(self):
hold, error = create_card_hold(self.db, self.obama, D('20.00'))
self.hold, error = create_card_hold(self.db, self.obama, D('20.00'))
assert error == '' # sanity check

capture_card_hold(self.db, self.obama, D('0.01'), hold)
capture_card_hold(self.db, self.obama, D('0.01'), self.hold)
assert self.obama.balance == P('obama').balance == D('9.41')
assert self.obama.get_credit_card_error() == ''

# Clean up
cancel_card_hold(hold)


class TestsWithoutBillingHarness(Harness):

Expand Down

0 comments on commit 7748698

Please sign in to comment.