Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Rework emails.py file and its tests

Pacu26a00b2cf4233
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index 06518b5864..c0cb10827a 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -15,37 +15,58 @@ CONFIRM_PAYMENT_TEMPLATE = 'source/order/payment/confirm_payment'
CONFIRM_NOTE_TEMPLATE = 'source/order/note/confirm_note'
-def _send_confirmation(email, url, template, context=None):
+def get_email_context(order_token):
+ """Prepares context required for email template rendering."""
site = Site.objects.get_current()
+ order_url = build_absolute_uri(
+ reverse('order:details', kwargs={'token': order_token}))
ctx = {
'protocol': 'https' if settings.ENABLE_SSL else 'http',
'site_name': site.name,
'domain': site.domain,
- 'url': url}
- if context:
- ctx.update(context)
- send_templated_mail(
- from_email=settings.ORDER_FROM_EMAIL,
- recipient_list=[email],
- context=ctx,
- template_name=template)
+ 'url': order_url}
+ return ctx
def collect_data_for_email(order_pk, template):
+ """Collects data required for email sending.
+
+ Args:
+ order_pk (int): order primary key
+ template (str): email template path
+ """
order = Order.objects.get(pk=order_pk)
- email = order.get_user_current_email()
- url = build_absolute_uri(
- reverse('order:details', kwargs={'token': order.token}))
- return {'email': email, 'url': url, 'template': template}
+ recipient_email = order.get_user_current_email()
+ email_context = get_email_context(order.token)
+
+ # Order confirmation template requires additional information
+ if template == CONFIRM_ORDER_TEMPLATE:
+ email_markup = get_order_confirmation_markup(order)
+ email_context.update({'order': order, 'schema_markup': email_markup})
+ return {
+ 'recipient_email': recipient_email, 'template': template,
+ 'context': email_context}
+
+
+def _send_confirmation(recipient_email, template, context):
+ """Sends confirmation email
+
+ Args:
+ recipient_email (str): recipient email
+ template (str): email template path
+ context (dict): context required by email's template
+ """
+ send_templated_mail(
+ from_email=settings.ORDER_FROM_EMAIL,
+ recipient_list=[recipient_email],
+ context=context,
+ template_name=template)
@shared_task
def send_order_confirmation(order_pk):
- order = Order.objects.get(pk=order_pk)
- email_markup = get_order_confirmation_markup(order)
- context = {'order': order, 'schema_markup': email_markup}
+ """Sends order confirmation email."""
email_data = collect_data_for_email(order_pk, CONFIRM_ORDER_TEMPLATE)
- email_data.update({'context': context})
_send_confirmation(**email_data)
@@ -67,11 +88,13 @@ def send_fulfillment_update(order_pk, fulfillment_pk):
@shared_task
def send_payment_confirmation(order_pk):
+ """Sends payment confirmation email."""
email_data = collect_data_for_email(order_pk, CONFIRM_PAYMENT_TEMPLATE)
_send_confirmation(**email_data)
@shared_task
def send_note_confirmation(order_pk):
+ """Notifies customer, when new note was added to an order."""
email_data = collect_data_for_email(order_pk, CONFIRM_NOTE_TEMPLATE)
_send_confirmation(**email_data)
diff --git a/tests/conftest.py b/tests/conftest.py
index 1481fba3f4..510993c56e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -72,8 +72,15 @@ def request_cart_with_item(product_in_stock, request_cart):
@pytest.fixture
-def order(billing_address):
- return Order.objects.create(billing_address=billing_address)
+def order(billing_address, customer_user):
+ return Order.objects.create(
+ billing_address=billing_address,
+ user_email=customer_user.email,
+ total_net=Money(100, currency='USD'),
+ total_gross=Money(123, currency='USD'),
+ shipping_price_net=Money(10, currency='USD'),
+ shipping_price_gross=Money(12, currency='USD'),
+ user=customer_user)
@pytest.fixture()
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 38f52111e3..6c03a18bf3 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -4,36 +4,20 @@ import pytest
from django.conf import settings
import saleor.order.emails as emails
+from saleor.order.emails_helpers import get_order_confirmation_markup
-DOMAIN = 'mirumee.com'
-SITE_NAME = 'mirumee.com'
-URL = 'wooba/looba'
-EMAIL_FROM = settings.ORDER_FROM_EMAIL
-
[email protected]('fun,template,update_context', [
- (emails.send_order_confirmation, emails.CONFIRM_ORDER_TEMPLATE, True),
- (emails.send_payment_confirmation, emails.CONFIRM_PAYMENT_TEMPLATE, False),
- (emails.send_note_confirmation, emails.CONFIRM_NOTE_TEMPLATE, False)])
[email protected]('send_email,template', [
+ (emails.send_payment_confirmation, emails.CONFIRM_PAYMENT_TEMPLATE),
+ (emails.send_note_confirmation, emails.CONFIRM_NOTE_TEMPLATE),
+ (emails.send_order_confirmation, emails.CONFIRM_ORDER_TEMPLATE)])
@mock.patch('saleor.order.emails.send_templated_mail')
-def test_send_emails(
- mocked_templated_email, update_context, order, template, fun):
- with mock.patch('saleor.order.emails.collect_data_for_email',
- return_value={
- 'email': EMAIL,
- 'url': URL,
- 'template': template}):
- fun(order.pk)
- context = {
- 'protocol': 'http',
- 'domain': DOMAIN,
- 'site_name': SITE_NAME,
- 'url': URL}
- if update_context:
- context.update({'order': order})
- mocked_templated_email.assert_called_once_with(
- recipient_list=[EMAIL],
- context=context,
- from_email=EMAIL_FROM,
- template_name=template)
+def test_send_emails(mocked_templated_email, order, template, send_email):
+ send_email(order.pk)
+ email_data = emails.collect_data_for_email(order.pk, template)
+ email_context = email_data['context']
+ mocked_templated_email.assert_called_once_with(
+ recipient_list=[order.get_user_current_email()],
+ context=email_context,
+ from_email=settings.ORDER_FROM_EMAIL,
+ template_name=template)