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_forms.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # coding=utf-8
  2. from django.test import TestCase
  3. from django.test.utils import override_settings
  4. from oscar.apps.address.models import Country
  5. from oscar.apps.checkout.forms import ShippingAddressForm
  6. from oscar.test.factories import CountryFactory
  7. class AnotherShippingAddressForm(ShippingAddressForm):
  8. phone_number_fields = {
  9. "phone_number": {
  10. "required": False,
  11. "help_text": "",
  12. "max_length": 32,
  13. "label": "Phone number",
  14. },
  15. "another_phone_number": {
  16. "required": False,
  17. "help_text": "Another phone number help text",
  18. "max_length": 32,
  19. "label": "Another phone number",
  20. },
  21. }
  22. class TestShippingAddressForm(TestCase):
  23. minimal_data = {
  24. "first_name": "Bärry",
  25. "last_name": "Chuckle",
  26. "line1": "1 King St",
  27. "line4": "Gothám",
  28. "postcode": "N1 7RR",
  29. }
  30. def setUp(self):
  31. CountryFactory(iso_3166_1_a2="GB", is_shipping_country=True)
  32. def test_removes_country_field(self):
  33. self.assertTrue("country" not in ShippingAddressForm().fields)
  34. def test_keeps_country_field(self):
  35. CountryFactory(iso_3166_1_a2="DE", is_shipping_country=True)
  36. self.assertTrue("country" in ShippingAddressForm().fields)
  37. @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=("last_name", "postcode"))
  38. def test_required_fields_validated(self):
  39. form = ShippingAddressForm()
  40. self.assertTrue(form.fields["last_name"].required)
  41. self.assertTrue(form.fields["postcode"].required)
  42. self.assertFalse(form.fields["first_name"].required)
  43. self.assertFalse(form.fields["line2"].required)
  44. self.assertFalse(form.fields["line3"].required)
  45. self.assertFalse(form.fields["line4"].required)
  46. @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=("phone_number",))
  47. def test_required_phone_number_validated(self):
  48. # This needs a separate test because of the logic in PhoneNumberMixin
  49. form = ShippingAddressForm()
  50. self.assertTrue(form.fields["phone_number"].required)
  51. # Tests where the country field is hidden
  52. def test_is_valid_without_phone_number(self):
  53. self.assertTrue(ShippingAddressForm(self.minimal_data).is_valid())
  54. def test_only_accepts_british_local_phone_number(self):
  55. data = self.minimal_data.copy()
  56. data["phone_number"] = "07 914721389" # local UK number
  57. self.assertTrue(ShippingAddressForm(data).is_valid())
  58. data["phone_number"] = "0176 968 426 71" # local German number
  59. self.assertFalse(ShippingAddressForm(data).is_valid())
  60. def test_only_accepts_british_local_phone_numbers(self):
  61. data = self.minimal_data.copy()
  62. # Both numbers are British local numbers
  63. data["phone_number"] = "07 914721389"
  64. data["another_phone_number"] = "0344493 0787" # British Airways
  65. self.assertTrue(AnotherShippingAddressForm(data).is_valid())
  66. # Both numbers are local German numbers
  67. data["phone_number"] = "0176 968 426 71"
  68. data["another_phone_number"] = "07032 15 49225" # IBM Germany
  69. self.assertFalse(AnotherShippingAddressForm(data).is_valid())
  70. # One number is British number, another is German number
  71. data["phone_number"] = "07 914721389"
  72. data["another_phone_number"] = "0176 968 426 71"
  73. self.assertFalse(AnotherShippingAddressForm(data).is_valid())
  74. # As previous case, but numbers are reversed
  75. data["phone_number"] = "0176 968 426 71"
  76. data["another_phone_number"] = "07 914721389"
  77. self.assertFalse(AnotherShippingAddressForm(data).is_valid())
  78. def test_is_valid_with_international_phone_number(self):
  79. data = self.minimal_data.copy()
  80. data["phone_number"] = "+49 176 968426 71"
  81. form = ShippingAddressForm(data)
  82. self.assertTrue(form.is_valid())
  83. def test_is_valid_with_international_phone_numbers(self):
  84. data = self.minimal_data.copy()
  85. data["phone_number"] = "+49 176 968426 71"
  86. data["another_phone_number"] = "+49-1805-426452"
  87. form = AnotherShippingAddressForm(data)
  88. self.assertTrue(form.is_valid())
  89. # Tests where the country field exists
  90. def test_needs_country_data(self):
  91. CountryFactory(iso_3166_1_a2="DE", is_shipping_country=True)
  92. self.assertFalse(ShippingAddressForm(self.minimal_data).is_valid())
  93. data = self.minimal_data.copy()
  94. data["country"] = Country.objects.get(iso_3166_1_a2="GB").pk
  95. self.assertTrue(ShippingAddressForm(data).is_valid())
  96. def test_only_accepts_local_phone_number_when_country_matches(self):
  97. CountryFactory(iso_3166_1_a2="DE", is_shipping_country=True)
  98. data = self.minimal_data.copy()
  99. data["phone_number"] = "07 914721389" # local UK number
  100. data["country"] = Country.objects.get(iso_3166_1_a2="DE").pk
  101. self.assertFalse(ShippingAddressForm(data).is_valid())
  102. data["country"] = Country.objects.get(iso_3166_1_a2="GB").pk
  103. self.assertTrue(ShippingAddressForm(data).is_valid())
  104. def test_only_accepts_local_phone_numbers_when_country_matches(self):
  105. CountryFactory(iso_3166_1_a2="DE", is_shipping_country=True)
  106. data = self.minimal_data.copy()
  107. # Local UK numbers
  108. data["phone_number"] = "07 914721389"
  109. data["another_phone_number"] = "0344493 0787"
  110. data["country"] = Country.objects.get(iso_3166_1_a2="DE").pk
  111. self.assertFalse(ShippingAddressForm(data).is_valid())
  112. data["country"] = Country.objects.get(iso_3166_1_a2="GB").pk
  113. self.assertTrue(ShippingAddressForm(data).is_valid())
  114. @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=("phone_number",))
  115. def test_local_phone_number_invalid_without_country(self):
  116. # Add another country, so we have two.
  117. CountryFactory(iso_3166_1_a2="DE", is_shipping_country=True)
  118. data = self.minimal_data.copy()
  119. data["phone_number"] = "07 914721389"
  120. # User hasn't selected a country. Because there are multiple country
  121. # choices we should not accept the local number.
  122. form = ShippingAddressForm(data)
  123. self.assertFalse(form.is_valid())
  124. self.assertIn("phone_number", form.errors)