/
test_userprofile.py
126 lines (110 loc) · 3.76 KB
/
test_userprofile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# encoding: utf-8
from __future__ import unicode_literals
try:
# Python 3
from urllib.parse import urlencode
except ImportError:
# Python 2
from urllib import urlencode
import i18naddress
import pytest
from django.core.exceptions import ValidationError
from django.http import QueryDict
from saleor.userprofile import forms, models, i18n
from saleor.userprofile.validators import validate_possible_number
@pytest.fixture
def billing_address(db):
return models.Address.objects.create(
first_name='John', last_name='Doe',
company_name='Mirumee Software',
street_address_1='Tęczowa 7',
city='Wrocław',
postal_code='53-601',
country='PL')
@pytest.mark.parametrize('country', ['CN', 'PL', 'US'])
def test_address_form_for_country(country):
data = {
'first_name': 'John',
'last_name': 'Doe',
'country': country,
'phone': '123456789'}
form = forms.get_address_form(data, country_code=country)[0]
errors = form.errors
rules = i18naddress.get_validation_rules({'country_code': country})
required = rules.required_fields
if 'street_address' in required:
assert 'street_address_1' in errors
else:
assert 'street_address_1' not in errors
if 'city' in required:
assert 'city' in errors
else:
assert 'city' not in errors
if 'city_area' in required:
assert 'city_area' in errors
else:
assert 'city_area' not in errors
if 'country_area' in required:
assert 'country_area' in errors
else:
assert 'country_area' not in errors
if 'postal_code' in required:
assert 'postal_code' in errors
else:
assert 'postal_code' not in errors
def test_address_form_postal_code_validation():
data = {
'first_name': 'John',
'last_name': 'Doe',
'country': 'PL',
'postal_code': 'XXX'}
form = forms.get_address_form(data, country_code='PL')[0]
errors = form.errors
assert 'postal_code' in errors
@pytest.mark.parametrize('form_data, form_valid, expected_preview, expected_country',[
({'preview': True}, False, True, 'PL'),
({'preview': False,
'street_address_1': 'Foo bar',
'postal_code': '00-123',
'city': 'Warsaw'
}, True, False, 'PL'),
({'preview': True, 'country': 'US'}, False, True, 'US'),
({'preview': False,
'street_address_1': 'Foo bar',
'postal_code': '0213',
'city': 'Warsaw'
}, False, False, 'PL'),
])
def test_get_address_form(form_data, form_valid, expected_preview, expected_country):
data = {
'first_name': 'John',
'last_name': 'Doe',
'country': 'PL'}
data.update(form_data)
query_dict = urlencode(data)
form, preview = forms.get_address_form(
data=QueryDict(query_dict), country_code=data['country'])
assert preview is expected_preview
assert form.is_valid() is form_valid
assert form.i18n_country_code == expected_country
def test_country_aware_form_has_only_supported_countries():
default_form = i18n.COUNTRY_FORMS['US']
instance = default_form()
country_field = instance.fields['country']
country_choices = [code for code, label in country_field.choices]
for country in i18n.UNKNOWN_COUNTRIES:
assert country not in i18n.COUNTRY_FORMS
assert country not in country_choices
@pytest.mark.parametrize("input,exception", [
('123', ValidationError),
('+48123456789', None),
('+12025550169', None),
('+481234567890', ValidationError),
('testext', ValidationError),
])
def test_validate_possible_number(input, exception):
if exception is not None:
with pytest.raises(exception):
validate_possible_number(input)
else:
validate_possible_number(input)