From 080a5793ea8cc1a5dd4a3a269fd0823065ba8b94 Mon Sep 17 00:00:00 2001 From: Joffrey Bienvenu Date: Sat, 28 Oct 2023 15:40:32 +0200 Subject: [PATCH] feat: make `cc` and `bcc` templated fields in EmailOperator --- airflow/providers/smtp/operators/smtp.py | 6 +++--- tests/providers/smtp/operators/test_smtp.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/airflow/providers/smtp/operators/smtp.py b/airflow/providers/smtp/operators/smtp.py index 8dd6889ab07f1..db1f162c21287 100644 --- a/airflow/providers/smtp/operators/smtp.py +++ b/airflow/providers/smtp/operators/smtp.py @@ -36,15 +36,15 @@ class EmailOperator(BaseOperator): :param html_content: content of the email, html markup is allowed. (templated) :param files: file names to attach in email (templated) - :param cc: list of recipients to be added in CC field - :param bcc: list of recipients to be added in BCC field + :param cc: list of recipients to be added in CC field (templated) + :param bcc: list of recipients to be added in BCC field (templated) :param mime_subtype: MIME sub content type :param mime_charset: character set parameter added to the Content-Type header. :param custom_headers: additional headers to add to the MIME message. """ - template_fields: Sequence[str] = ("to", "from_email", "subject", "html_content", "files") + template_fields: Sequence[str] = ("to", "from_email", "subject", "html_content", "files", "cc", "bcc") template_fields_renderers = {"html_content": "html"} template_ext: Sequence[str] = (".html",) ui_color = "#e6faf9" diff --git a/tests/providers/smtp/operators/test_smtp.py b/tests/providers/smtp/operators/test_smtp.py index 0786d0b07379c..6239c058dadf5 100644 --- a/tests/providers/smtp/operators/test_smtp.py +++ b/tests/providers/smtp/operators/test_smtp.py @@ -27,6 +27,9 @@ class TestEmailOperator: + def setup_method(self): + self.default_op_kwargs = dict(to="to", subject="subject", html_content="content") + @patch("airflow.providers.smtp.hooks.smtp.SmtpHook.get_connection") @patch(smtplib_string) def test_loading_sender_email_from_connection(self, mock_smtplib, mock_hook_conn): @@ -46,7 +49,13 @@ def test_loading_sender_email_from_connection(self, mock_smtplib, mock_hook_conn ), ) smtp_client_mock = mock_smtplib.SMTP_SSL() - op = EmailOperator(task_id="test_email", to="to", subject="subject", html_content="content") + op = EmailOperator(task_id="test_email", **self.default_op_kwargs) op.execute({}) call_args = smtp_client_mock.sendmail.call_args.kwargs assert call_args["from_addr"] == sender_email + + def test_assert_templated_fields(self): + """Test expected templated fields.""" + operator = EmailOperator(task_id="test_assert_templated_fields", **self.default_op_kwargs) + template_fields = ("to", "from_email", "subject", "html_content", "files", "cc", "bcc") + assert operator.template_fields == template_fields