Exemple #1
0
 def test_record_exchange_doesnt_update_balance_for_positive_amounts(self):
     record_exchange(
         self.db, self.janet_route,
         amount=D("0.59"), fee=D("0.41"), vat=D("0.00"),
         participant=self.janet, status='pre',
     )
     janet = Participant.from_username('janet')
     assert self.janet.balance == janet.balance == D('0.00')
 def test_record_exchange_doesnt_update_balance_for_positive_amounts(self):
     record_exchange(self.db,
                     self.janet_route,
                     amount=D("0.59"),
                     fee=D("0.41"),
                     participant=self.janet,
                     status='pre')
     janet = Participant.from_username('janet')
     assert self.janet.balance == janet.balance == D('0.00')
 def test_record_exchange_updates_balance_for_negative_amounts(self):
     self.make_exchange('mango-cc', 50, 0, self.homer)
     record_exchange(
         self.db,
         self.homer_route,
         amount=D('-35.84'),
         fee=D('0.75'),
         participant=self.homer,
         status='pre',
     )
     homer = Participant.from_username('homer')
     assert homer.balance == D('13.41')
 def test_record_exchange_updates_balance_for_negative_amounts(self):
     self.make_exchange('mango-cc', 50, 0, self.homer)
     record_exchange(
         self.db,
         self.homer_route,
         amount=D('-35.84'),
         fee=D('0.75'),
         participant=self.homer,
         status='pre',
     )
     homer = Participant.from_username('homer')
     assert homer.balance == D('13.41')
 def test_payout_callback(self, Get):
     homer, ba = self.homer, self.homer_route
     for status in ('succeeded', 'failed'):
         status_up = status.upper()
         error = 'FOO' if status == 'failed' else None
         self.make_exchange('mango-cc', 10, 0, homer)
         e_id = record_exchange(self.db, ba, -10, 0, 0, homer, 'pre')
         assert homer.balance == 0
         homer.close(None)
         assert homer.status == 'closed'
         qs = "EventType=PAYOUT_NORMAL_" + status_up + "&RessourceId=123456790"
         payout = PayOut()
         payout.Status = status_up
         payout.ResultCode = '000001' if error else '000000'
         payout.ResultMessage = error
         payout.AuthorId = homer.mangopay_user_id
         payout.Tag = str(e_id)
         Get.return_value = payout
         r = self.callback(qs)
         assert CSRF_TOKEN not in r.headers.cookie
         assert r.code == 200, r.text
         homer = homer.refetch()
         if status == 'succeeded':
             assert homer.balance == 0
             assert homer.status == 'closed'
         else:
             assert homer.balance == 10
             assert homer.status == 'active'
             emails = self.get_emails()
             assert len(emails) == 1
             assert emails[0]['to'][0] == 'homer <%s>' % homer.email
             assert 'fail' in emails[0]['subject']
         homer.update_status('active')  # reset for next loop run
 def test_payin_bank_wire_callback(self, Get):
     homer = self.homer
     route = ExchangeRoute.insert(homer, 'mango-bw', 'x')
     for status in ('failed', 'succeeded'):
         status_up = status.upper()
         error = 'FOO' if status == 'failed' else None
         e_id = record_exchange(self.db, route, 11, 0, 0, homer, 'pre')
         assert homer.balance == 0
         homer.close(None)
         assert homer.status == 'closed'
         qs = "EventType=PAYIN_NORMAL_"+status_up+"&RessourceId=123456790"
         payin = PayIn()
         payin.Status = status_up
         payin.ResultCode = '000001' if error else '000000'
         payin.ResultMessage = error
         payin.AuthorId = homer.mangopay_user_id
         payin.PaymentType = 'BANK_WIRE'
         payin.Tag = str(e_id)
         Get.return_value = payin
         r = self.callback(qs)
         assert r.code == 200, r.text
         homer = homer.refetch()
         if status == 'succeeded':
             assert homer.balance == 11
             assert homer.status == 'active'
         else:
             assert homer.balance == 0
             assert homer.status == 'closed'
         emails = self.get_emails()
         assert len(emails) == 1
         assert emails[0]['to'][0] == 'homer <%s>' % homer.email
         assert status[:4] in emails[0]['subject']
         homer.update_status('active')  # reset for next loop run
 def test_payout_callback(self, Get):
     homer, ba = self.homer, self.homer_route
     for status in ('succeeded', 'failed'):
         status_up = status.upper()
         error = 'FOO' if status == 'failed' else None
         self.make_exchange('mango-cc', 10, 0, homer)
         e_id = record_exchange(self.db, ba, -10, 0, homer, 'pre')
         assert homer.balance == 0
         homer.close(None)
         assert homer.status == 'closed'
         qs = "EventType=PAYOUT_NORMAL_"+status_up+"&RessourceId=123456790"
         payout = PayOut()
         payout.Status = status_up
         payout.ResultCode = '000001' if error else '000000'
         payout.ResultMessage = error
         payout.AuthorId = homer.mangopay_user_id
         payout.Tag = str(e_id)
         Get.return_value = payout
         r = self.callback(qs, csrf_token=False)
         assert b'csrf_token' not in r.headers.cookie
         assert r.code == 200, r.text
         homer = homer.refetch()
         if status == 'succeeded':
             assert homer.balance == 0
             assert homer.status == 'closed'
         else:
             assert homer.balance == 10
             assert homer.status == 'active'
         homer.update_status('active')  # reset for next loop run
Exemple #8
0
 def _test_payin_bank_wire_callback_amount_mismatch(self, Get, fee):
     homer = self.homer
     route = ExchangeRoute.insert(homer, 'mango-bw', 'x')
     e_id = record_exchange(self.db, route, 11, 0, 0, homer, 'pre')
     assert homer.balance == 0
     homer.close(None)
     assert homer.status == 'closed'
     qs = "EventType=PAYIN_NORMAL_SUCCEEDED&RessourceId=123456790"
     payin = BankWirePayIn()
     payin.Status = 'SUCCEEDED'
     payin.ResultCode = '000000'
     payin.ResultMessage = None
     payin.AuthorId = homer.mangopay_user_id
     payin.PaymentType = 'BANK_WIRE'
     payin.DeclaredDebitedFunds = Money(4500, 'EUR')
     payin.DeclaredFees = Money(100, 'EUR')
     payin.DebitedFunds = Money(302, 'EUR')
     payin.Fees = Money(fee, 'EUR')
     payin.CreditedFunds = Money(302 - fee, 'EUR')
     payin.Tag = str(e_id)
     Get.return_value = payin
     r = self.callback(qs)
     assert r.code == 200, r.text
     e = self.db.one("SELECT * FROM exchanges WHERE id = %s", (e_id, ))
     assert e.amount == D(payin.CreditedFunds.Amount) / D(100)
     assert e.fee == D(fee) / D(100)
     assert e.vat == D('0.01')
     assert e.status == 'succeeded'
     homer = homer.refetch()
     assert homer.balance == e.amount
     assert homer.status == 'active'
     emails = self.get_emails()
     assert len(emails) == 1
     assert emails[0]['to'][0] == 'homer <%s>' % homer.email
     assert 'succ' in emails[0]['subject']
Exemple #9
0
 def test_record_exchange_result_doesnt_restore_balance_on_success(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 50, 0, homer)
     e_id = record_exchange(self.db, ba, D('-43.98'), D('1.60'), 0, homer, 'pre')
     assert homer.balance == D('4.42')
     record_exchange_result(self.db, e_id, 'succeeded', None, homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('4.42')
Exemple #10
0
 def test_record_exchange_result_updates_balance_for_positive_amounts(self):
     janet, cc = self.janet, self.janet_route
     self.make_exchange('mango-cc', 4, 0, janet)
     e_id = record_exchange(self.db, cc, D('31.59'), D('0.01'), 0, janet, 'pre')
     assert janet.balance == D('4.00')
     record_exchange_result(self.db, e_id, 'succeeded', None, janet)
     janet = Participant.from_username('janet')
     assert janet.balance == D('35.59')
Exemple #11
0
 def test_record_exchange_result_restores_balance_on_error(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 30, 0, homer)
     e_id = record_exchange(self.db, ba, D('-27.06'), D('0.81'), 0, homer, 'pre')
     assert homer.balance == D('02.13')
     record_exchange_result(self.db, e_id, 'failed', 'SOME ERROR', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('30.00')
Exemple #12
0
 def test_record_exchange_result_updates_balance_for_positive_amounts(self):
     janet, cc = self.janet, self.janet_route
     self.make_exchange('mango-cc', 4, 0, janet)
     e_id = record_exchange(self.db, cc, D('31.59'), D('0.01'), 0, janet,
                            'pre')
     assert janet.balance == D('4.00')
     record_exchange_result(self.db, e_id, 'succeeded', None, janet)
     janet = Participant.from_username('janet')
     assert janet.balance == D('35.59')
Exemple #13
0
 def test_record_exchange_result_doesnt_restore_balance_on_success(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 50, 0, homer)
     e_id = record_exchange(self.db, ba, D('-43.98'), D('1.60'), 0, homer,
                            'pre')
     assert homer.balance == D('4.42')
     record_exchange_result(self.db, e_id, 'succeeded', None, homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('4.42')
Exemple #14
0
 def test_record_exchange_result_restores_balance_on_error(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 30, 0, homer)
     e_id = record_exchange(self.db, ba, D('-27.06'), D('0.81'), 0, homer,
                            'pre')
     assert homer.balance == D('02.13')
     record_exchange_result(self.db, e_id, 'failed', 'SOME ERROR', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('30.00')
Exemple #15
0
 def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 37, 0, homer)
     e_id = record_exchange(self.db, ba, D('-32.45'), D('0.86'), 0, homer, 'pre')
     assert homer.balance == D('3.69')
     ba.update_error('invalidated')
     record_exchange_result(self.db, e_id, 'failed', 'oops', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('37.00')
     assert ba.error == homer.get_bank_account_error() == 'invalidated'
Exemple #16
0
 def make_exchange(self, route, amount, fee, participant, status='succeeded', error='', vat=0):
     if not isinstance(route, ExchangeRoute):
         network = route
         route = ExchangeRoute.from_network(participant, network)
         if not route:
             from .mangopay import MangopayHarness
             route = ExchangeRoute.insert(participant, network, MangopayHarness.card_id)
             assert route
     e_id = record_exchange(self.db, route, amount, fee, vat, participant, 'pre')
     record_exchange_result(self.db, e_id, status, error, participant)
     return e_id
Exemple #17
0
 def make_exchange(self, route, amount, fee, participant, status='succeeded', error='', vat=0):
     if not isinstance(route, ExchangeRoute):
         network = route
         route = ExchangeRoute.from_network(participant, network)
         if not route:
             from .mangopay import MangopayHarness
             route = ExchangeRoute.insert(participant, network, MangopayHarness.card_id)
             assert route
     e_id = record_exchange(self.db, route, amount, fee, vat, participant, 'pre')
     record_exchange_result(self.db, e_id, status, error, participant)
     return e_id
Exemple #18
0
 def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(
         self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 37, 0, homer)
     e_id = record_exchange(self.db, ba, D('-32.45'), D('0.86'), 0, homer,
                            'pre')
     assert homer.balance == D('3.69')
     ba.update_error('invalidated')
     record_exchange_result(self.db, e_id, 'failed', 'oops', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('37.00')
     assert ba.error == homer.get_bank_account_error() == 'invalidated'
 def test_payout_refund_callback(self, R_Get, PO_Get):
     homer, ba = self.homer, self.homer_route
     for status in ('failed', 'succeeded'):
         # Create the payout
         self.make_exchange('mango-cc', 10, 0, homer)
         e_id = record_exchange(self.db, ba, -9, 1, 0, homer, 'pre')
         assert homer.balance == 0
         homer.close(None)
         assert homer.status == 'closed'
         payout = PayOut()
         payout.Status = 'SUCCEEDED'
         payout.ResultCode = '000000'
         payout.AuthorId = homer.mangopay_user_id
         payout.Tag = str(e_id)
         PO_Get.return_value = payout
         # Create the refund
         status_up = status.upper()
         error = 'FOO' if status == 'failed' else None
         refund = Refund()
         refund.DebitedFunds = Money(900, 'EUR')
         refund.Fees = Money(-100, 'EUR')
         refund.Status = status_up
         refund.ResultCode = '000001' if error else '000000'
         refund.ResultMessage = error
         reason = refund.RefundReason = RefundReason()
         reason.RefundReasonMessage = 'BECAUSE 42'
         refund.AuthorId = homer.mangopay_user_id
         R_Get.return_value = refund
         # Call back
         qs = "EventType=PAYOUT_REFUND_" + status_up + "&RessourceId=123456790"
         r = self.callback(qs)
         assert r.code == 200, r.text
         homer = homer.refetch()
         if status == 'failed':
             assert homer.balance == 0
             assert homer.status == 'closed'
         else:
             assert homer.balance == 10
             assert homer.status == 'active'
             emails = self.get_emails()
             assert len(emails) == 1
             assert emails[0]['to'][0] == 'homer <%s>' % homer.email
             assert 'fail' in emails[0]['subject']
             assert 'BECAUSE 42' in emails[0]['text']
         homer.update_status('active')  # reset for next loop run
 def test_payin_bank_wire_callback(self, Get):
     homer = self.homer
     route = ExchangeRoute.insert(homer, 'mango-bw', 'x')
     for status in ('failed', 'succeeded'):
         status_up = status.upper()
         error = 'FOO' if status == 'failed' else None
         e_id = record_exchange(self.db, route, 11, 0, 0, homer, 'pre')
         assert homer.balance == 0
         homer.close(None)
         assert homer.status == 'closed'
         qs = "EventType=PAYIN_NORMAL_" + status_up + "&RessourceId=123456790"
         payin = PayIn()
         payin.Status = status_up
         payin.ResultCode = '000001' if error else '000000'
         payin.ResultMessage = error
         payin.AuthorId = homer.mangopay_user_id
         payin.PaymentType = 'BANK_WIRE'
         pd = payin.PaymentDetails = PayInPaymentDetailsBankWire()
         pd.DeclaredDebitedFunds = Money(1100, 'EUR').__dict__
         pd.DeclaredFees = Money(0, 'EUR').__dict__
         payin.CreditedFunds = Money(1100, 'EUR')
         payin.Tag = str(e_id)
         Get.return_value = payin
         r = self.callback(qs)
         assert r.code == 200, r.text
         homer = homer.refetch()
         if status == 'succeeded':
             assert homer.balance == 11
             assert homer.status == 'active'
         else:
             assert homer.balance == 0
             assert homer.status == 'closed'
         emails = self.get_emails()
         assert len(emails) == 1
         assert emails[0]['to'][0] == 'homer <%s>' % homer.email
         assert status[:4] in emails[0]['subject']
         homer.update_status('active')  # reset for next loop run
Exemple #21
0
 def test_record_exchange_fails_if_negative_balance(self):
     with pytest.raises(NegativeBalance):
         record_exchange(self.db, self.homer_route, D("-10.00"), D("0.41"),
                         0, self.homer, 'pre')
Exemple #22
0
 def test_record_exchange_failure(self):
     record_exchange(self.db, self.janet_route, D("10.00"), D("0.01"), 0,
                     self.janet, 'failed', 'OOPS')
     janet = Participant.from_id(self.janet.id)
     assert self.janet.balance == janet.balance == 0
     assert self.janet_route.error == 'OOPS'
Exemple #23
0
 def test_record_exchange_fails_if_negative_balance(self):
     with pytest.raises(NegativeBalance):
         record_exchange(self.db, self.homer_route, D("-10.00"), D("0.41"), 0, self.homer, 'pre')
Exemple #24
0
 def test_record_exchange_failure(self):
     record_exchange(self.db, self.janet_route, D("10.00"), D("0.01"), 0, self.janet, 'failed', 'OOPS')
     janet = Participant.from_id(self.janet.id)
     assert self.janet.balance == janet.balance == 0
     assert self.janet_route.error == 'OOPS'