You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_guest_checkout.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import sys
  2. from importlib import import_module, reload
  3. from unittest import mock
  4. from urllib.parse import quote
  5. from django.conf import settings
  6. from django.test.utils import override_settings
  7. from django.urls import clear_url_caches, reverse
  8. from oscar.core.loading import get_class
  9. from oscar.test import factories
  10. from oscar.test.testcases import WebTestCase
  11. from . import (
  12. CheckoutMixin, IndexViewPreConditionsMixin, PaymentDetailsPreviewViewMixin,
  13. PaymentDetailsViewMixin, PaymentMethodViewMixin, ShippingAddressViewMixin,
  14. ShippingMethodViewMixin)
  15. GatewayForm = get_class('checkout.forms', 'GatewayForm')
  16. def reload_url_conf():
  17. # Reload URLs to pick up the overridden settings
  18. if settings.ROOT_URLCONF in sys.modules:
  19. reload(sys.modules[settings.ROOT_URLCONF])
  20. import_module(settings.ROOT_URLCONF)
  21. clear_url_caches()
  22. class AnonymousMixin:
  23. is_anonymous = True
  24. def setUp(self):
  25. reload_url_conf()
  26. super().setUp()
  27. # Disable skip conditions, so that we do not first get redirected forwards
  28. @mock.patch('oscar.apps.checkout.session.CheckoutSessionMixin.skip_unless_basket_requires_shipping')
  29. @mock.patch('oscar.apps.checkout.session.CheckoutSessionMixin.skip_unless_payment_is_required')
  30. def test_does_not_require_login(
  31. self,
  32. mock_skip_unless_payment_is_required,
  33. mock_skip_unless_basket_requires_shipping,
  34. ):
  35. response = self.get(reverse(self.view_name))
  36. self.assertRedirectsTo(response, 'basket:summary')
  37. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  38. class TestIndexView(AnonymousMixin, IndexViewPreConditionsMixin, CheckoutMixin, WebTestCase):
  39. view_name = 'checkout:index'
  40. def test_redirects_new_customers_to_registration_page(self):
  41. self.add_product_to_basket()
  42. page = self.get(reverse('checkout:index'))
  43. form = page.form
  44. form['options'].select(GatewayForm.NEW)
  45. new_user_email = 'newcustomer@test.com'
  46. form['username'].value = new_user_email
  47. response = form.submit()
  48. expected_url = '{register_url}?next={forward}&email={email}'.format(
  49. register_url=reverse('customer:register'),
  50. forward=reverse('checkout:shipping-address'),
  51. email=quote(new_user_email))
  52. self.assertRedirects(response, expected_url)
  53. def test_redirects_existing_customers_to_shipping_address_page(self):
  54. password = 'password'
  55. user = factories.UserFactory(password=password)
  56. self.add_product_to_basket()
  57. page = self.get(reverse('checkout:index'))
  58. form = page.form
  59. form.select('options', GatewayForm.EXISTING)
  60. form['username'].value = user.email
  61. form['password'].value = password
  62. response = form.submit()
  63. self.assertRedirectsTo(response, 'checkout:shipping-address')
  64. def test_redirects_guest_customers_to_shipping_address_page(self):
  65. self.add_product_to_basket()
  66. page = self.get(reverse('checkout:index'))
  67. form = page.form
  68. form.select('options', GatewayForm.GUEST)
  69. form['username'] = 'guest@example.com'
  70. response = form.submit()
  71. self.assertRedirectsTo(response, 'checkout:shipping-address')
  72. def test_prefill_form_with_email_for_returning_guest(self):
  73. self.add_product_to_basket()
  74. email = 'forgetfulguest@test.com'
  75. self.enter_guest_details(email)
  76. page = self.get(reverse('checkout:index'))
  77. self.assertEqual(email, page.form['username'].value)
  78. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  79. class TestShippingAddressView(AnonymousMixin, ShippingAddressViewMixin, CheckoutMixin, WebTestCase):
  80. view_name = 'checkout:shipping-address'
  81. next_view_name = 'checkout:shipping-method'
  82. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  83. class TestShippingMethodView(AnonymousMixin, ShippingMethodViewMixin, CheckoutMixin, WebTestCase):
  84. view_name = 'checkout:shipping-method'
  85. next_view_name = 'checkout:payment-method'
  86. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  87. class TestPaymentMethodView(AnonymousMixin, PaymentMethodViewMixin, CheckoutMixin, WebTestCase):
  88. view_name = 'checkout:payment-method'
  89. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  90. class TestPaymentDetailsView(AnonymousMixin, PaymentDetailsViewMixin, CheckoutMixin, WebTestCase):
  91. view_name = 'checkout:payment-details'
  92. @override_settings(OSCAR_ALLOW_ANON_CHECKOUT=True)
  93. class TestPaymentDetailsPreviewView(AnonymousMixin, PaymentDetailsPreviewViewMixin, CheckoutMixin, WebTestCase):
  94. view_name = 'checkout:preview'
  95. def test_placing_order_saves_guest_email_with_order(self):
  96. preview = self.ready_to_place_an_order()
  97. thank_you = preview.forms['place_order_form'].submit().follow()
  98. order = thank_you.context['order']
  99. self.assertEqual('hello@egg.com', order.guest_email)