This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_checkout.py
1561 lines (1255 loc) · 52.4 KB
/
test_checkout.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
from unittest.mock import Mock, patch
import pytest
from django.contrib.auth.models import AnonymousUser
from django.urls import reverse
from django.utils import timezone
from django_countries.fields import Country
from freezegun import freeze_time
from prices import Money, TaxedMoney, TaxedMoneyRange
from saleor.account import CustomerEvents
from saleor.account.models import Address, CustomerEvent, User
from saleor.account.utils import store_user_address
from saleor.checkout import AddressType, views
from saleor.checkout.forms import CheckoutVoucherForm, CountryForm
from saleor.checkout.utils import (
add_variant_to_checkout,
add_voucher_to_checkout,
change_billing_address_in_checkout,
change_shipping_address_in_checkout,
clear_shipping_method,
create_order,
get_checkout_context,
get_shipping_price_estimate,
get_voucher_discount_for_checkout,
get_voucher_for_checkout,
is_valid_shipping_method,
prepare_order_data,
recalculate_checkout_discount,
remove_voucher_from_checkout,
)
from saleor.core.exceptions import InsufficientStock
from saleor.core.taxes import zero_money, zero_taxed_money
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import NotApplicable, Voucher
from saleor.extensions.manager import ExtensionsManager, get_extensions_manager
from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.models import OrderEvent
from saleor.shipping.models import ShippingZone
from .utils import get_redirect_location
def test_country_form_country_choices():
form = CountryForm(data={"csrf": "", "country": "PL"})
assert form.fields["country"].choices == []
zone = ShippingZone.objects.create(countries=["PL", "DE"], name="Europe")
form = CountryForm(data={"csrf": "", "country": "PL"})
expected_choices = [(country.code, country.name) for country in zone.countries]
expected_choices = sorted(expected_choices, key=lambda choice: choice[1])
assert form.fields["country"].choices == expected_choices
def test_is_valid_shipping_method(checkout_with_item, address, shipping_zone):
checkout = checkout_with_item
checkout.shipping_address = address
checkout.save()
# no shipping method assigned
assert not is_valid_shipping_method(checkout, None)
shipping_method = shipping_zone.shipping_methods.first()
checkout.shipping_method = shipping_method
checkout.save()
assert is_valid_shipping_method(checkout, None)
zone = ShippingZone.objects.create(name="DE", countries=["DE"])
shipping_method.shipping_zone = zone
shipping_method.save()
assert not is_valid_shipping_method(checkout, None)
def test_clear_shipping_method(checkout, shipping_method):
checkout.shipping_method = shipping_method
checkout.save()
clear_shipping_method(checkout)
checkout.refresh_from_db()
assert not checkout.shipping_method
@pytest.mark.parametrize(
"checkout_length, is_shipping_required, redirect_url",
[
(0, True, reverse("checkout:index")),
(0, False, reverse("checkout:index")),
(1, True, reverse("checkout:shipping-address")),
(1, False, reverse("checkout:summary")),
],
)
def test_view_checkout_index(
monkeypatch, rf, checkout_length, is_shipping_required, redirect_url
):
checkout = Mock(
__len__=Mock(return_value=checkout_length),
is_shipping_required=Mock(return_value=is_shipping_required),
)
monkeypatch.setattr(
"saleor.checkout.utils.get_checkout_from_request", lambda req, qs: checkout
)
url = reverse("checkout:start")
request = rf.get(url, follow=True)
response = views.checkout_start(request)
assert response.url == redirect_url
def test_view_checkout_index_authorized_user(
authorized_client, customer_user, request_checkout_with_item
):
request_checkout_with_item.user = customer_user
request_checkout_with_item.save()
url = reverse("checkout:start")
response = authorized_client.get(url, follow=True)
redirect_url = reverse("checkout:shipping-address")
assert response.request["PATH_INFO"] == redirect_url
def test_view_checkout_shipping_address(client, request_checkout_with_item):
url = reverse("checkout:shipping-address")
data = {
"email": "test@example.com",
"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",
}
response = client.get(url)
assert response.request["PATH_INFO"] == url
response = client.post(url, data, follow=True)
redirect_url = reverse("checkout:shipping-method")
assert response.request["PATH_INFO"] == redirect_url
assert request_checkout_with_item.email == "test@example.com"
def test_view_checkout_shipping_address_with_invalid_data(
client, request_checkout_with_item
):
url = reverse("checkout:shipping-address")
data = {
"email": "test@example.com",
"first_name": "John",
"last_name": "Doe",
"street_address_1": "Aleje Jerozolimskie 2",
"street_address_2": "",
"city": "Warszawa",
"city_area": "",
"country_area": "",
"postal_code": "00-37412",
"phone": "+48536984008",
"country": "PL",
}
response = client.post(url, data, follow=True)
assert response.request["PATH_INFO"] == url
def test_view_checkout_shipping_address_authorized_user(
authorized_client, customer_user, request_checkout_with_item
):
request_checkout_with_item.user = customer_user
request_checkout_with_item.save()
url = reverse("checkout:shipping-address")
data = {"address": customer_user.default_billing_address.pk}
response = authorized_client.post(url, data, follow=True)
redirect_url = reverse("checkout:shipping-method")
assert response.request["PATH_INFO"] == redirect_url
assert request_checkout_with_item.email == customer_user.email
def test_view_checkout_shipping_address_without_shipping(
request_checkout, product_without_shipping, client
):
variant = product_without_shipping.variants.get()
add_variant_to_checkout(request_checkout, variant)
url = reverse("checkout:shipping-address")
response = client.get(url)
assert response.status_code == 302
assert get_redirect_location(response) == reverse("checkout:summary")
assert not request_checkout.email
def test_view_checkout_shipping_method(
client, shipping_zone, address, request_checkout_with_item
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.save()
url = reverse("checkout:shipping-method")
data = {"shipping_method": shipping_zone.shipping_methods.first().pk}
response = client.get(url)
assert response.request["PATH_INFO"] == url
response = client.post(url, data, follow=True)
redirect_url = reverse("checkout:summary")
assert response.request["PATH_INFO"] == redirect_url
def test_view_checkout_shipping_method_authorized_user(
authorized_client, customer_user, shipping_zone, address, request_checkout_with_item
):
request_checkout_with_item.user = customer_user
request_checkout_with_item.email = customer_user.email
request_checkout_with_item.shipping_address = address
request_checkout_with_item.save()
url = reverse("checkout:shipping-method")
data = {"shipping_method": shipping_zone.shipping_methods.first().pk}
response = authorized_client.get(url)
assert response.request["PATH_INFO"] == url
response = authorized_client.post(url, data, follow=True)
redirect_url = reverse("checkout:summary")
assert response.request["PATH_INFO"] == redirect_url
def test_view_checkout_shipping_method_without_shipping(
request_checkout, product_without_shipping, client
):
variant = product_without_shipping.variants.get()
add_variant_to_checkout(request_checkout, variant)
url = reverse("checkout:shipping-method")
response = client.get(url)
assert response.status_code == 302
assert get_redirect_location(response) == reverse("checkout:summary")
def test_view_checkout_shipping_method_without_address(
request_checkout_with_item, client
):
url = reverse("checkout:shipping-method")
response = client.get(url)
assert response.status_code == 302
redirect_url = reverse("checkout:shipping-address")
assert get_redirect_location(response) == redirect_url
@patch("saleor.checkout.utils.send_order_confirmation")
def test_view_checkout_summary_finalize_with_voucher(
mock_send_confirmation,
client,
shipping_zone,
address,
request_checkout_with_item,
voucher,
):
checkout = request_checkout_with_item
expected_voucher_usage_count = voucher.used + 1
checkout.shipping_address = address
checkout.voucher_code = voucher.code
checkout.email = "test@example.com"
checkout.shipping_method = shipping_zone.shipping_methods.first()
checkout.save()
url = reverse("checkout:summary")
data = {"address": "shipping_address"}
response = client.get(url)
assert response.status_code == 200
assert response.request["PATH_INFO"] == url
response = client.post(url, data, follow=True)
assert response.status_code == 200
order = response.context["order"]
assert order.user_email == "test@example.com"
redirect_url = reverse("order:payment", kwargs={"token": order.token})
assert response.request["PATH_INFO"] == redirect_url
# we expect the user to be anonymous, thus None
mock_send_confirmation.delay.assert_called_once_with(order.pk, None)
# checkout should be deleted after order is created
assert checkout.pk is None
# Ensure the voucher was updated
voucher.refresh_from_db(fields=["used"])
assert voucher.used == expected_voucher_usage_count
@patch("saleor.checkout.utils.send_order_confirmation")
def test_view_checkout_summary_anonymous_user(
mock_send_confirmation, client, shipping_zone, address, request_checkout_with_item
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
url = reverse("checkout:summary")
data = {"address": "shipping_address"}
response = client.get(url)
assert response.request["PATH_INFO"] == url
response = client.post(url, data, follow=True)
order = response.context["order"]
assert order.user_email == "test@example.com"
redirect_url = reverse("order:payment", kwargs={"token": order.token})
assert response.request["PATH_INFO"] == redirect_url
# we expect the user to be anonymous, thus None
mock_send_confirmation.delay.assert_called_once_with(order.pk, None)
# checkout should be deleted after order is created
assert request_checkout_with_item.pk is None
@patch("saleor.checkout.utils.send_order_confirmation")
def test_view_checkout_summary_authorized_user(
mock_send_confirmation,
authorized_client,
customer_user,
shipping_zone,
address,
request_checkout_with_item,
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.user = customer_user
request_checkout_with_item.email = customer_user.email
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
url = reverse("checkout:summary")
data = {"address": "shipping_address"}
response = authorized_client.get(url)
assert response.request["PATH_INFO"] == url
response = authorized_client.post(url, data, follow=True)
order = response.context["order"]
assert order.user_email == customer_user.email
redirect_url = reverse("order:payment", kwargs={"token": order.token})
assert response.request["PATH_INFO"] == redirect_url
mock_send_confirmation.delay.assert_called_once_with(order.pk, customer_user.pk)
@patch("saleor.checkout.utils.send_order_confirmation")
def test_view_checkout_summary_save_language(
mock_send_confirmation,
authorized_client,
customer_user,
shipping_zone,
address,
request_checkout_with_item,
settings,
):
settings.LANGUAGE_CODE = "en"
user_language = "fr"
authorized_client.cookies[settings.LANGUAGE_COOKIE_NAME] = user_language
url = reverse("set_language")
data = {"language": "fr"}
authorized_client.post(url, data)
request_checkout_with_item.shipping_address = address
request_checkout_with_item.user = customer_user
request_checkout_with_item.email = customer_user.email
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
url = reverse("checkout:summary")
data = {"address": "shipping_address"}
response = authorized_client.get(url, HTTP_ACCEPT_LANGUAGE=user_language)
assert response.request["PATH_INFO"] == url
response = authorized_client.post(
url, data, follow=True, HTTP_ACCEPT_LANGUAGE=user_language
)
order = response.context["order"]
assert order.user_email == customer_user.email
assert order.language_code == user_language
redirect_url = reverse("order:payment", kwargs={"token": order.token})
assert response.request["PATH_INFO"] == redirect_url
mock_send_confirmation.delay.assert_called_once_with(order.pk, customer_user.pk)
def test_view_checkout_summary_without_address(request_checkout_with_item, client):
url = reverse("checkout:summary")
response = client.get(url)
assert response.status_code == 302
redirect_url = reverse("checkout:shipping-address")
assert get_redirect_location(response) == redirect_url
def test_view_checkout_summary_without_shipping_zone(
request_checkout_with_item, client, address
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.save()
url = reverse("checkout:summary")
response = client.get(url)
assert response.status_code == 302
redirect_url = reverse("checkout:shipping-method")
assert get_redirect_location(response) == redirect_url
def test_view_checkout_summary_with_invalid_voucher(
client, request_checkout_with_item, shipping_zone, address, voucher
):
voucher.usage_limit = 3
voucher.save()
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
url = reverse("checkout:summary")
voucher_url = "{url}?next={url}".format(url=url)
data = {"discount-voucher": voucher.code}
response = client.post(voucher_url, data, follow=True, HTTP_REFERER=url)
assert response.context["checkout"].voucher_code == voucher.code
voucher.used = 3
voucher.save()
data = {"address": "shipping_address"}
response = client.post(url, data, follow=True)
checkout = response.context["checkout"]
assert not checkout.voucher_code
assert not checkout.discount_amount
assert not checkout.discount_name
response = client.post(url, data, follow=True)
order = response.context["order"]
assert not order.voucher
assert not order.discount_amount
assert not order.discount_name
def test_view_checkout_summary_with_invalid_voucher_code(
client, request_checkout_with_item, shipping_zone, address
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
url = reverse("checkout:summary")
voucher_url = "{url}?next={url}".format(url=url)
data = {"discount-voucher": "invalid-code"}
response = client.post(voucher_url, data, follow=True, HTTP_REFERER=url)
assert "voucher" in response.context["voucher_form"].errors
assert response.context["checkout"].voucher_code is None
def test_view_checkout_place_order_with_expired_voucher_code(
client, request_checkout_with_item, shipping_zone, address, voucher
):
checkout = request_checkout_with_item
# add shipping information to the checkout
checkout.shipping_address = address
checkout.email = "test@example.com"
checkout.shipping_method = shipping_zone.shipping_methods.first()
# set voucher to be expired
yesterday = timezone.now() - datetime.timedelta(days=1)
voucher.end_date = yesterday
voucher.save()
# put the voucher code to checkout
checkout.voucher_code = voucher.code
# save the checkout
checkout.save()
checkout_url = reverse("checkout:summary")
# place order
data = {"address": "shipping_address"}
response = client.post(checkout_url, data, follow=True)
# order should not have been placed
assert response.request["PATH_INFO"] == checkout_url
# ensure the voucher was removed
checkout.refresh_from_db()
assert not checkout.voucher_code
def test_view_checkout_place_order_with_item_out_of_stock(
client, request_checkout_with_item, shipping_zone, address, voucher, product
):
checkout = request_checkout_with_item
variant = product.variants.get()
# add shipping information to the checkout
checkout.shipping_address = address
checkout.email = "test@example.com"
checkout.shipping_method = shipping_zone.shipping_methods.first()
checkout.save()
# make the variant be out of stock
variant.quantity = 0
variant.save()
checkout_url = reverse("checkout:summary")
redirect_url = reverse("checkout:index")
# place order
data = {"address": "shipping_address"}
response = client.post(checkout_url, data, follow=True)
# order should have been aborted,
# and user should have been redirected to its checkout
assert response.request["PATH_INFO"] == redirect_url
def test_view_checkout_place_order_without_shipping_address(
client, request_checkout_with_item, shipping_zone
):
checkout = request_checkout_with_item
# add shipping information to the checkout
checkout.email = "test@example.com"
checkout.shipping_method = shipping_zone.shipping_methods.first()
# save the checkout
checkout.save()
checkout_url = reverse("checkout:summary")
redirect_url = reverse("checkout:shipping-address")
# place order
data = {"address": "shipping_address"}
response = client.post(checkout_url, data, follow=True)
# order should have been aborted,
# and user should have been redirected to its checkout
assert response.request["PATH_INFO"] == redirect_url
def test_view_checkout_summary_remove_voucher(
client, request_checkout_with_item, shipping_zone, voucher, address
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
request_checkout_with_item.save()
remove_voucher_url = reverse("checkout:summary")
voucher_url = "{url}?next={url}".format(url=remove_voucher_url)
data = {"discount-voucher": voucher.code}
response = client.post(
voucher_url, data, follow=True, HTTP_REFERER=remove_voucher_url
)
assert response.context["checkout"].voucher_code == voucher.code
url = reverse("checkout:remove-voucher")
response = client.post(url, follow=True, HTTP_REFERER=remove_voucher_url)
assert not response.context["checkout"].voucher_code
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_creates_expected_events(
request_checkout_with_item, customer_user, shipping_method, is_anonymous_user
):
checkout = request_checkout_with_item
checkout_user = None if is_anonymous_user else customer_user
# Ensure not events are existing prior
assert not OrderEvent.objects.exists()
assert not CustomerEvent.objects.exists()
# Prepare valid checkout
checkout.user = checkout_user
checkout.billing_address = customer_user.default_billing_address
checkout.shipping_address = customer_user.default_shipping_address
checkout.shipping_method = shipping_method
checkout.save()
# Place checkout
order = create_order(
checkout=checkout,
order_data=prepare_order_data(
checkout=checkout, tracking_code="tracking_code", discounts=None
),
user=customer_user if not is_anonymous_user else AnonymousUser(),
)
# Ensure only two events were created, and retrieve them
placement_event, email_sent_event = order.events.all() # type: OrderEvent
# Ensure the correct order event was created
assert placement_event.type == OrderEvents.PLACED # is the event the expected type
assert placement_event.user == checkout_user # is the user anonymous/ the customer
assert placement_event.order is order # is the associated backref order valid
assert placement_event.date # ensure a date was set
assert not placement_event.parameters # should not have any additional parameters
# Ensure the correct email sent event was created
assert email_sent_event.type == OrderEvents.EMAIL_SENT # should be email sent event
assert email_sent_event.user == checkout_user # ensure the user is none or valid
assert email_sent_event.order is order # ensure the mail event is related to order
assert email_sent_event.date # ensure a date was set
assert email_sent_event.parameters == { # ensure the correct parameters were set
"email": order.get_customer_email(),
"email_type": OrderEventsEmails.ORDER,
}
# Check no event was created if the user was anonymous
if is_anonymous_user:
assert not CustomerEvent.objects.exists() # should not have created any event
return # we are done testing as the user is anonymous
# Ensure the correct customer event was created if the user was not anonymous
placement_event = customer_user.events.get() # type: CustomerEvent
assert placement_event.type == CustomerEvents.PLACED_ORDER # check the event type
assert placement_event.user == customer_user # check the backref is valid
assert placement_event.order == order # check the associated order is valid
assert placement_event.date # ensure a date was set
assert not placement_event.parameters # should not have any additional parameters
def test_create_order_insufficient_stock(
request_checkout, customer_user, product_without_shipping
):
variant = product_without_shipping.variants.get()
add_variant_to_checkout(request_checkout, variant, 10, check_quantity=False)
request_checkout.user = customer_user
request_checkout.billing_address = customer_user.default_billing_address
request_checkout.shipping_address = customer_user.default_billing_address
request_checkout.save()
with pytest.raises(InsufficientStock):
prepare_order_data(
checkout=request_checkout, tracking_code="tracking_code", discounts=None
)
def test_create_order_doesnt_duplicate_order(
checkout_with_item, customer_user, shipping_method
):
checkout = checkout_with_item
checkout.user = customer_user
checkout.billing_address = customer_user.default_billing_address
checkout.shipping_address = customer_user.default_billing_address
checkout.shipping_method = shipping_method
checkout.save()
order_data = prepare_order_data(checkout=checkout, tracking_code="", discounts=None)
order_1 = create_order(checkout=checkout, order_data=order_data, user=customer_user)
assert order_1.checkout_token == checkout.token
order_2 = create_order(checkout=checkout, order_data=order_data, user=customer_user)
assert order_1.pk == order_2.pk
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_with_gift_card(
checkout_with_gift_card, customer_user, shipping_method, is_anonymous_user
):
checkout_user = None if is_anonymous_user else customer_user
checkout = checkout_with_gift_card
checkout.user = checkout_user
checkout.billing_address = customer_user.default_billing_address
checkout.shipping_address = customer_user.default_billing_address
checkout.shipping_method = shipping_method
checkout.save()
subtotal = checkout.get_subtotal()
shipping_price = checkout.get_shipping_price()
total_gross_without_gift_cards = (
TaxedMoney(subtotal, subtotal)
+ TaxedMoney(shipping_price, shipping_price)
- checkout.discount
).gross
gift_cards_balance = checkout.get_total_gift_cards_balance()
order = create_order(
checkout=checkout,
order_data=prepare_order_data(
checkout=checkout, tracking_code="tracking_code", discounts=None
),
user=customer_user if not is_anonymous_user else AnonymousUser(),
)
assert order.gift_cards.count() == 1
assert order.gift_cards.first().current_balance.amount == 0
assert order.total.gross == (total_gross_without_gift_cards - gift_cards_balance)
def test_create_order_with_gift_card_partial_use(
checkout_with_item, gift_card_used, customer_user, shipping_method
):
checkout = checkout_with_item
checkout.user = customer_user
checkout.billing_address = customer_user.default_billing_address
checkout.shipping_address = customer_user.default_billing_address
checkout.shipping_method = shipping_method
checkout.save()
checkout_total = checkout.get_total()
price_without_gift_card = TaxedMoney(net=checkout_total, gross=checkout_total)
gift_card_balance_before_order = gift_card_used.current_balance_amount
checkout.gift_cards.add(gift_card_used)
checkout.save()
order = create_order(
checkout=checkout,
order_data=prepare_order_data(
checkout=checkout, tracking_code="tracking_code", discounts=None
),
user=customer_user,
)
gift_card_used.refresh_from_db()
expected_old_balance = (
price_without_gift_card.gross.amount + gift_card_used.current_balance_amount
)
assert order.gift_cards.count() > 0
assert order.total == zero_taxed_money()
assert gift_card_balance_before_order == expected_old_balance
def test_create_order_with_many_gift_cards(
checkout_with_item,
gift_card_created_by_staff,
gift_card,
customer_user,
shipping_method,
):
checkout = checkout_with_item
checkout.user = customer_user
checkout.billing_address = customer_user.default_billing_address
checkout.shipping_address = customer_user.default_billing_address
checkout.shipping_method = shipping_method
checkout.save()
checkout_total = checkout.get_total()
price_without_gift_card = TaxedMoney(net=checkout_total, gross=checkout_total)
gift_cards_balance_before_order = (
gift_card_created_by_staff.current_balance.amount
+ gift_card.current_balance.amount
)
checkout.gift_cards.add(gift_card_created_by_staff)
checkout.gift_cards.add(gift_card)
checkout.save()
order = create_order(
checkout=checkout,
order_data=prepare_order_data(
checkout=checkout, tracking_code="tracking_code", discounts=None
),
user=customer_user,
)
gift_card_created_by_staff.refresh_from_db()
gift_card.refresh_from_db()
zero_price = zero_money()
assert order.gift_cards.count() > 0
assert gift_card_created_by_staff.current_balance == zero_price
assert gift_card.current_balance == zero_price
assert price_without_gift_card.gross.amount == (
gift_cards_balance_before_order + order.total.gross.amount
)
def test_note_in_created_order(request_checkout_with_item, address, customer_user):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.note = "test_note"
request_checkout_with_item.save()
order = create_order(
checkout=request_checkout_with_item,
order_data=prepare_order_data(
checkout=request_checkout_with_item,
tracking_code="tracking_code",
discounts=None,
),
user=customer_user,
)
assert order.customer_note == request_checkout_with_item.note
@pytest.mark.parametrize(
"total, min_spent_amount, total_quantity, min_checkout_items_quantity, "
"discount_value, discount_value_type, expected_value",
[
(20, 20, 2, 2, 50, DiscountValueType.PERCENTAGE, 10),
(20, None, 2, None, 50, DiscountValueType.PERCENTAGE, 10),
(20, 20, 2, 2, 5, DiscountValueType.FIXED, 5),
(20, None, 2, None, 5, DiscountValueType.FIXED, 5),
],
)
def test_get_discount_for_checkout_value_voucher(
total,
min_spent_amount,
total_quantity,
min_checkout_items_quantity,
discount_value,
discount_value_type,
expected_value,
):
voucher = Voucher(
code="unique",
type=VoucherType.ENTIRE_ORDER,
discount_value_type=discount_value_type,
discount_value=discount_value,
min_spent=(
Money(min_spent_amount, "USD") if min_spent_amount is not None else None
),
min_checkout_items_quantity=min_checkout_items_quantity,
)
checkout = Mock(
get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity
)
discount = get_voucher_discount_for_checkout(voucher, checkout)
assert discount == Money(expected_value, "USD")
@patch("saleor.discount.utils.validate_voucher")
def test_get_voucher_discount_for_checkout_voucher_validation(
mock_validate_voucher, voucher, checkout_with_voucher
):
get_voucher_discount_for_checkout(voucher, checkout_with_voucher)
manager = get_extensions_manager()
subtotal = manager.calculate_checkout_subtotal(checkout_with_voucher, [])
quantity = checkout_with_voucher.quantity
customer_email = checkout_with_voucher.get_customer_email()
mock_validate_voucher.assert_called_once_with(
voucher, subtotal.gross, quantity, customer_email
)
@pytest.mark.parametrize(
"total, total_quantity, discount_value, discount_type, min_spent_amount, "
"min_checkout_items_quantity",
[
("99", 9, 10, DiscountValueType.FIXED, None, 10),
("99", 9, 10, DiscountValueType.FIXED, 100, None),
("99", 10, 10, DiscountValueType.PERCENTAGE, 100, 10),
("100", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),
("99", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),
],
)
def test_get_discount_for_checkout_entire_order_voucher_not_applicable(
total,
total_quantity,
discount_value,
discount_type,
min_spent_amount,
min_checkout_items_quantity,
):
voucher = Voucher(
code="unique",
type=VoucherType.ENTIRE_ORDER,
discount_value_type=discount_type,
discount_value=discount_value,
min_spent=(
Money(min_spent_amount, "USD") if min_spent_amount is not None else None
),
min_checkout_items_quantity=min_checkout_items_quantity,
)
checkout = Mock(
get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity
)
with pytest.raises(NotApplicable):
get_voucher_discount_for_checkout(voucher, checkout)
@pytest.mark.parametrize(
"discount_value, discount_type, apply_once_per_order, discount_amount",
[
(5, DiscountValueType.FIXED, True, 5),
(5, DiscountValueType.FIXED, False, 15),
(10000, DiscountValueType.FIXED, True, 10),
(10, DiscountValueType.PERCENTAGE, True, 1),
(10, DiscountValueType.PERCENTAGE, False, 5),
],
)
def test_get_discount_for_checkout_specific_products_voucher(
checkout_with_items,
product_list,
discount_value,
discount_type,
apply_once_per_order,
discount_amount,
):
voucher = Voucher.objects.create(
code="unique",
type=VoucherType.SPECIFIC_PRODUCT,
discount_value_type=discount_type,
discount_value=discount_value,
apply_once_per_order=apply_once_per_order,
)
for product in product_list:
voucher.products.add(product)
discount = get_voucher_discount_for_checkout(voucher, checkout_with_items)
assert discount == Money(discount_amount, "USD")
@pytest.mark.parametrize(
"total, total_quantity, discount_value, discount_type, min_spent_amount,"
"min_checkout_items_quantity",
[
("99", 9, 10, DiscountValueType.FIXED, None, 10),
("99", 9, 10, DiscountValueType.FIXED, 100, None),
("99", 10, 10, DiscountValueType.PERCENTAGE, 100, 10),
("100", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),
("99", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),
],
)
def test_get_discount_for_checkout_specific_products_voucher_not_applicable(
monkeypatch,
total,
total_quantity,
discount_value,
discount_type,
min_spent_amount,
min_checkout_items_quantity,
):
discounts = []
monkeypatch.setattr(
"saleor.checkout.utils.get_prices_of_discounted_specific_product",
lambda checkout, discounts, product: [],
)
voucher = Voucher(
code="unique",
type=VoucherType.SPECIFIC_PRODUCT,
discount_value_type=discount_type,
discount_value=discount_value,
min_spent=(
Money(min_spent_amount, "USD") if min_spent_amount is not None else None
),
min_checkout_items_quantity=min_checkout_items_quantity,
)
checkout = Mock(
get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity
)
with pytest.raises(NotApplicable):
get_voucher_discount_for_checkout(voucher, checkout, discounts)
@pytest.mark.parametrize(
"shipping_cost, shipping_country_code, discount_value, discount_type,"
"countries, expected_value",
[
(10, None, 50, DiscountValueType.PERCENTAGE, [], 5),
(10, None, 20, DiscountValueType.FIXED, [], 10),
(10, "PL", 20, DiscountValueType.FIXED, [], 10),
(5, "PL", 5, DiscountValueType.FIXED, ["PL"], 5),
],
)
def test_get_discount_for_checkout_shipping_voucher(
shipping_cost,