def test_invalid_number_fails_validation(self): class TestForm(PhoneNumberMixin, AbstractAddressForm): phone_number_fields = { 'phone_number': { 'required': False, 'help_text': '', 'max_length': 32, 'label': 'Phone number' } } class Meta: model = ShippingAddress fields = ['country', 'phone_number', 'postcode'] CountryFactory(iso_3166_1_a2='GB', is_shipping_country=True) form = TestForm(data={ 'phone_number': '123456', 'country': 'GB', 'postcode': 'WW1 2BB' }) self.assertFalse(form.is_valid()) self.assertEqual( form.errors['phone_number'], ['This is not a valid local phone format for UNITED KINGDOM.'])
def test_needs_country_data(self): CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True) self.assertFalse(ShippingAddressForm(self.minimal_data).is_valid()) data = self.minimal_data.copy() data['country'] = Country.objects.get(iso_3166_1_a2='GB').pk self.assertTrue(ShippingAddressForm(data).is_valid())
def test_local_phone_number_invalid_without_country(self): # Add another country, so we have two. CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True) data = self.minimal_data.copy() data['phone_number'] = '07 914721389' # User hasn't selected a country. Because there are multiple country # choices we should not accept the local number. form = ShippingAddressForm(data) self.assertFalse(form.is_valid()) self.assertIn('phone_number', form.errors)
def test_only_accepts_local_phone_number_when_country_matches(self): CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True) data = self.minimal_data.copy() data['phone_number'] = '07 914721389' # local UK number data['country'] = Country.objects.get(iso_3166_1_a2='DE').pk self.assertFalse(ShippingAddressForm(data).is_valid()) data['country'] = Country.objects.get(iso_3166_1_a2='GB').pk self.assertTrue(ShippingAddressForm(data).is_valid())
def test_optional_empty_field_validates(self): class TestForm(PhoneNumberMixin, AbstractAddressForm): phone_number_fields = { 'phone_number': { 'required': False, 'help_text': '', 'max_length': 32, 'label': 'Phone number' } } class Meta: model = ShippingAddress fields = ['country', 'phone_number', 'postcode'] CountryFactory(iso_3166_1_a2='GB', is_shipping_country=True) form = TestForm(data={ 'phone_number': '', 'country': 'GB', 'postcode': 'WW1 2BB' }) self.assertTrue(form.is_valid())
def test_required_empty_field_raises_validation_error(self): class TestForm(PhoneNumberMixin, AbstractAddressForm): phone_number_fields = { 'phone_number': { 'required': True, 'help_text': '', 'max_length': 32, 'label': 'Phone number' } } class Meta: model = ShippingAddress fields = ['country', 'phone_number', 'postcode'] CountryFactory(iso_3166_1_a2='GB', is_shipping_country=True) form = TestForm(data={ 'phone_number': '', 'country': 'GB', 'postcode': 'WW1 2BB' }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['phone_number'], ['This field is required.'])
def test_keeps_country_field(self): CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True) self.assertTrue('country' in ShippingAddressForm().fields)
def setUp(self): CountryFactory(iso_3166_1_a2='GB', is_shipping_country=True)