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_actions.py
219 lines (166 loc) · 7.05 KB
/
test_order_actions.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
from decimal import Decimal
from unittest.mock import patch
import pytest
from prices import Money, TaxedMoney
from saleor.order import FulfillmentStatus, OrderEvents, OrderEventsEmails, OrderStatus
from saleor.order.actions import (
automatically_fulfill_digital_lines,
cancel_fulfillment,
cancel_order,
clean_mark_order_as_paid,
fulfill_order_line,
handle_fully_paid_order,
mark_order_as_paid,
)
from saleor.order.models import Fulfillment
from saleor.payment import ChargeStatus, PaymentError
from saleor.product.models import DigitalContent
from .utils import create_image
@pytest.fixture
def order_with_digital_line(order, digital_content, variant, site_settings):
site_settings.automatic_fulfillment_digital_products = True
site_settings.save()
variant.digital_content = digital_content
variant.digital_content.save()
product_type = variant.product.product_type
product_type.is_shipping_required = False
product_type.is_digital = True
product_type.save()
net = variant.get_price()
gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
order.lines.create(
product_name=str(variant.product),
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
quantity=3,
variant=variant,
unit_price=TaxedMoney(net=net, gross=gross),
tax_rate=23,
)
return order
@patch("saleor.order.emails.send_fulfillment_confirmation.delay")
@patch("saleor.order.emails.send_payment_confirmation.delay")
def test_handle_fully_paid_order_digital_lines(
mock_send_payment_confirmation,
mock_send_fulfillment_confirmation,
order_with_digital_line,
):
order = order_with_digital_line
handle_fully_paid_order(order)
fulfillment = order.fulfillments.first()
event_order_paid, event_email_sent, event_order_fulfilled, event_digital_links = (
order.events.all()
)
assert event_order_paid.type == OrderEvents.ORDER_FULLY_PAID
assert event_email_sent.type == OrderEvents.EMAIL_SENT
assert event_order_fulfilled.type == OrderEvents.EMAIL_SENT
assert event_digital_links.type == OrderEvents.EMAIL_SENT
assert (
event_order_fulfilled.parameters["email_type"] == OrderEventsEmails.FULFILLMENT
)
assert (
event_digital_links.parameters["email_type"] == OrderEventsEmails.DIGITAL_LINKS
)
mock_send_payment_confirmation.assert_called_once_with(order.pk)
mock_send_fulfillment_confirmation.assert_called_once_with(order.pk, fulfillment.pk)
order.refresh_from_db()
assert order.status == OrderStatus.FULFILLED
@patch("saleor.order.emails.send_payment_confirmation.delay")
def test_handle_fully_paid_order(mock_send_payment_confirmation, order):
handle_fully_paid_order(order)
event_order_paid, event_email_sent = order.events.all()
assert event_order_paid.type == OrderEvents.ORDER_FULLY_PAID
assert event_email_sent.type == OrderEvents.EMAIL_SENT
assert event_email_sent.parameters == {
"email": order.get_customer_email(),
"email_type": OrderEventsEmails.PAYMENT,
}
mock_send_payment_confirmation.assert_called_once_with(order.pk)
@patch("saleor.order.emails.send_payment_confirmation.delay")
def test_handle_fully_paid_order_no_email(mock_send_payment_confirmation, order):
order.user = None
order.user_email = ""
handle_fully_paid_order(order)
event = order.events.get()
assert event.type == OrderEvents.ORDER_FULLY_PAID
assert not mock_send_payment_confirmation.called
def test_mark_as_paid(admin_user, draft_order):
mark_order_as_paid(draft_order, admin_user)
payment = draft_order.payments.last()
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.captured_amount == draft_order.total.gross.amount
assert draft_order.events.last().type == (OrderEvents.ORDER_MARKED_AS_PAID)
def test_clean_mark_order_as_paid(payment_txn_preauth):
order = payment_txn_preauth.order
with pytest.raises(PaymentError):
clean_mark_order_as_paid(order)
def test_cancel_fulfillment(fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
line_1 = fulfillment.lines.first()
line_2 = fulfillment.lines.first()
cancel_fulfillment(fulfillment, None, restock=False)
assert fulfillment.status == FulfillmentStatus.CANCELED
assert fulfilled_order.status == OrderStatus.UNFULFILLED
assert line_1.order_line.quantity_fulfilled == 0
assert line_2.order_line.quantity_fulfilled == 0
def test_cancel_order(fulfilled_order):
cancel_order(fulfilled_order, None, restock=False)
assert all(
[
f.status == FulfillmentStatus.CANCELED
for f in fulfilled_order.fulfillments.all()
]
)
assert fulfilled_order.status == OrderStatus.CANCELED
def test_fulfill_order_line(order_with_lines):
order = order_with_lines
line = order.lines.first()
quantity_fulfilled_before = line.quantity_fulfilled
variant = line.variant
stock_quantity_after = variant.quantity - line.quantity
fulfill_order_line(line, line.quantity)
variant.refresh_from_db()
assert variant.quantity == stock_quantity_after
assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
def test_fulfill_order_line_with_variant_deleted(order_with_lines):
line = order_with_lines.lines.first()
line.variant.delete()
line.refresh_from_db()
fulfill_order_line(line, line.quantity)
def test_fulfill_order_line_without_inventory_tracking(order_with_lines):
order = order_with_lines
line = order.lines.first()
quantity_fulfilled_before = line.quantity_fulfilled
variant = line.variant
variant.track_inventory = False
variant.save()
# stock should not change
stock_quantity_after = variant.quantity
fulfill_order_line(line, line.quantity)
variant.refresh_from_db()
assert variant.quantity == stock_quantity_after
assert line.quantity_fulfilled == quantity_fulfilled_before + line.quantity
@patch("saleor.order.actions.emails.send_fulfillment_confirmation")
@patch("saleor.order.utils.get_default_digital_content_settings")
def test_fulfill_digital_lines(
mock_digital_settings, mock_email_fulfillment, order_with_lines, media_root
):
mock_digital_settings.return_value = {"automatic_fulfillment": True}
line = order_with_lines.lines.all()[0]
image_file, image_name = create_image()
variant = line.variant
digital_content = DigitalContent.objects.create(
content_file=image_file, product_variant=variant, use_default_settings=True
)
line.variant.digital_content = digital_content
line.is_shipping_required = False
line.save()
order_with_lines.refresh_from_db()
automatically_fulfill_digital_lines(order_with_lines)
line.refresh_from_db()
fulfillment = Fulfillment.objects.get(order=order_with_lines)
fulfillment_lines = fulfillment.lines.all()
assert fulfillment_lines.count() == 1
assert line.digital_content_url
assert mock_email_fulfillment.delay.called