Changeset View
Changeset View
Standalone View
Standalone View
looper/tests/test_transactions.py
| from unittest import mock | from unittest import mock | ||||
| from django.contrib.admin.models import LogEntry | from django.contrib.admin.models import LogEntry | ||||
| import requests.exceptions | import requests.exceptions | ||||
| import looper.exceptions | import looper.exceptions | ||||
| from . import AbstractLooperTestCase | from .base import AbstractLooperTestCase | ||||
| from .. import admin_log | from .. import admin_log | ||||
| class TransactionsTestCase(AbstractLooperTestCase): | class TransactionsTestCase(AbstractLooperTestCase): | ||||
| def test_transaction_create_from_subscription(self) -> None: | def test_transaction_create_from_subscription(self) -> None: | ||||
| subscription = self.create_subscription() | subscription = self.create_subscription() | ||||
| # We used to generate the initial order automatically, but no longer. | # We used to generate the initial order automatically, but no longer. | ||||
| Show All 35 Lines | class TransactionsTestCase(AbstractLooperTestCase): | ||||
| @mock.patch('looper.gateways.BraintreeGateway.transact_sale') | @mock.patch('looper.gateways.BraintreeGateway.transact_sale') | ||||
| def test_charge_gateway_error(self, mock_transact_sale) -> None: | def test_charge_gateway_error(self, mock_transact_sale) -> None: | ||||
| subscription = self.create_subscription() | subscription = self.create_subscription() | ||||
| order = subscription.generate_order() | order = subscription.generate_order() | ||||
| trans = order.generate_transaction() | trans = order.generate_transaction() | ||||
| mock_transact_sale.assert_not_called() | mock_transact_sale.assert_not_called() | ||||
| mock_transact_sale.side_effect = looper.exceptions.GatewayError( | mock_transact_sale.side_effect = looper.exceptions.GatewayError( | ||||
| 'mocked error', errors=['äuẅ']) | 'mocked error', errors=['äuẅ'] | ||||
| ) | |||||
| self.assertFalse(trans.charge(customer_ip_address='fe80::5ad5:4eaf:feb0:4747')) | self.assertFalse(trans.charge(customer_ip_address='fe80::5ad5:4eaf:feb0:4747')) | ||||
| # The transaction should be marked as failed. | # The transaction should be marked as failed. | ||||
| trans.refresh_from_db() | trans.refresh_from_db() | ||||
| self.assertEqual('failed', trans.status) | self.assertEqual('failed', trans.status) | ||||
| self.assertEqual('', trans.transaction_id) | self.assertEqual('', trans.transaction_id) | ||||
| self.assertEqual(trans.ip_address, 'fe80::5ad5:4eaf:feb0:4747') | self.assertEqual(trans.ip_address, 'fe80::5ad5:4eaf:feb0:4747') | ||||
| ▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | class TransactionsTestCase(AbstractLooperTestCase): | ||||
| def test_charge_very_long_error(self, mock_transact_sale) -> None: | def test_charge_very_long_error(self, mock_transact_sale) -> None: | ||||
| subscription = self.create_subscription() | subscription = self.create_subscription() | ||||
| order = subscription.generate_order() | order = subscription.generate_order() | ||||
| trans = order.generate_transaction() | trans = order.generate_transaction() | ||||
| mock_transact_sale.assert_not_called() | mock_transact_sale.assert_not_called() | ||||
| # We have had this error when the server rack was suffering a DDoS attack, | # We have had this error when the server rack was suffering a DDoS attack, | ||||
| # which was the reason to change the field type from VARCHAR(n) to TEXT. | # which was the reason to change the field type from VARCHAR(n) to TEXT. | ||||
| long_message = ("HTTPSConnectionPool(host='api.braintreegateway.com', port=443): Max " | long_message = ( | ||||
| "HTTPSConnectionPool(host='api.braintreegateway.com', port=443): Max " | |||||
| "retries exceeded with url: /merchants/abcabcdeadbeefaa/transactions (" | "retries exceeded with url: /merchants/abcabcdeadbeefaa/transactions (" | ||||
| "Caused by ConnectTimeoutError(" | "Caused by ConnectTimeoutError(" | ||||
| "<urllib3.connection.VerifiedHTTPSConnection object at 0x7f4ed327a908>, " | "<urllib3.connection.VerifiedHTTPSConnection object at 0x7f4ed327a908>, " | ||||
| "'Connection to api.braintreegateway.com timed out. (connect " | "'Connection to api.braintreegateway.com timed out. (connect " | ||||
| "timeout=60)'))") | "timeout=60)'))" | ||||
| ) | |||||
| mock_transact_sale.side_effect = requests.exceptions.ConnectTimeout(long_message) | mock_transact_sale.side_effect = requests.exceptions.ConnectTimeout(long_message) | ||||
| self.assertFalse(trans.charge(customer_ip_address='fe80::5ad5:4eaf:feb0:4747')) | self.assertFalse(trans.charge(customer_ip_address='fe80::5ad5:4eaf:feb0:4747')) | ||||
| # The transaction should be marked as failed. | # The transaction should be marked as failed. | ||||
| trans.refresh_from_db() | trans.refresh_from_db() | ||||
| self.assertEqual('failed', trans.status) | self.assertEqual('failed', trans.status) | ||||
| self.assertEqual('', trans.transaction_id) | self.assertEqual('', trans.transaction_id) | ||||
| self.assertEqual(trans.ip_address, 'fe80::5ad5:4eaf:feb0:4747') | self.assertEqual(trans.ip_address, 'fe80::5ad5:4eaf:feb0:4747') | ||||
| Show All 38 Lines | |||||