old_contents: from __future__ import unicode_literals
  import mock
  
  import balanced
  
  from gittip import authentication, billing
  
  from tests import GittipBaseDBTest
  
  
  class TestBilling(GittipBaseDBTest):
      def setUp(self):
          super(TestBilling, self).setUp()
          self.participant_id = 'lgtest'
          self.pp_customer_id = '/v1/marketplaces/M123/accounts/A123'
          self.tok = '/v1/marketplaces/M123/accounts/A123/cards/C123'
          billing.db = self.db
  
      @mock.patch('balanced.Account')
      def test_associate_valid(self, ba):
          not_found = balanced.exc.NoResultFound()
          ba.query.filter.return_value.one.side_effect = not_found
          ba.return_value.save.return_value.uri = self.pp_customer_id
  
          # first time through, payment processor account is None
          billing.associate(self.participant_id, None, self.tok)
  
          expected_email_address = '{}@gittip.com'.format(
              self.participant_id
          )
          _, kwargs = balanced.Account.call_args
          self.assertTrue(kwargs['email_address'], expected_email_address)
  
          user = authentication.User.from_id(self.participant_id)
          # participant in db should be updated
          self.assertEqual(user.session['pp_customer_id'], self.pp_customer_id)
  
      @mock.patch('balanced.Account')
      def test_associate_invalid_card(self, ba):
          error_message = 'Something terrible'
          not_found = balanced.exc.HTTPError(error_message)
          ba.find.return_value.save.side_effect = not_found
  
          # second time through, payment processor account is balanced
          # account_uri
          billing.associate(self.participant_id, self.pp_customer_id, self.tok)
  
          user = authentication.User.from_id(self.participant_id)
          # participant in db should be updated to reflect the error message of
          # last uppepdate
          self.assertEqual(user.session['last_bill_result'], error_message)
          self.assertTrue(ba.find.call_count)
  
new_contents: from __future__ import unicode_literals
  import mock
  
  import balanced
  
  from gittip import authentication, billing
  
  from tests import GittipBaseDBTest
  
  
  class TestBilling(GittipBaseDBTest):
      def setUp(self):
          super(TestBilling, self).setUp()
          self.participant_id = 'lgtest'
          self.pp_customer_id = '/v1/marketplaces/M123/accounts/A123'
          self.tok = '/v1/marketplaces/M123/accounts/A123/cards/C123'
          billing.db = self.db
  
      @mock.patch('balanced.Account')
      def test_associate_valid(self, ba):
          not_found = balanced.exc.NoResultFound()
          ba.query.filter.return_value.one.side_effect = not_found
          ba.return_value.save.return_value.uri = self.pp_customer_id
  
          # first time through, payment processor account is None
          billing.associate(self.participant_id, None, self.tok)
  
          expected_email_address = '{}@gittip.com'.format(
              self.participant_id
          )
          _, kwargs = balanced.Account.call_args
          self.assertTrue(kwargs['email_address'], expected_email_address)
  
          user = authentication.User.from_id(self.participant_id)
          # participant in db should be updated
          self.assertEqual(user.session['pp_customer_id'], self.pp_customer_id)
  
      @mock.patch('balanced.Account')
      def test_associate_invalid_card(self, ba):
          error_message = 'Something terrible'
          not_found = balanced.exc.HTTPError(error_message)
          ba.find.return_value.save.side_effect = not_found
  
          # second time through, payment processor account is balanced
          # account_uri
          billing.associate(self.participant_id, self.pp_customer_id, self.tok)
  
          user = authentication.User.from_id(self.participant_id)
          # participant in db should be updated to reflect the error message of
          # last update
          self.assertEqual(user.session['last_bill_result'], error_message)
          self.assertTrue(ba.find.call_count)
  
      @mock.patch('balanced.Account')
      def test_clear(self, ba):
          valid_card = mock.Mock()
          valid_card.is_valid = True
          invalid_card = mock.Mock()
          invalid_card.is_valid = False
          card_collection = [
              valid_card, invalid_card
          ]
          balanced.Account.find.return_value.cards = card_collection
  
          MURKY = """\
  
              UPDATE participants
                 SET pp_customer_id='not null'
                   , last_bill_result='ooga booga'
               WHERE id=%s
  
          """
          self.db.execute(MURKY, (self.participant_id,))
  
          billing.clear(self.participant_id, self.pp_customer_id)
  
          self.assertFalse(valid_card.is_valid)
          self.assertTrue(valid_card.save.call_count)
          self.assertFalse(invalid_card.save.call_count)
  
          user = authentication.User.from_id(self.participant_id)
          self.assertFalse(user.session['last_bill_result'])
          self.assertFalse(user.session['pp_customer_id'])
  
subject: test clearing card data