Skip to content

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

Raise error when sender address is not set

Mikail KOCAK62f487a70399
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index 9a1f47a108..b43ab1c50c 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -283,10 +283,7 @@ class Shop(graphene.ObjectType):
@staticmethod
@permission_required("site.manage_settings")
def resolve_default_mail_sender_address(_, info):
- return (
- info.context.site.settings.default_mail_sender_address
- or settings.DEFAULT_FROM_EMAIL
- )
+ return info.context.site.settings.default_mail_sender_address
@staticmethod
def resolve_company_address(_, info):
diff --git a/saleor/settings.py b/saleor/settings.py
index df3e7bf330..f5fe88c427 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -122,7 +122,9 @@ if not EMAIL_URL and SENDGRID_USERNAME and SENDGRID_PASSWORD:
SENDGRID_USERNAME,
SENDGRID_PASSWORD,
)
-email_config = dj_email_url.parse(EMAIL_URL or "console://")
+email_config = dj_email_url.parse(
+ EMAIL_URL or "console://[email protected]:console@example/"
+)
EMAIL_FILE_PATH = email_config["EMAIL_FILE_PATH"]
EMAIL_HOST_USER = email_config["EMAIL_HOST_USER"]
diff --git a/saleor/site/models.py b/saleor/site/models.py
index ec87170b60..1a717f1d95 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -4,6 +4,7 @@ from typing import Optional
from django.conf import settings
from django.contrib.sites.models import Site
+from django.core.exceptions import ImproperlyConfigured
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import pgettext_lazy
@@ -83,15 +84,15 @@ class SiteSettings(models.Model):
return self.site.name
@property
- def default_from_email(self) -> Optional[str]:
- sender_name: Optional[str] = self.default_mail_sender_name or ""
+ def default_from_email(self) -> str:
+ sender_name: str = self.default_mail_sender_name or ""
sender_address: Optional[str] = self.default_mail_sender_address
if not sender_address:
sender_address = settings.DEFAULT_FROM_EMAIL
if not sender_address:
- return None
+ raise ImproperlyConfigured("No sender email address has been set-up")
sender_name, sender_address = parseaddr(sender_address)
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index d790300e05..b489550058 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -302,7 +302,7 @@ def test_query_default_mail_sender_settings_not_set(
data = content["data"]["shop"]
assert data["defaultMailSenderName"] is None
- assert data["defaultMailSenderAddress"] == "[email protected]"
+ assert data["defaultMailSenderAddress"] is None
def test_shop_digital_content_settings_mutation(
diff --git a/tests/test_emails.py b/tests/test_emails.py
index 5f9d09dfc5..cbf7138a34 100644
--- a/tests/test_emails.py
+++ b/tests/test_emails.py
@@ -2,6 +2,7 @@ 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
@@ -202,7 +203,22 @@ def test_send_dummy_email_with_header_injection(
):
site_settings.default_mail_sender_address = sender_name
site_settings.default_mail_sender_name = sender_address
- site_settings.save()
+ 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",)