forked from saleor/saleor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_account.py
315 lines (270 loc) · 9.73 KB
/
test_account.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import re
from urllib.parse import urlencode
import i18naddress
import pytest
from django.core.exceptions import ValidationError
from django.core.files import File
from django.http import QueryDict
from django.template import Context, Template
from django_countries.fields import Country
from saleor.account import forms, i18n
from saleor.account.i18n import AddressForm
from saleor.account.models import User
from saleor.account.templatetags.i18n_address_tags import format_address
from saleor.account.utils import get_random_avatar, remove_staff_member
from saleor.account.validators import validate_possible_number
@pytest.mark.parametrize("country", ["CN", "PL", "US", "IE"])
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(
"country, phone, is_valid",
(
("US", "123-456-7890", False),
("US", "(541) 754-3010", True),
("FR", "0600000000", True),
),
)
def test_address_form_phone_number_validation(country, phone, is_valid):
data = {"country": country, "phone": phone}
form = forms.get_address_form(data, country_code="PL")[0]
errors = form.errors
if not is_valid:
assert "phone" in errors
else:
assert "phone" not 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_get_address_form_no_country_code():
form, _ = forms.get_address_form(data={}, country_code=None)
assert isinstance(form, AddressForm)
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_data, is_valid",
(
({"phone": "123"}, False),
({"phone": "+48123456789"}, True),
({"phone": "+12025550169"}, True),
({"phone": "+481234567890"}, False),
({"phone": "testext"}, False),
({"phone": "1-541-754-3010"}, False),
({"phone": "001-541-754-3010"}, False),
({"phone": "+1-541-754-3010"}, True),
({"country": "US", "phone": "123-456-7890"}, False),
({"country": "US", "phone": "555-555-5555"}, False),
({"country": "US", "phone": "754-3010"}, False),
({"country": "US", "phone": "001-541-754-3010"}, False),
({"country": "US", "phone": "(541) 754-3010"}, True),
({"country": "US", "phone": "1-541-754-3010"}, True),
({"country": "FR", "phone": "1234567890"}, False),
({"country": "FR", "phone": "0600000000"}, True),
),
)
def test_validate_possible_number(input_data, is_valid):
if not is_valid:
with pytest.raises(ValidationError):
validate_possible_number(**input_data)
else:
validate_possible_number(**input_data)
def test_format_address(address):
formatted_address = format_address(address)
address_html = "<br>".join(map(str, formatted_address["address_lines"]))
context = Context({"address": address})
tpl = Template("{% load i18n_address_tags %}" "{% format_address address %}")
rendered_html = tpl.render(context)
assert address_html in rendered_html
assert "inline-address" not in rendered_html
assert str(address.phone) in rendered_html
def test_format_address_all_options(address):
formatted_address = format_address(
address, include_phone=False, inline=True, latin=True
)
address_html = ", ".join(map(str, formatted_address["address_lines"]))
context = Context({"address": address})
tpl = Template(
r"{% load i18n_address_tags %}"
r"{% format_address address include_phone=False inline=True"
r" latin=True %}"
)
rendered_html = tpl.render(context)
assert address_html in rendered_html
assert "inline-address" in rendered_html
assert str(address.phone) not in rendered_html
def test_address_as_data(address):
data = address.as_data()
assert data == {
"first_name": "John",
"last_name": "Doe",
"company_name": "Mirumee Software",
"street_address_1": "Tęczowa 7",
"street_address_2": "",
"city": "WROCŁAW",
"city_area": "",
"postal_code": "53-601",
"country": "PL",
"country_area": "",
"phone": "+48713988102",
}
def test_copy_address(address):
copied_address = address.get_copy()
assert copied_address.pk != address.pk
assert copied_address == address
def test_compare_addresses(address):
copied_address = address.get_copy()
assert address == copied_address
def test_compare_addresses_with_country_object(address):
copied_address = address.get_copy()
copied_address.country = Country("PL")
copied_address.save()
assert address == copied_address
def test_compare_addresses_different_country(address):
copied_address = address.get_copy()
copied_address.country = Country("FR")
copied_address.save()
assert address != copied_address
@pytest.mark.parametrize(
"email, first_name, last_name, full_name",
[
("[email protected]", "John", "Doe", "John Doe"),
("[email protected]", "John", "", "John"),
("[email protected]", "", "Doe", "Doe"),
("[email protected]", "", "", "[email protected]"),
],
)
def test_get_full_name_user_with_names(
email, first_name, last_name, full_name, address
):
user = User(email=email, first_name=first_name, last_name=last_name)
assert user.get_full_name() == full_name
@pytest.mark.parametrize(
"email, first_name, last_name, full_name",
[
("[email protected]", "John", "Doe", "John Doe"),
("[email protected]", "John", "", "John"),
("[email protected]", "", "Doe", "Doe"),
("[email protected]", "", "", "[email protected]"),
],
)
def test_get_full_name_user_with_address(
email, first_name, last_name, full_name, address
):
address.first_name = first_name
address.last_name = last_name
user = User(email=email, default_billing_address=address)
assert user.get_full_name() == full_name
@pytest.mark.parametrize(
"email, first_name, last_name, full_name",
[
("[email protected]", "John", "Doe", "John Doe"),
("[email protected]", "John", "", "John"),
("[email protected]", "", "Doe", "Doe"),
("[email protected]", "", "", "Arnold Green"),
],
)
def test_get_full_name(email, first_name, last_name, full_name, address):
address.first_name = "Arnold"
address.last_name = "Green"
user = User(
email=email,
first_name=first_name,
last_name=last_name,
default_billing_address=address,
)
assert user.get_full_name() == full_name
def test_get_random_avatar():
avatar = get_random_avatar()
assert isinstance(avatar, File)
assert re.match(r"avatar\d+.png", avatar.name)
def test_remove_staff_member_with_orders(staff_user, permission_manage_products, order):
order.user = staff_user
order.save()
staff_user.user_permissions.add(permission_manage_products)
remove_staff_member(staff_user)
staff_user = User.objects.get(pk=staff_user.pk)
assert not staff_user.is_staff
assert not staff_user.user_permissions.exists()
def test_remove_staff_member(staff_user):
remove_staff_member(staff_user)
assert not User.objects.filter(pk=staff_user.pk).exists()