Changeset View
Changeset View
Standalone View
Standalone View
looper/tests/test_checkout.py
- This file was moved from tests/test_checkout.py.
| import datetime | import datetime | ||||
| from typing import Callable, Dict, Iterable, Optional, TypeVar, cast | from typing import Callable, Dict, Iterable, Optional, TypeVar, cast | ||||
| from unittest import mock | from unittest import mock | ||||
| import responses | import responses | ||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||
| from django.dispatch import receiver | from django.dispatch import receiver | ||||
| from django.http import HttpResponse | from django.http import HttpResponse | ||||
| from django.test import override_settings | from django.test import override_settings | ||||
| from django.urls import reverse | from django.urls import reverse | ||||
| from looper import gateways, models, permissions, signals | from looper import gateways, models, permissions, signals | ||||
| from looper.exceptions import GatewayError | from looper.exceptions import GatewayError | ||||
| from looper.models import PlanVariation | from looper.models import PlanVariation | ||||
| from looper.views.tokens import GATEWAY_TOKEN_EXPIRY | from looper.views.tokens import GATEWAY_TOKEN_EXPIRY | ||||
| from tests.base import AbstractLooperTestCase | from .base import AbstractLooperTestCase | ||||
| # Prevent communication with Google's reCAPTCHA API. | # Prevent communication with Google's reCAPTCHA API. | ||||
| @override_settings(GOOGLE_RECAPTCHA_SECRET_KEY='') | @override_settings(GOOGLE_RECAPTCHA_SECRET_KEY='') | ||||
| class AbstractCheckoutTestCase(AbstractLooperTestCase): | class AbstractCheckoutTestCase(AbstractLooperTestCase): | ||||
| fixtures = ['devfund', 'testuser', 'systemuser'] | fixtures = ['devfund', 'testuser', 'systemuser'] | ||||
| valid_payload = { | valid_payload = { | ||||
| 'plan_variation': '6', | 'plan_variation': '6', | ||||
| ▲ Show 20 Lines • Show All 85 Lines • ▼ Show 20 Lines | def setUp(self) -> None: | ||||
| super().setUp() | super().setUp() | ||||
| # Do a GET first to mimic the normal request flow. | # Do a GET first to mimic the normal request flow. | ||||
| self.client.force_login(self.user) | self.client.force_login(self.user) | ||||
| r = self.client.get(self.checkout_url) | r = self.client.get(self.checkout_url) | ||||
| self.assertEqual(r.status_code, 200) | self.assertEqual(r.status_code, 200) | ||||
| @override_settings( | @override_settings( | ||||
| LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='tests.settings.is_never_authorized_for_customer' | LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='looper_example_project.settings.is_never_authorized_for_customer' | ||||
| ) | ) | ||||
| def test_checkout_can_change_customer_fail(self) -> None: | def test_checkout_can_change_customer_fail(self) -> None: | ||||
| self.client.force_login(self.user) | self.client.force_login(self.user) | ||||
| r = self.client.get(self.checkout_url) | r = self.client.get(self.checkout_url) | ||||
| self.assertEqual(r.status_code, 403) | self.assertEqual(r.status_code, 403) | ||||
| def test_checkout_create_missing_required_fields(self) -> None: | def test_checkout_create_missing_required_fields(self) -> None: | ||||
| self.client.force_login(self.user) | self.client.force_login(self.user) | ||||
| ▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | def test_checkout_create_valid_subscription(self) -> None: | ||||
| 'looper:checkout_payment_successful', | 'looper:checkout_payment_successful', | ||||
| kwargs={'customer_id': self.customer.pk, 'transaction_id': transaction.pk}, | kwargs={'customer_id': self.customer.pk, 'transaction_id': transaction.pk}, | ||||
| ) | ) | ||||
| self.assertEqual(success_url, r['Location']) | self.assertEqual(success_url, r['Location']) | ||||
| resp = self.client.get(success_url) | resp = self.client.get(success_url) | ||||
| self.assertEqual(200, resp.status_code) | self.assertEqual(200, resp.status_code) | ||||
| with override_settings( | with override_settings( | ||||
| LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='tests.settings.is_never_authorized_for_customer' | LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='looper_example_project.settings.is_never_authorized_for_customer' | ||||
| ): | ): | ||||
| # But not if we are not allowed to see it. | # But not if we are not allowed to see it. | ||||
| resp = self.client.get(success_url) | resp = self.client.get(success_url) | ||||
| self.assertEqual(403, resp.status_code) | self.assertEqual(403, resp.status_code) | ||||
| def test_fail_on_unsupported_collection_method(self) -> None: | def test_fail_on_unsupported_collection_method(self) -> None: | ||||
| self.assertEqual(0, models.Subscription.objects.count()) | self.assertEqual(0, models.Subscription.objects.count()) | ||||
| ▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | def test_bank(self) -> None: | ||||
| 'looper:checkout_manual_payment', | 'looper:checkout_manual_payment', | ||||
| kwargs={'customer_id': self.customer.pk, 'gateway_name': 'bank', 'order_id': order.pk}, | kwargs={'customer_id': self.customer.pk, 'gateway_name': 'bank', 'order_id': order.pk}, | ||||
| ) | ) | ||||
| self.assertEqual(success_url, resp['Location']) | self.assertEqual(success_url, resp['Location']) | ||||
| resp = self.client.get(success_url) | resp = self.client.get(success_url) | ||||
| self.assertEqual(200, resp.status_code) | self.assertEqual(200, resp.status_code) | ||||
| with override_settings( | with override_settings( | ||||
| LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='tests.settings.is_never_authorized_for_customer' | LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='looper_example_project.settings.is_never_authorized_for_customer' | ||||
| ): | ): | ||||
| # But not if we are not allowed to see it. | # But not if we are not allowed to see it. | ||||
| resp = self.client.get(success_url) | resp = self.client.get(success_url) | ||||
| self.assertEqual(403, resp.status_code) | self.assertEqual(403, resp.status_code) | ||||
| @override_settings( | @override_settings( | ||||
| GOOGLE_RECAPTCHA_SECRET_KEY='-secret-key-', | GOOGLE_RECAPTCHA_SECRET_KEY='-secret-key-', | ||||
| ▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | def test_processor_declined(self) -> None: | ||||
| self.assertEqual(1, models.Subscription.objects.count()) | self.assertEqual(1, models.Subscription.objects.count()) | ||||
| self.assertEqual(1, models.Order.objects.count()) | self.assertEqual(1, models.Order.objects.count()) | ||||
| self.assertEqual(2, models.Transaction.objects.count()) | self.assertEqual(2, models.Transaction.objects.count()) | ||||
| class PayExistingOrder(AbstractCheckoutTestCase): | class PayExistingOrder(AbstractCheckoutTestCase): | ||||
| @override_settings( | @override_settings( | ||||
| LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='tests.settings.is_never_authorized_for_customer' | LOOPER_IS_AUTHORIZED_FOR_CUSTOMER_FUNCTION='looper_example_project.settings.is_never_authorized_for_customer' | ||||
| ) | ) | ||||
| def test_checkout_can_change_customer_fail(self) -> None: | def test_checkout_can_change_customer_fail(self) -> None: | ||||
| subs = self.create_on_hold_subscription() | subs = self.create_on_hold_subscription() | ||||
| order = subs.generate_order() | order = subs.generate_order() | ||||
| self.client.force_login(self.user) | self.client.force_login(self.user) | ||||
| url = reverse( | url = reverse( | ||||
| 'looper:checkout_existing_order', | 'looper:checkout_existing_order', | ||||
| ▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines | |||||