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_order.py
655 lines (508 loc) · 23.1 KB
/
test_order.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
from decimal import Decimal
from unittest.mock import MagicMock, patch
import pytest
from django.urls import reverse
from prices import Money, TaxedMoney
from saleor.account import events as account_events
from saleor.account.models import User
from saleor.checkout.utils import create_order, prepare_order_data
from saleor.core.exceptions import InsufficientStock
from saleor.core.weight import zero_weight
from saleor.discount.utils import validate_voucher_in_order
from saleor.order import OrderStatus, events as order_events, models
from saleor.order.models import Order
from saleor.order.templatetags.order_lines import display_translated_order_line_name
from saleor.order.utils import (
add_variant_to_order,
change_order_line_quantity,
delete_order_line,
recalculate_order,
restock_fulfillment_lines,
restock_order_lines,
update_order_prices,
update_order_status,
)
from saleor.payment import ChargeStatus
from saleor.payment.models import Payment
from .utils import get_redirect_location
def test_total_setter():
price = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))
order = models.Order()
order.total = price
assert order.total_net_amount == Decimal(10)
assert order.total.net == Money(10, "USD")
assert order.total_gross_amount == Decimal(15)
assert order.total.gross == Money(15, "USD")
assert order.total.tax == Money(5, "USD")
def test_order_get_subtotal(order_with_lines):
order_with_lines.discount_name = "Test discount"
order_with_lines.discount = order_with_lines.total.gross * Decimal("0.5")
recalculate_order(order_with_lines)
target_subtotal = order_with_lines.total - order_with_lines.shipping_price
target_subtotal += order_with_lines.discount
assert order_with_lines.get_subtotal() == target_subtotal
def test_add_variant_to_order_adds_line_for_new_variant(
order_with_lines, product, product_translation_fr, settings
):
order = order_with_lines
variant = product.variants.get()
lines_before = order.lines.count()
settings.LANGUAGE_CODE = "fr"
add_variant_to_order(order, variant, 1)
line = order.lines.last()
assert order.lines.count() == lines_before + 1
assert line.product_sku == variant.sku
assert line.quantity == 1
assert line.unit_price == TaxedMoney(net=Money(10, "USD"), gross=Money(10, "USD"))
assert line.translated_product_name == str(variant.product.translated)
assert line.variant_name == str(variant)
assert line.product_name == str(variant.product)
@pytest.mark.parametrize("track_inventory", (True, False))
def test_add_variant_to_order_allocates_stock_for_new_variant(
order_with_lines, product, track_inventory
):
variant = product.variants.get()
variant.track_inventory = track_inventory
variant.save()
stock_before = variant.quantity_allocated
add_variant_to_order(order_with_lines, variant, 1)
variant.refresh_from_db()
if track_inventory:
assert variant.quantity_allocated == stock_before + 1
else:
assert variant.quantity_allocated == stock_before
def test_add_variant_to_order_edits_line_for_existing_variant(order_with_lines):
existing_line = order_with_lines.lines.first()
variant = existing_line.variant
lines_before = order_with_lines.lines.count()
line_quantity_before = existing_line.quantity
add_variant_to_order(order_with_lines, variant, 1)
existing_line.refresh_from_db()
assert order_with_lines.lines.count() == lines_before
assert existing_line.product_sku == variant.sku
assert existing_line.quantity == line_quantity_before + 1
def test_add_variant_to_order_allocates_stock_for_existing_variant(order_with_lines):
existing_line = order_with_lines.lines.first()
variant = existing_line.variant
stock_before = variant.quantity_allocated
add_variant_to_order(order_with_lines, variant, 1)
variant.refresh_from_db()
assert variant.quantity_allocated == stock_before + 1
def test_add_variant_to_order_allow_overselling(order_with_lines):
existing_line = order_with_lines.lines.first()
variant = existing_line.variant
stock_before = variant.quantity_allocated
quantity = variant.quantity + 1
with pytest.raises(InsufficientStock):
add_variant_to_order(
order_with_lines, variant, quantity, allow_overselling=False
)
add_variant_to_order(order_with_lines, variant, quantity, allow_overselling=True)
variant.refresh_from_db()
assert variant.quantity_allocated == stock_before + quantity
def test_view_connect_order_with_user_authorized_user(
order, authorized_client, customer_user
):
order.user_email = customer_user.email
order.save()
url = reverse("order:connect-order-with-user", kwargs={"token": order.token})
response = authorized_client.post(url)
redirect_location = get_redirect_location(response)
assert redirect_location == reverse("order:details", args=[order.token])
order.refresh_from_db()
assert order.user == customer_user
def test_view_connect_order_with_user_different_email(
order, authorized_client, customer_user
):
"""Order was placed from different email, than user's
we are trying to assign it to."""
order.user = None
order.user_email = "example_email@email.email"
order.save()
assert order.user_email != customer_user.email
url = reverse("order:connect-order-with-user", kwargs={"token": order.token})
response = authorized_client.post(url)
redirect_location = get_redirect_location(response)
assert redirect_location == reverse("account:details")
order.refresh_from_db()
assert order.user is None
def test_view_order_with_deleted_variant(authorized_client, order_with_lines):
order = order_with_lines
order_details_url = reverse("order:details", kwargs={"token": order.token})
# delete a variant associated to the order
order.lines.first().variant.delete()
# check if the order details view handles the deleted variant
response = authorized_client.get(order_details_url)
assert response.status_code == 200
def test_view_fulfilled_order_with_deleted_variant(authorized_client, fulfilled_order):
order = fulfilled_order
order_details_url = reverse("order:details", kwargs={"token": order.token})
# delete a variant associated to the order
order.lines.first().variant.delete()
# check if the order details view handles the deleted variant
response = authorized_client.get(order_details_url)
assert response.status_code == 200
@pytest.mark.parametrize("track_inventory", (True, False))
def test_restock_order_lines(order_with_lines, track_inventory):
order = order_with_lines
line_1 = order.lines.first()
line_2 = order.lines.last()
line_1.variant.track_inventory = track_inventory
line_2.variant.track_inventory = track_inventory
line_1.variant.save()
line_2.variant.save()
stock_1_quantity_allocated_before = line_1.variant.quantity_allocated
stock_2_quantity_allocated_before = line_2.variant.quantity_allocated
stock_1_quantity_before = line_1.variant.quantity
stock_2_quantity_before = line_2.variant.quantity
restock_order_lines(order)
line_1.variant.refresh_from_db()
line_2.variant.refresh_from_db()
if track_inventory:
assert line_1.variant.quantity_allocated == (
stock_1_quantity_allocated_before - line_1.quantity
)
assert line_2.variant.quantity_allocated == (
stock_2_quantity_allocated_before - line_2.quantity
)
else:
assert line_1.variant.quantity_allocated == (stock_1_quantity_allocated_before)
assert line_2.variant.quantity_allocated == (stock_2_quantity_allocated_before)
assert line_1.variant.quantity == stock_1_quantity_before
assert line_2.variant.quantity == stock_2_quantity_before
assert line_1.quantity_fulfilled == 0
assert line_2.quantity_fulfilled == 0
def test_restock_fulfilled_order_lines(fulfilled_order):
line_1 = fulfilled_order.lines.first()
line_2 = fulfilled_order.lines.last()
stock_1_quantity_allocated_before = line_1.variant.quantity_allocated
stock_2_quantity_allocated_before = line_2.variant.quantity_allocated
stock_1_quantity_before = line_1.variant.quantity
stock_2_quantity_before = line_2.variant.quantity
restock_order_lines(fulfilled_order)
line_1.variant.refresh_from_db()
line_2.variant.refresh_from_db()
assert line_1.variant.quantity_allocated == stock_1_quantity_allocated_before
assert line_2.variant.quantity_allocated == stock_2_quantity_allocated_before
assert line_1.variant.quantity == stock_1_quantity_before + line_1.quantity
assert line_2.variant.quantity == stock_2_quantity_before + line_2.quantity
def test_restock_fulfillment_lines(fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
line_1 = fulfillment.lines.first()
line_2 = fulfillment.lines.last()
stock_1 = line_1.order_line.variant
stock_2 = line_2.order_line.variant
stock_1_quantity_allocated_before = stock_1.quantity_allocated
stock_2_quantity_allocated_before = stock_2.quantity_allocated
stock_1_quantity_before = stock_1.quantity
stock_2_quantity_before = stock_2.quantity
restock_fulfillment_lines(fulfillment)
stock_1.refresh_from_db()
stock_2.refresh_from_db()
assert stock_1.quantity_allocated == (
stock_1_quantity_allocated_before + line_1.quantity
)
assert stock_2.quantity_allocated == (
stock_2_quantity_allocated_before + line_2.quantity
)
assert stock_1.quantity == stock_1_quantity_before + line_1.quantity
assert stock_2.quantity == stock_2_quantity_before + line_2.quantity
def test_update_order_status(fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
line = fulfillment.lines.first()
order_line = line.order_line
order_line.quantity_fulfilled -= line.quantity
order_line.save()
line.delete()
update_order_status(fulfilled_order)
assert fulfilled_order.status == OrderStatus.PARTIALLY_FULFILLED
line = fulfillment.lines.first()
order_line = line.order_line
order_line.quantity_fulfilled -= line.quantity
order_line.save()
line.delete()
update_order_status(fulfilled_order)
assert fulfilled_order.status == OrderStatus.UNFULFILLED
def test_order_queryset_confirmed(draft_order):
other_orders = [
Order.objects.create(status=OrderStatus.UNFULFILLED),
Order.objects.create(status=OrderStatus.PARTIALLY_FULFILLED),
Order.objects.create(status=OrderStatus.FULFILLED),
Order.objects.create(status=OrderStatus.CANCELED),
]
confirmed_orders = Order.objects.confirmed()
assert draft_order not in confirmed_orders
assert all([order in confirmed_orders for order in other_orders])
def test_order_queryset_drafts(draft_order):
other_orders = [
Order.objects.create(status=OrderStatus.UNFULFILLED),
Order.objects.create(status=OrderStatus.PARTIALLY_FULFILLED),
Order.objects.create(status=OrderStatus.FULFILLED),
Order.objects.create(status=OrderStatus.CANCELED),
]
draft_orders = Order.objects.drafts()
assert draft_order in draft_orders
assert all([order not in draft_orders for order in other_orders])
def test_order_queryset_to_ship(settings):
total = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))
orders_to_ship = [
Order.objects.create(status=OrderStatus.UNFULFILLED, total=total),
Order.objects.create(status=OrderStatus.PARTIALLY_FULFILLED, total=total),
]
for order in orders_to_ship:
order.payments.create(
gateway="Dummy",
charge_status=ChargeStatus.FULLY_CHARGED,
total=order.total.gross.amount,
captured_amount=order.total.gross.amount,
currency=order.total.gross.currency,
)
orders_not_to_ship = [
Order.objects.create(status=OrderStatus.DRAFT, total=total),
Order.objects.create(status=OrderStatus.UNFULFILLED, total=total),
Order.objects.create(status=OrderStatus.PARTIALLY_FULFILLED, total=total),
Order.objects.create(status=OrderStatus.FULFILLED, total=total),
Order.objects.create(status=OrderStatus.CANCELED, total=total),
]
orders = Order.objects.ready_to_fulfill()
assert all([order in orders for order in orders_to_ship])
assert all([order not in orders for order in orders_not_to_ship])
def test_queryset_ready_to_capture():
total = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))
preauth_order = Order.objects.create(status=OrderStatus.UNFULFILLED, total=total)
Payment.objects.create(
order=preauth_order, charge_status=ChargeStatus.NOT_CHARGED, is_active=True
)
Order.objects.create(status=OrderStatus.DRAFT, total=total)
Order.objects.create(status=OrderStatus.UNFULFILLED, total=total)
Order.objects.create(status=OrderStatus.CANCELED, total=total)
qs = Order.objects.ready_to_capture()
assert preauth_order in qs
statuses = [o.status for o in qs]
assert OrderStatus.DRAFT not in statuses
assert OrderStatus.CANCELED not in statuses
def test_update_order_prices(order_with_lines):
address = order_with_lines.shipping_address
address.country = "DE"
address.save()
line_1 = order_with_lines.lines.first()
line_2 = order_with_lines.lines.last()
price_1 = line_1.variant.get_price()
price_1 = TaxedMoney(net=price_1, gross=price_1)
price_2 = line_2.variant.get_price()
price_2 = TaxedMoney(net=price_2, gross=price_2)
shipping_price = order_with_lines.shipping_method.get_total()
shipping_price = TaxedMoney(net=shipping_price, gross=shipping_price)
update_order_prices(order_with_lines, None)
line_1.refresh_from_db()
line_2.refresh_from_db()
assert line_1.unit_price == price_1
assert line_2.unit_price == price_2
assert order_with_lines.shipping_price == shipping_price
total = line_1.quantity * price_1 + line_2.quantity * price_2 + shipping_price
assert order_with_lines.total == total
def test_order_payment_flow(
request_checkout_with_item, client, address, customer_user, shipping_zone
):
request_checkout_with_item.shipping_address = address
request_checkout_with_item.billing_address = address.get_copy()
request_checkout_with_item.email = "test@example.com"
request_checkout_with_item.shipping_method = shipping_zone.shipping_methods.first()
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,
)
gateway = "Dummy"
# Select payment
url = reverse("order:payment", kwargs={"token": order.token})
data = {"gateway": gateway}
response = client.post(url, data, follow=True)
assert len(response.redirect_chain) == 1
assert response.status_code == 200
redirect_url = reverse(
"order:payment", kwargs={"token": order.token, "gateway": gateway}
)
assert response.request["PATH_INFO"] == redirect_url
# Go to payment details page, enter payment data
data = {
"gateway": gateway,
"is_active": True,
"total": order.total.gross.amount,
"currency": order.total.gross.currency,
"charge_status": ChargeStatus.FULLY_CHARGED,
}
response = client.post(redirect_url, data)
assert response.status_code == 302
redirect_url = reverse("order:payment-success", kwargs={"token": order.token})
assert get_redirect_location(response) == redirect_url
# Assert that payment object was created and contains correct data
payment = order.payments.all()[0]
assert payment.total == order.total.gross.amount
assert payment.currency == order.total.gross.currency
assert payment.transactions.count() == 1
assert payment.transactions.last().kind == "capture"
def test_create_user_after_order(order, client):
order.user_email = "hello@mirumee.com"
order.save()
url = reverse("order:checkout-success", kwargs={"token": order.token})
data = {"password": "password"}
response = client.post(url, data)
redirect_url = reverse("order:details", kwargs={"token": order.token})
assert get_redirect_location(response) == redirect_url
user = User.objects.filter(email="hello@mirumee.com").first()
assert user is not None
assert user.orders.filter(token=order.token).exists()
def test_view_order_details(order, client):
url = reverse("order:details", kwargs={"token": order.token})
response = client.get(url)
assert response.status_code == 200
def test_add_order_note_view(order, authorized_client, customer_user):
order.user_email = customer_user.email
order.save()
url = reverse("order:details", kwargs={"token": order.token})
customer_note = "bla-bla note"
data = {"customer_note": customer_note}
response = authorized_client.post(url, data)
redirect_url = reverse("order:details", kwargs={"token": order.token})
assert get_redirect_location(response) == redirect_url
order.refresh_from_db()
assert order.customer_note == customer_note
# Ensure an order event was triggered
note_event = order_events.OrderEvent.objects.get() # type: order_events.OrderEvent
assert note_event.type == order_events.OrderEvents.NOTE_ADDED
assert note_event.user == customer_user
assert note_event.order == order
assert note_event.parameters == {"message": customer_note}
# Ensure a customer event was triggered
note_event = account_events.CustomerEvent.objects.get()
assert note_event.type == account_events.CustomerEvents.NOTE_ADDED_TO_ORDER
assert note_event.user == customer_user
assert note_event.order == order
assert note_event.parameters == {"message": customer_note}
def test_add_order_note_view_anonymous_order(order, authorized_client, customer_user):
order.user_email = customer_user.email
order.user = None
order.save(update_fields=["user_email", "user"])
url = reverse("order:details", kwargs={"token": order.token})
customer_note = "bla-bla note"
data = {"customer_note": customer_note}
response = authorized_client.post(url, data)
assert response.status_code == 302
# Ensure an order event was triggered
note_event = order_events.OrderEvent.objects.last() # type: order_events.OrderEvent
assert note_event.type == order_events.OrderEvents.NOTE_ADDED
assert note_event.user == customer_user
assert note_event.order == order
assert note_event.parameters == {"message": customer_note}
# Ensure a customer event was not triggered because the order has no user
assert not account_events.CustomerEvent.objects.exists()
def test_anonymously_add_order_note_view_anonymous_order(order, client, customer_user):
order.user_email = customer_user.email
order.user = None
order.save(update_fields=["user_email", "user"])
url = reverse("order:details", kwargs={"token": order.token})
customer_note = "bla-bla note"
data = {"customer_note": customer_note}
response = client.post(url, data)
assert response.status_code == 302
# Ensure an order event was triggered
note_event = order_events.OrderEvent.objects.last() # type: order_events.OrderEvent
assert note_event.type == order_events.OrderEvents.NOTE_ADDED
assert note_event.user is None
assert note_event.order == order
assert note_event.parameters == {"message": customer_note}
# Ensure a customer event was not triggered because the order has no user
assert not account_events.CustomerEvent.objects.exists()
def _calculate_order_weight_from_lines(order):
weight = zero_weight()
for line in order:
weight += line.variant.get_weight() * line.quantity
return weight
def test_calculate_order_weight(order_with_lines):
order_weight = order_with_lines.weight
calculated_weight = _calculate_order_weight_from_lines(order_with_lines)
assert calculated_weight == order_weight
def test_order_weight_add_more_variant(order_with_lines):
variant = order_with_lines.lines.first().variant
add_variant_to_order(order_with_lines, variant, 2)
order_with_lines.refresh_from_db()
assert order_with_lines.weight == _calculate_order_weight_from_lines(
order_with_lines
)
def test_order_weight_add_new_variant(order_with_lines, product):
variant = product.variants.first()
add_variant_to_order(order_with_lines, variant, 2)
order_with_lines.refresh_from_db()
assert order_with_lines.weight == _calculate_order_weight_from_lines(
order_with_lines
)
def test_order_weight_change_line_quantity(order_with_lines):
line = order_with_lines.lines.first()
new_quantity = line.quantity + 2
change_order_line_quantity(None, line, new_quantity, line.quantity)
order_with_lines.refresh_from_db()
assert order_with_lines.weight == _calculate_order_weight_from_lines(
order_with_lines
)
def test_order_weight_delete_line(order_with_lines):
line = order_with_lines.lines.first()
delete_order_line(line)
assert order_with_lines.weight == _calculate_order_weight_from_lines(
order_with_lines
)
def test_get_order_weight_non_existing_product(order_with_lines, product):
# Removing product should not affect order's weight
order = order_with_lines
variant = product.variants.first()
add_variant_to_order(order, variant, 1)
old_weight = order.get_total_weight()
product.delete()
order.refresh_from_db()
new_weight = order.get_total_weight()
assert old_weight == new_weight
@patch("saleor.discount.utils.validate_voucher")
def test_get_voucher_discount_for_order_voucher_validation(
mock_validate_voucher, voucher, order_with_lines
):
order_with_lines.voucher = voucher
order_with_lines.save()
subtotal = order_with_lines.get_subtotal()
quantity = order_with_lines.get_total_quantity()
customer_email = order_with_lines.get_customer_email()
validate_voucher_in_order(order_with_lines)
mock_validate_voucher.assert_called_once_with(
voucher, subtotal.gross, quantity, customer_email
)
@pytest.mark.parametrize(
"product_name, variant_name, translated_product_name, translated_variant_name,"
"expected_display_name",
[
("product", "variant", "", "", "product (variant)"),
("product", "", "", "", "product"),
("product", "", "productPL", "", "productPL"),
("product", "variant", "productPL", "", "productPL (variant)"),
("product", "variant", "productPL", "variantPl", "productPL (variantPl)"),
("product", "variant", "", "variantPl", "product (variantPl)"),
],
)
def test_display_translated_order_line_name(
product_name,
variant_name,
translated_product_name,
translated_variant_name,
expected_display_name,
):
order_line = MagicMock(
product_name=product_name,
variant_name=variant_name,
translated_product_name=translated_product_name,
translated_variant_name=translated_variant_name,
)
display_name = display_translated_order_line_name(order_line)
assert display_name == expected_display_name