-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_emails.py
224 lines (175 loc) · 7.58 KB
/
test_emails.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
from unittest import mock
import pytest
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.templatetags.static import static
from templated_email import get_connection
import saleor.account.emails as account_emails
import saleor.order.emails as emails
from saleor.core.emails import get_email_context
from saleor.core.utils import build_absolute_uri
from saleor.order.utils import add_variant_to_order
def test_get_email_context(site_settings):
site = site_settings.site
logo_url = build_absolute_uri(static("images/logo-light.svg"))
expected_send_kwargs = {"from_email": site_settings.default_from_email}
proper_context = {
"domain": site.domain,
"logo_url": logo_url,
"site_name": site.name,
}
send_kwargs, received_context = get_email_context()
assert send_kwargs == expected_send_kwargs
assert proper_context == received_context
def test_collect_data_for_order_confirmation_email(order):
"""Order confirmation email requires extra data, which should be present
in email's context.
"""
template = emails.CONFIRM_ORDER_TEMPLATE
email_data = emails.collect_data_for_email(order.pk, template)
email_context = email_data["context"]
assert email_context["order"] == order
assert "schema_markup" in email_context
def test_collect_data_for_fullfillment_email(fulfilled_order):
template = emails.CONFIRM_FULFILLMENT_TEMPLATE
fulfillment = fulfilled_order.fulfillments.first()
fulfillment_data = emails.collect_data_for_fullfillment_email(
fulfilled_order.pk, template, fulfillment.pk
)
email_context = fulfillment_data["context"]
assert email_context["fulfillment"] == fulfillment
email_data = emails.collect_data_for_email(fulfilled_order.pk, template)
assert all([key in email_context for key, item in email_data["context"].items()])
def test_collect_data_for_email(order):
template = emails.CONFIRM_PAYMENT_TEMPLATE
email_data = emails.collect_data_for_email(order.pk, template)
email_context = email_data["context"]
# This properties should be present only for order confirmation email
assert "schema_markup" not in email_context
@pytest.mark.parametrize(
"send_email,template",
[
(emails.send_payment_confirmation, emails.CONFIRM_PAYMENT_TEMPLATE),
(emails.send_order_confirmation, emails.CONFIRM_ORDER_TEMPLATE),
],
)
@mock.patch("saleor.order.emails.send_templated_mail")
def test_send_emails(
mocked_templated_email, order, template, send_email, site_settings
):
send_email(order.pk)
email_data = emails.collect_data_for_email(order.pk, template)
recipients = [order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
"from_email": site_settings.default_from_email,
"template_name": template,
}
mocked_templated_email.assert_called_once_with(
recipient_list=recipients, **expected_call_kwargs
)
# Render the email to ensure there is no error
email_connection = get_connection()
email_connection.get_email_message(to=recipients, **expected_call_kwargs)
@pytest.mark.parametrize(
"send_email,template",
[
(emails.send_payment_confirmation, emails.CONFIRM_PAYMENT_TEMPLATE),
(emails.send_order_confirmation, emails.CONFIRM_ORDER_TEMPLATE),
],
)
@mock.patch("saleor.order.emails.send_templated_mail")
def test_send_confirmation_emails_without_addresses(
mocked_templated_email, order, template, send_email, site_settings, digital_content
):
assert not order.lines.count()
add_variant_to_order(order, digital_content.product_variant, quantity=1)
order.shipping_address = None
order.shipping_method = None
order.billing_address = None
order.save(update_fields=["shipping_address", "shipping_method", "billing_address"])
send_email(order.pk)
email_data = emails.collect_data_for_email(order.pk, template)
recipients = [order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
"from_email": site_settings.default_from_email,
"template_name": template,
}
mocked_templated_email.assert_called_once_with(
recipient_list=recipients, **expected_call_kwargs
)
# Render the email to ensure there is no error
email_connection = get_connection()
email_connection.get_email_message(to=recipients, **expected_call_kwargs)
@pytest.mark.parametrize(
"send_email,template",
[
(
emails.send_fulfillment_confirmation,
emails.CONFIRM_FULFILLMENT_TEMPLATE,
), # noqa
(emails.send_fulfillment_update, emails.UPDATE_FULFILLMENT_TEMPLATE),
],
)
@mock.patch("saleor.order.emails.send_templated_mail")
def test_send_fulfillment_emails(
mocked_templated_email, template, send_email, fulfilled_order, site_settings
):
fulfillment = fulfilled_order.fulfillments.first()
send_email(order_pk=fulfilled_order.pk, fulfillment_pk=fulfillment.pk)
email_data = emails.collect_data_for_fullfillment_email(
fulfilled_order.pk, template, fulfillment.pk
)
recipients = [fulfilled_order.get_customer_email()]
expected_call_kwargs = {
"context": email_data["context"],
"from_email": site_settings.default_from_email,
"template_name": template,
}
mocked_templated_email.assert_called_once_with(
recipient_list=recipients, **expected_call_kwargs
)
# Render the email to ensure there is no error
email_connection = get_connection()
email_connection.get_email_message(to=recipients, **expected_call_kwargs)
def test_email_having_display_name_in_settings(customer_user, site_settings, settings):
expected_from_email = "Info <hello@mirumee.com>"
site_settings.default_mail_sender_name = None
site_settings.default_mail_sender_address = None
settings.DEFAULT_FROM_EMAIL = expected_from_email
assert site_settings.default_from_email == expected_from_email
def test_send_dummy_email_with_utf_8(customer_user, site_settings):
site_settings.default_mail_sender_address = "hello@example.com"
site_settings.default_mail_sender_name = "徐 欣"
site_settings.save(
update_fields=["default_mail_sender_address", "default_mail_sender_name"]
)
account_emails.send_account_delete_confirmation_email(customer_user)
assert len(mail.outbox) > 0
message: mail.EmailMessage = mail.outbox[-1]
assert message.from_email == "徐 欣 <hello@example.com>"
assert message.extra_headers == {}
@pytest.mark.parametrize(
"sender_name, sender_address",
(("徐 欣", "hello@example.com\nOopsie: Hello"), ("徐\n欣", "hello@example.com")),
)
def test_send_dummy_email_with_header_injection(
customer_user, site_settings, sender_name, sender_address
):
site_settings.default_mail_sender_address = sender_name
site_settings.default_mail_sender_name = sender_address
site_settings.save(
update_fields=["default_mail_sender_address", "default_mail_sender_name"]
)
account_emails.send_account_delete_confirmation_email(customer_user)
assert len(mail.outbox) == 0
def test_email_with_email_not_configured_raises_error(settings, site_settings):
"""Ensure an exception is thrown when not default sender is set;
both missing in the settings.py and in the site settings table.
"""
site_settings.default_mail_sender_address = None
settings.DEFAULT_FROM_EMAIL = None
with pytest.raises(ImproperlyConfigured) as exc:
_ = site_settings.default_from_email
assert exc.value.args == ("No sender email address has been set-up",)