chore: proper current_app.config proxy usage (#34345)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Maxime Beauchemin
2025-07-31 19:27:42 -07:00
committed by GitHub
parent 6c9cda758a
commit cb27d5fe8d
144 changed files with 1428 additions and 1119 deletions

View File

@@ -25,7 +25,8 @@ from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from unittest import mock
from superset import app
from flask import current_app
from superset.utils import core as utils
from tests.integration_tests.base_tests import SupersetTestCase
@@ -37,7 +38,7 @@ logger = logging.getLogger(__name__)
class TestEmailSmtp(SupersetTestCase):
def setUp(self):
app.config["SMTP_SSL"] = False
current_app.config["SMTP_SSL"] = False
@mock.patch("superset.utils.core.send_mime_email")
def test_send_smtp(self, mock_send_mime):
@@ -45,16 +46,16 @@ class TestEmailSmtp(SupersetTestCase):
attachment.write(b"attachment")
attachment.seek(0)
utils.send_email_smtp(
"to", "subject", "content", app.config, files=[attachment.name]
"to", "subject", "content", current_app.config, files=[attachment.name]
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEApplication("attachment")
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
@@ -66,29 +67,29 @@ class TestEmailSmtp(SupersetTestCase):
attachment.seek(0)
# putting this into a variable so that we can reset after the test
base_email_mutator = app.config["EMAIL_HEADER_MUTATOR"]
base_email_mutator = current_app.config["EMAIL_HEADER_MUTATOR"]
def mutator(msg, **kwargs):
msg["foo"] = "bar"
return msg
app.config["EMAIL_HEADER_MUTATOR"] = mutator
current_app.config["EMAIL_HEADER_MUTATOR"] = mutator
utils.send_email_smtp(
"to", "subject", "content", app.config, files=[attachment.name]
"to", "subject", "content", current_app.config, files=[attachment.name]
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert msg["foo"] == "bar"
assert len(msg.get_payload()) == 2
mimeapp = MIMEApplication("attachment")
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator
current_app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator
@mock.patch("superset.utils.core.send_mime_email")
def test_send_smtp_with_email_mutator_changing_recipients(self, mock_send_mime):
@@ -97,42 +98,42 @@ class TestEmailSmtp(SupersetTestCase):
attachment.seek(0)
# putting this into a variable so that we can reset after the test
base_email_mutator = app.config["EMAIL_HEADER_MUTATOR"]
base_email_mutator = current_app.config["EMAIL_HEADER_MUTATOR"]
def mutator(msg, **kwargs):
msg.replace_header("To", "mutated")
return msg
app.config["EMAIL_HEADER_MUTATOR"] = mutator
current_app.config["EMAIL_HEADER_MUTATOR"] = mutator
utils.send_email_smtp(
"to", "subject", "content", app.config, files=[attachment.name]
"to", "subject", "content", current_app.config, files=[attachment.name]
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["mutated"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEApplication("attachment")
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator
current_app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator
@mock.patch("superset.utils.core.send_mime_email")
def test_send_smtp_data(self, mock_send_mime):
utils.send_email_smtp(
"to", "subject", "content", app.config, data={"1.txt": b"data"}
"to", "subject", "content", current_app.config, data={"1.txt": b"data"}
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEApplication("data")
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
@@ -144,17 +145,17 @@ class TestEmailSmtp(SupersetTestCase):
"to",
"subject",
"content",
app.config,
current_app.config,
images=dict(blah=image), # noqa: C408
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
logger.debug(call_args)
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEImage(image)
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
@@ -168,18 +169,18 @@ class TestEmailSmtp(SupersetTestCase):
"to",
"subject",
"content",
app.config,
current_app.config,
files=[attachment.name],
cc="cc",
bcc="bcc",
)
assert mock_send_mime.called
call_args = mock_send_mime.call_args[0]
assert call_args[0] == app.config["SMTP_MAIL_FROM"]
assert call_args[0] == current_app.config["SMTP_MAIL_FROM"]
assert call_args[1] == ["to", "cc", "bcc"]
msg = call_args[2]
assert msg["Subject"] == "subject"
assert msg["From"] == app.config["SMTP_MAIL_FROM"]
assert msg["From"] == current_app.config["SMTP_MAIL_FROM"]
assert len(msg.get_payload()) == 2
mimeapp = MIMEApplication("attachment")
assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload()
@@ -190,11 +191,13 @@ class TestEmailSmtp(SupersetTestCase):
mock_smtp.return_value = mock.Mock()
mock_smtp_ssl.return_value = mock.Mock()
msg = MIMEMultipart()
utils.send_mime_email("from", "to", msg, app.config, dryrun=False)
mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"])
utils.send_mime_email("from", "to", msg, current_app.config, dryrun=False)
mock_smtp.assert_called_with(
current_app.config["SMTP_HOST"], current_app.config["SMTP_PORT"]
)
assert mock_smtp.return_value.starttls.called
mock_smtp.return_value.login.assert_called_with(
app.config["SMTP_USER"], app.config["SMTP_PASSWORD"]
current_app.config["SMTP_USER"], current_app.config["SMTP_PASSWORD"]
)
mock_smtp.return_value.sendmail.assert_called_with(
"from", "to", msg.as_string()
@@ -204,37 +207,47 @@ class TestEmailSmtp(SupersetTestCase):
@mock.patch("smtplib.SMTP_SSL")
@mock.patch("smtplib.SMTP")
def test_send_mime_ssl(self, mock_smtp, mock_smtp_ssl):
app.config["SMTP_SSL"] = True
current_app.config["SMTP_SSL"] = True
mock_smtp.return_value = mock.Mock()
mock_smtp_ssl.return_value = mock.Mock()
utils.send_mime_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
utils.send_mime_email(
"from", "to", MIMEMultipart(), current_app.config, dryrun=False
)
assert not mock_smtp.called
mock_smtp_ssl.assert_called_with(
app.config["SMTP_HOST"], app.config["SMTP_PORT"], context=None
current_app.config["SMTP_HOST"],
current_app.config["SMTP_PORT"],
context=None,
)
@mock.patch("smtplib.SMTP_SSL")
@mock.patch("smtplib.SMTP")
def test_send_mime_ssl_server_auth(self, mock_smtp, mock_smtp_ssl):
app.config["SMTP_SSL"] = True
app.config["SMTP_SSL_SERVER_AUTH"] = True
current_app.config["SMTP_SSL"] = True
current_app.config["SMTP_SSL_SERVER_AUTH"] = True
mock_smtp.return_value = mock.Mock()
mock_smtp_ssl.return_value = mock.Mock()
utils.send_mime_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
utils.send_mime_email(
"from", "to", MIMEMultipart(), current_app.config, dryrun=False
)
assert not mock_smtp.called
mock_smtp_ssl.assert_called_with(
app.config["SMTP_HOST"], app.config["SMTP_PORT"], context=mock.ANY
current_app.config["SMTP_HOST"],
current_app.config["SMTP_PORT"],
context=mock.ANY,
)
called_context = mock_smtp_ssl.call_args.kwargs["context"]
assert called_context.verify_mode == ssl.CERT_REQUIRED
@mock.patch("smtplib.SMTP")
def test_send_mime_tls_server_auth(self, mock_smtp):
app.config["SMTP_STARTTLS"] = True
app.config["SMTP_SSL_SERVER_AUTH"] = True
current_app.config["SMTP_STARTTLS"] = True
current_app.config["SMTP_SSL_SERVER_AUTH"] = True
mock_smtp.return_value = mock.Mock()
mock_smtp.return_value.starttls.return_value = mock.Mock()
utils.send_mime_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
utils.send_mime_email(
"from", "to", MIMEMultipart(), current_app.config, dryrun=False
)
mock_smtp.return_value.starttls.assert_called_with(context=mock.ANY)
called_context = mock_smtp.return_value.starttls.call_args.kwargs["context"]
assert called_context.verify_mode == ssl.CERT_REQUIRED
@@ -242,23 +255,29 @@ class TestEmailSmtp(SupersetTestCase):
@mock.patch("smtplib.SMTP_SSL")
@mock.patch("smtplib.SMTP")
def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl):
smtp_user = app.config["SMTP_USER"]
smtp_password = app.config["SMTP_PASSWORD"]
app.config["SMTP_USER"] = None
app.config["SMTP_PASSWORD"] = None
smtp_user = current_app.config["SMTP_USER"]
smtp_password = current_app.config["SMTP_PASSWORD"]
current_app.config["SMTP_USER"] = None
current_app.config["SMTP_PASSWORD"] = None
mock_smtp.return_value = mock.Mock()
mock_smtp_ssl.return_value = mock.Mock()
utils.send_mime_email("from", "to", MIMEMultipart(), app.config, dryrun=False)
utils.send_mime_email(
"from", "to", MIMEMultipart(), current_app.config, dryrun=False
)
assert not mock_smtp_ssl.called
mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"])
mock_smtp.assert_called_with(
current_app.config["SMTP_HOST"], current_app.config["SMTP_PORT"]
)
assert not mock_smtp.login.called
app.config["SMTP_USER"] = smtp_user
app.config["SMTP_PASSWORD"] = smtp_password
current_app.config["SMTP_USER"] = smtp_user
current_app.config["SMTP_PASSWORD"] = smtp_password
@mock.patch("smtplib.SMTP_SSL")
@mock.patch("smtplib.SMTP")
def test_send_mime_dryrun(self, mock_smtp, mock_smtp_ssl):
utils.send_mime_email("from", "to", MIMEMultipart(), app.config, dryrun=True)
utils.send_mime_email(
"from", "to", MIMEMultipart(), current_app.config, dryrun=True
)
assert not mock_smtp.called
assert not mock_smtp_ssl.called