forked from saleor/saleor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_checkout_integration.py
358 lines (297 loc) · 13.9 KB
/
test_checkout_integration.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from django.urls import reverse
from payments import FraudStatus, PaymentStatus
from saleor.userprofile.models import User
from .utils import get_redirect_location
def test_checkout_flow(request_cart_with_item, client, shipping_method):
# Enter checkout
checkout_index = client.get(reverse('checkout:index'), follow=True)
# Checkout index redirects directly to shipping address step
shipping_address = client.get(checkout_index.request['PATH_INFO'])
# Enter shipping address data
shipping_data = {
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Doe',
'street_address_1': 'Aleje Jerozolimskie 2',
'street_address_2': '',
'city': 'Warszawa',
'city_area': '',
'country_area': '',
'postal_code': '00-374',
'phone': '+48536984008',
'country': 'PL'}
shipping_response = client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)
# Select shipping method
shipping_method_page = client.get(shipping_response.request['PATH_INFO'])
# Redirect to summary after shipping method selection
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True)
# Summary page asks for Billing address, default is the same as shipping
address_data = {'address': 'shipping_address'}
summary_response = client.post(
shipping_method_response.request['PATH_INFO'], data=address_data,
follow=True)
# After summary step, order is created and it waits for payment
order = summary_response.context['order']
# Select payment method
payment_page = client.post(
summary_response.request['PATH_INFO'], data={'method': 'default'},
follow=True)
assert len(payment_page.redirect_chain) == 1
assert payment_page.status_code == 200
# Go to payment details page, enter payment data
payment_page_url = payment_page.redirect_chain[0][0]
payment_data = {
'status': PaymentStatus.PREAUTH,
'fraud_status': FraudStatus.UNKNOWN,
'gateway_response': '3ds-disabled',
'verification_result': 'waiting'}
payment_response = client.post(payment_page_url, data=payment_data)
assert payment_response.status_code == 302
order_password = reverse(
'order:create-password', kwargs={'token': order.token})
assert get_redirect_location(payment_response) == order_password
def test_checkout_flow_authenticated_user(
authorized_client, billing_address, request_cart_with_item,
customer_user, shipping_method):
# Prepare some data
customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
# Enter checkout
# Checkout index redirects directly to shipping address step
shipping_address = authorized_client.get(
reverse('checkout:index'), follow=True)
# Enter shipping address data
shipping_data = {'address': billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)
# Select shipping method
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = authorized_client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True)
# Summary page asks for Billing address, default is the same as shipping
payment_method_data = {'address': 'shipping_address'}
payment_method_page = authorized_client.post(
shipping_method_response.request['PATH_INFO'],
data=payment_method_data, follow=True)
# After summary step, order is created and it waits for payment
order = payment_method_page.context['order']
# Select payment method
payment_page = authorized_client.post(
payment_method_page.request['PATH_INFO'], data={'method': 'default'},
follow=True)
# Go to payment details page, enter payment data
payment_data = {
'status': PaymentStatus.PREAUTH,
'fraud_status': FraudStatus.UNKNOWN,
'gateway_response': '3ds-disabled',
'verification_result': 'waiting'}
payment_response = authorized_client.post(
payment_page.request['PATH_INFO'], data=payment_data)
assert payment_response.status_code == 302
order_password = reverse(
'order:create-password', kwargs={'token': order.token})
assert get_redirect_location(payment_response) == order_password
def test_address_without_shipping(request_cart_with_item, client, monkeypatch):
monkeypatch.setattr(
'saleor.checkout.core.Checkout.is_shipping_required', False)
response = client.get(reverse('checkout:shipping-address'))
assert response.status_code == 302
assert get_redirect_location(response) == reverse('checkout:summary')
def test_shipping_method_without_shipping(
request_cart_with_item, client, monkeypatch):
monkeypatch.setattr('saleor.checkout.core.Checkout.is_shipping_required',
False)
response = client.get(reverse('checkout:shipping-method'))
assert response.status_code == 302
assert get_redirect_location(response) == reverse('checkout:summary')
def test_shipping_method_without_address(request_cart_with_item, client):
response = client.get(reverse('checkout:shipping-method'))
assert response.status_code == 302
assert (
get_redirect_location(response) ==
reverse('checkout:shipping-address'))
def test_summary_without_address(request_cart_with_item, client):
response = client.get(reverse('checkout:summary'))
assert response.status_code == 302
assert (
get_redirect_location(response) == reverse('checkout:shipping-method'))
def test_summary_without_shipping_method(
request_cart_with_item, client, monkeypatch):
# address test return true
monkeypatch.setattr(
'saleor.checkout.core.Checkout.email', True)
response = client.get(reverse('checkout:summary'))
assert response.status_code == 302
assert (
get_redirect_location(response) == reverse('checkout:shipping-method'))
def test_email_is_saved_in_order(
authorized_client, billing_address, customer_user,
request_cart_with_item, shipping_method):
# Prepare some data
customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
# Enter checkout
# Checkout index redirects directly to shipping address step
shipping_address = authorized_client.get(
reverse('checkout:index'), follow=True)
# Enter shipping address data
shipping_data = {'address': billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True)
# Select shipping method
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = authorized_client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True)
# Summary page asks for Billing address, default is the same as shipping
payment_method_data = {'address': 'shipping_address'}
payment_method_page = authorized_client.post(
shipping_method_response.request['PATH_INFO'],
data=payment_method_data, follow=True)
# After summary step, order is created and it waits for payment
order = payment_method_page.context['order']
assert order.user_email == customer_user.email
def test_voucher_invalid(
client, request_cart_with_item, shipping_method, voucher):
# related issues: #549 #544
voucher.usage_limit = 3
voucher.save()
# Enter checkout
checkout_index = client.get(reverse('checkout:index'), follow=True)
# Checkout index redirects directly to shipping address step
shipping_address = client.get(checkout_index.request['PATH_INFO'])
# Enter shipping address data
shipping_data = {
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Doe',
'street_address_1': 'Aleje Jerozolimskie 2',
'street_address_2': '',
'city': 'Warszawa',
'city_area': '',
'country_area': '',
'postal_code': '00-374',
'country': 'PL'}
shipping_response = client.post(shipping_address.request['PATH_INFO'],
data=shipping_data, follow=True)
# Select shipping method
shipping_method_page = client.get(shipping_response.request['PATH_INFO'])
# Redirect to summary after shipping method selection
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True)
# Summary page asks for Billing address, default is the same as shipping
url = shipping_method_response.request['PATH_INFO']
discount_data = {'discount-voucher': voucher.code}
voucher_response = client.post(
'{url}?next={url}'.format(url=url), follow=True, data=discount_data,
HTTP_REFERER=url)
assert voucher_response.context['checkout'].voucher_code == voucher.code
voucher.used = 3
voucher.save()
address_data = {'address': 'shipping_address'}
assert url == reverse('checkout:summary')
summary_response = client.post(url, data=address_data, follow=True)
assert summary_response.context['checkout'].voucher_code is None
summary_response = client.post(url, data=address_data, follow=True)
assert summary_response.context['order'].voucher is None
def test_remove_voucher(
client, request_cart_with_item, shipping_method, voucher):
# Enter checkout
checkout_index = client.get(reverse('checkout:index'), follow=True)
# Checkout index redirects directly to shipping address step
shipping_address = client.get(checkout_index.request['PATH_INFO'])
# Enter shipping address data
shipping_data = {
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Doe',
'street_address_1': 'Aleje Jerozolimskie 2',
'street_address_2': '',
'city': 'Warszawa',
'city_area': '',
'country_area': '',
'postal_code': '00-374',
'country': 'PL'}
shipping_response = client.post(shipping_address.request['PATH_INFO'],
data=shipping_data, follow=True)
# Select shipping method
shipping_method_page = client.get(shipping_response.request['PATH_INFO'])
# Redirect to summary after shipping method selection
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True)
# Summary page asks for Billing address, default is the same as shipping
url = shipping_method_response.request['PATH_INFO']
discount_data = {'discount-voucher': voucher.code}
voucher_response = client.post(
'{url}?next={url}'.format(url=url), follow=True, data=discount_data,
HTTP_REFERER=url)
assert voucher_response.context['checkout'].voucher_code is not None
# Remove voucher from checkout
voucher_response = client.post(
reverse('checkout:remove-voucher'), follow=True, HTTP_REFERER=url)
assert voucher_response.status_code == 200
assert voucher_response.context['checkout'].voucher_code is None
def test_language_is_saved_in_order(
authorized_client, billing_address, customer_user,
request_cart_with_item, settings, shipping_method):
# Prepare some data
user_language = 'fr'
settings.LANGUAGE_CODE = 'en'
customer_user.addresses.add(billing_address)
request_cart_with_item.user = customer_user
request_cart_with_item.save()
# Enter checkout
# Checkout index redirects directly to shipping address step
shipping_address = authorized_client.get(
reverse('checkout:index'), follow=True,
HTTP_ACCEPT_LANGUAGE=user_language)
# Enter shipping address data
shipping_data = {'address': billing_address.pk}
shipping_method_page = authorized_client.post(
shipping_address.request['PATH_INFO'], data=shipping_data, follow=True,
HTTP_ACCEPT_LANGUAGE=user_language)
# Select shipping method
shipping_method_data = {
'method': shipping_method.price_per_country.first().pk}
shipping_method_response = authorized_client.post(
shipping_method_page.request['PATH_INFO'], data=shipping_method_data,
follow=True, HTTP_ACCEPT_LANGUAGE=user_language)
# Summary page asks for Billing address, default is the same as shipping
payment_method_data = {'address': 'shipping_address'}
payment_method_page = authorized_client.post(
shipping_method_response.request['PATH_INFO'],
data=payment_method_data, follow=True,
HTTP_ACCEPT_LANGUAGE=user_language)
# After summary step, order is created and it waits for payment
order = payment_method_page.context['order']
assert order.language_code == user_language
def test_create_user_after_order(order, client):
order.user_email = '[email protected]'
order.save()
assert not User.objects.filter(email='[email protected]').exists()
data = {'password': 'password'}
url = reverse('order:create-password', kwargs={'token': order.token})
response = client.post(url, data=data)
redirect_location = get_redirect_location(response)
detail_url = reverse('order:details', kwargs={'token': order.token})
assert redirect_location == detail_url
user = User.objects.filter(email='[email protected]')
assert user.exists()
user = user.first()
assert user.orders.filter(token=order.token).exists()