def test_dwolla_payment_form_handles_valueerror(self, oauth_refresh, dwolla_get_sources): oauth_refresh.return_value = { 'error': 'access_denied', 'error_description': 'Arbitrary error code.' } dwolla_get_sources.return_value = DWOLLA_SOURCES order = OrderFactory() order.event.organization.dwolla_test_account = DwollaOrganizationAccountFactory( access_token_expires=timezone.now() - timedelta(1)) order.event.organization.save() person = PersonFactory() person.dwolla_test_account = DwollaUserAccountFactory() person.save() pin = '1234' source = 'Balance' form = DwollaPaymentForm(order=order, amount=Decimal('42.15'), data={ 'dwolla_pin': pin, 'source': source }, user=person) self.assertTrue(form.is_bound) self.assertTrue(form.errors) self.assertEqual(form.errors['__all__'], [oauth_refresh.return_value['error_description']])
def test_dwolla_charge__user(self): event = EventFactory(api_type=Event.TEST, application_fee_percent=Decimal('2.5')) event.organization.dwolla_test_account = DwollaOrganizationAccountFactory( ) event.organization.save() self.assertTrue(event.dwolla_connected()) dwolla_prep(Event.TEST) person = PersonFactory() person.dwolla_test_account = DwollaUserAccountFactory() person.save() order = OrderFactory(person=person, event=event, code='dwoll1') charge = dwolla_charge( account=person.dwolla_test_account, amount=42.15, order=order, event=event, pin=settings.DWOLLA_TEST_USER_PIN, source='Balance', ) self.assertIsInstance(charge, dict) self.assertEqual(charge["Type"], "money_received") self.assertEqual(len(charge['Fees']), 1) self.assertEqual(charge["Notes"], "Order {} for {}".format(order.code, event.name)) txn = Transaction.from_dwolla_charge(charge, event=event) # 42.15 * 0.025 = 1.05 self.assertEqual(Decimal(txn.application_fee), Decimal('1.05')) # 0.25 self.assertEqual(Decimal(txn.processing_fee), Decimal('0')) refund = dwolla_refund(order=order, event=event, payment_id=txn.remote_id, amount=txn.amount, pin=settings.DWOLLA_TEST_ORGANIZATION_PIN) self.assertIsInstance(refund, dict) self.assertEqual(refund["Amount"], txn.amount) refund_info = transactions.info( tid=str(refund['TransactionId']), alternate_token=event.organization.get_dwolla_account( event.api_type).get_token()) self.assertEqual(refund_info["Notes"], "Order {} for {}".format(order.code, event.name)) refund_txn = Transaction.from_dwolla_refund(refund, txn, event=event) self.assertEqual(refund_txn.amount, -1 * txn.amount) self.assertEqual(refund_txn.application_fee, 0) self.assertEqual(refund_txn.processing_fee, 0)
def test_dolla_charge__negative(self): event = EventFactory(api_type=Event.TEST, application_fee_percent=Decimal('2.5')) event.organization.dwolla_test_account = DwollaOrganizationAccountFactory( ) event.organization.save() self.assertTrue(event.dwolla_connected()) dwolla_prep(Event.TEST) person = PersonFactory() person.dwolla_test_account = DwollaUserAccountFactory() person.save() order = OrderFactory(person=person, event=event, code='dwoll1') with self.assertRaises(InvalidAmountException): dwolla_charge( account=person.dwolla_test_account, amount=-1.00, order=order, event=event, pin=settings.DWOLLA_TEST_USER_PIN, source='Balance', )
def test_negative_charge_adds_errors(self, dwolla_get_sources): dwolla_get_sources.return_value = DWOLLA_SOURCES order = OrderFactory() event = order.event event.organization.dwolla_test_account = DwollaOrganizationAccountFactory( ) event.organization.save() person = PersonFactory() person.dwolla_test_account = DwollaUserAccountFactory() person.save() pin = '1234' source = 'Balance' form = DwollaPaymentForm(order=order, amount=Decimal('-1.00'), data={ 'dwolla_pin': pin, 'source': source }, user=person) self.assertTrue(form.is_bound) self.assertTrue(form.errors) self.assertEqual(form.errors['__all__'], ["Cannot charge an amount less than zero."])
def test_dwolla_payment_form(self, dwolla_charge, dwolla_get_sources): dwolla_charge.return_value = DWOLLA_CHARGE dwolla_get_sources.return_value = DWOLLA_SOURCES order = OrderFactory() event = order.event event.organization.dwolla_test_account = DwollaOrganizationAccountFactory( ) event.organization.save() person = PersonFactory() person.dwolla_test_account = DwollaUserAccountFactory() person.save() pin = '1234' source = 'Balance' form = DwollaPaymentForm(order=order, amount=Decimal('42.15'), data={ 'dwolla_pin': pin, 'source': source }, user=person) self.assertTrue(form.is_bound) self.assertFalse(form.errors) dwolla_charge.assert_called_once_with( account=person.dwolla_test_account, amount=42.15, event=event, pin=pin, source=source, order=order, ) txn = form.save() self.assertIsInstance(txn, Transaction) self.assertEqual(txn.event, event) self.assertEqual(Decimal(str(txn.amount)), Decimal('42.15')) self.assertEqual(Decimal(str(txn.application_fee)), Decimal('1.05')) self.assertEqual(Decimal(str(txn.processing_fee)), Decimal('0.25'))