diff --git a/libsys_airflow/plugins/data_exports/email.py b/libsys_airflow/plugins/data_exports/email.py index cad123e90..f39fdef4d 100644 --- a/libsys_airflow/plugins/data_exports/email.py +++ b/libsys_airflow/plugins/data_exports/email.py @@ -7,7 +7,7 @@ from airflow.configuration import conf from airflow.decorators import task from airflow.models import Variable -from airflow.utils.email import send_email +from libsys_airflow.plugins.shared.utils import send_email_with_server_name from libsys_airflow.plugins.shared.utils import is_production @@ -132,7 +132,9 @@ def generate_holdings_errors_emails(error_reports: dict): html_content = _oclc_report_html(report, library) - send_email(to=to_emails, subject=subject_line, html_content=html_content) + send_email_with_server_name( + to=to_emails, subject=subject_line, html_content=html_content + ) def generate_oclc_new_marc_errors_email(error_reports: dict): @@ -168,7 +170,9 @@ def generate_oclc_new_marc_errors_email(error_reports: dict): html_content = _oclc_report_html(report, library) - send_email(to=to_emails, subject=subject_line, html_content=html_content) + send_email_with_server_name( + to=to_emails, subject=subject_line, html_content=html_content + ) def generate_multiple_oclc_identifiers_email(multiple_codes: list): @@ -189,7 +193,7 @@ def generate_multiple_oclc_identifiers_email(multiple_codes: list): html_content = _oclc_identifiers(multiple_codes, folio_url) if is_production(): - send_email( + send_email_with_server_name( to=[ devs_email, cohort_emails["business"], @@ -203,7 +207,7 @@ def generate_multiple_oclc_identifiers_email(multiple_codes: list): ) else: folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "") - send_email( + send_email_with_server_name( to=[ devs_email, ], @@ -269,7 +273,7 @@ def failed_transmission_email(files: list, **kwargs): run_url, ) - send_email( + send_email_with_server_name( to=[ devs_to_email_addr, ], diff --git a/libsys_airflow/plugins/digital_bookplates/email.py b/libsys_airflow/plugins/digital_bookplates/email.py index d4007caf1..f2a1d87c6 100644 --- a/libsys_airflow/plugins/digital_bookplates/email.py +++ b/libsys_airflow/plugins/digital_bookplates/email.py @@ -5,9 +5,11 @@ from airflow.configuration import conf from airflow.decorators import task from airflow.models import Variable -from airflow.utils.email import send_email -from libsys_airflow.plugins.shared.utils import is_production +from libsys_airflow.plugins.shared.utils import ( + is_production, + send_email_with_server_name, +) logger = logging.getLogger(__name__) @@ -133,6 +135,15 @@ def _summary_add_979_email(dag_runs: list, folio_url: str) -> str: ).render(dag_runs=dag_runs, folio_url=folio_url, dag_url=dag_url) +def _to_addresses(): + devs_to_email_addr = Variable.get("EMAIL_DEVS") + bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL") + addresses = [devs_to_email_addr] + if is_production(): + addresses.append(bookplates_email_addr) + return addresses + + @task def bookplates_metadata_email(**kwargs): """ @@ -158,33 +169,17 @@ def bookplates_metadata_email(**kwargs): logger.info("Updated bookplate metadata to send in email") logger.info("Generating email of fetch digital bookplate metadata run") - devs_to_email_addr = Variable.get("EMAIL_DEVS") - bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL") - folio_url = Variable.get("FOLIO_URL") html_content = _new_updated_bookplates_email_body( new=new_bookplates, updated=updated_bookplates, ) - if is_production(): - send_email( - to=[ - bookplates_email_addr, - devs_to_email_addr, - ], - subject="Digital bookplates new and updated metadata", - html_content=html_content, - ) - else: - folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "") - send_email( - to=[ - devs_to_email_addr, - ], - subject=f"{folio_url} - Digital bookplates new and updated metadata", - html_content=html_content, - ) + send_email_with_server_name( + to=_to_addresses(), + subject="Digital bookplates new and updated metadata", + html_content=html_content, + ) @task @@ -198,38 +193,17 @@ def deleted_from_argo_email(**kwargs): logger.info("No Deleted Druids from Argo") return - devs_to_email_addr = Variable.get("EMAIL_DEVS") - bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL") - folio_url = Variable.get("FOLIO_URL") - html_content = _deleted_from_argo_email_body(deleted_druids) - if is_production(): - send_email( - to=[ - bookplates_email_addr, - devs_to_email_addr, - ], - subject="Deleted Druids from Argo for Digital bookplates", - html_content=html_content, - ) - else: - folio_url = folio_url.replace("https://", "").replace(".stanford.edu", "") - send_email( - to=[ - devs_to_email_addr, - ], - subject=f"{folio_url} - Deleted Druids from Argo for Digital bookplate", - html_content=html_content, - ) + send_email_with_server_name( + to=_to_addresses(), + subject="Deleted Druids from Argo for Digital bookplates", + html_content=html_content, + ) @task def missing_fields_email(**kwargs): - devs_to_email_addr = Variable.get("EMAIL_DEVS") - bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL") - folio_url = Variable.get("FOLIO_URL") - failures = kwargs["failures"] if len(failures) < 1: @@ -238,22 +212,11 @@ def missing_fields_email(**kwargs): html_content = _missing_fields_body(failures) - if is_production(): - send_email( - to=[ - bookplates_email_addr, - devs_to_email_addr, - ], - subject="Missing Fields for Digital Bookplates", - html_content=html_content, - ) - else: - folio_env = folio_url.replace("https://", "").replace(".stanford.edu", "") - send_email( - to=[devs_to_email_addr], - subject=f"{folio_env} - Missing Fields for Digital Bookplates", - html_content=html_content, - ) + send_email_with_server_name( + to=_to_addresses(), + subject="Missing Fields for Digital Bookplates", + html_content=html_content, + ) return True @@ -262,27 +225,15 @@ def missing_fields_email(**kwargs): def summary_add_979_dag_runs(**kwargs): dag_runs = kwargs["dag_runs"] additional_email = kwargs.get("email") - folio_url = Variable.get("FOLIO_URL") - devs_to_email_addr = Variable.get("EMAIL_DEVS") - bookplates_email_addr = Variable.get("BOOKPLATES_EMAIL") - to_emails = [devs_to_email_addr] + to_emails = _to_addresses() if additional_email: to_emails.append(additional_email) html_content = _summary_add_979_email(dag_runs, folio_url) - if is_production(): - to_emails.append(bookplates_email_addr) - send_email( - to=to_emails, - subject="Summary of Adding 979 fields to MARC Workflows", - html_content=html_content, - ) - else: - folio_env = folio_url.replace("https://", "").replace(".stanford.edu", "") - send_email( - to=to_emails, - subject=f"{folio_env} - Summary of Adding 979 fields to MARC", - html_content=html_content, - ) + send_email_with_server_name( + to=to_emails, + subject="Summary of Adding 979 fields to MARC Workflows", + html_content=html_content, + ) diff --git a/libsys_airflow/plugins/folio/encumbrances/email.py b/libsys_airflow/plugins/folio/encumbrances/email.py index b00f95c36..4b67e1c44 100644 --- a/libsys_airflow/plugins/folio/encumbrances/email.py +++ b/libsys_airflow/plugins/folio/encumbrances/email.py @@ -1,10 +1,7 @@ -import re - from airflow.models import Variable -from airflow.utils.email import send_email from jinja2 import Template -from libsys_airflow.plugins.shared.utils import is_production +from libsys_airflow.plugins.shared.utils import send_email_with_server_name def email_log(**kwargs): @@ -28,7 +25,7 @@ def email_log(**kwargs): to_addresses.append(lane_email) with open(log_file, 'r') as fo: - send_email( + send_email_with_server_name( to=to_addresses, subject=subject(library=library), html_content=_email_body(fo), @@ -48,9 +45,4 @@ def _email_body(log): def subject(**kwargs): library = kwargs.get("library", "") - folio_url = Variable.get("FOLIO_URL", "Test or Stage") - if is_production(): - return f"Fix Encumbrances for {library}" - else: - folio_url = re.sub('https?://', '', folio_url) - return f"{folio_url} - Fix Encumbrances for {library}" + return f"Fix Encumbrances for {library}" diff --git a/libsys_airflow/plugins/folio/helpers/bw.py b/libsys_airflow/plugins/folio/helpers/bw.py index f9837dbaf..481889cd9 100644 --- a/libsys_airflow/plugins/folio/helpers/bw.py +++ b/libsys_airflow/plugins/folio/helpers/bw.py @@ -7,7 +7,7 @@ from jinja2 import Template from airflow.models import Variable -from airflow.utils.email import send_email +from libsys_airflow.plugins.shared.utils import send_email_with_server_name logger = logging.getLogger(__name__) @@ -123,7 +123,7 @@ def email_bw_summary(devs_email, task_instance): ) if user_email and len(user_email) > 0: to_addresses.append(user_email) - send_email( + send_email_with_server_name( to=to_addresses, subject=f"Boundwith Summary for file {file_name}", html_content=html_content, @@ -140,7 +140,9 @@ def email_failure(context): html_body = _bw_error_body(ti, params) - send_email(to=to_addresses, subject=f"Error {ti.task_id}", html_content=html_body) + send_email_with_server_name( + to=to_addresses, subject=f"Error {ti.task_id}", html_content=html_body + ) def create_bw_record(**kwargs) -> dict: diff --git a/libsys_airflow/plugins/orafin/emails.py b/libsys_airflow/plugins/orafin/emails.py index 2797ed5a7..21ebe187a 100644 --- a/libsys_airflow/plugins/orafin/emails.py +++ b/libsys_airflow/plugins/orafin/emails.py @@ -9,10 +9,10 @@ from jinja2 import Environment, Template from airflow.models import Variable -from airflow.utils.email import send_email from libsys_airflow.plugins.orafin.models import Invoice from libsys_airflow.plugins.orafin.payments import models_converter +from libsys_airflow.plugins.shared.utils import send_email_with_server_name logger = logging.getLogger(__name__) @@ -223,7 +223,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str): sul_html_content = _excluded_email_body(sul_invoices, folio_url) if len(sul_html_content.strip()) > 0: logger.info(f"Sending email to {sul_to_email_addr} for SUL rejected invoices") - send_email( + send_email_with_server_name( to=[ sul_to_email_addr, devs_to_email_addr, @@ -238,7 +238,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str): logger.info( f"Sending email to {bus_to_email_addr} for Business rejected invoices" ) - send_email( + send_email_with_server_name( to=[bus_to_email_addr, devs_to_email_addr], subject="Rejected Invoices for Business", html_content=bus_html_content, @@ -247,7 +247,7 @@ def generate_excluded_email(invoices_reasons: list, folio_url: str): law_html_content = _excluded_email_body(law_invoices, folio_url) if len(law_html_content.strip()) > 0: logger.info(f"Sending email to {law_to_email_addr} for Law rejected invoices") - send_email( + send_email_with_server_name( to=[ law_to_email_addr, devs_to_email_addr, @@ -293,7 +293,7 @@ def generate_invoice_error_email(invoice_id: str, folio_url: str, ti=None): folio_url=folio_url, invoice_id=invoice_id, row=ap_report_row ) - send_email( + send_email_with_server_name( to=[ sul_to_email_addr, bus_to_email_addr, @@ -352,7 +352,7 @@ def generate_ap_error_report_email(folio_url: str, ti=None) -> int: missing_invoices_df, cancelled_invoices_df, paid_invoices_df, folio_url ) - send_email( + send_email_with_server_name( to=[ sul_to_email_addr, law_to_email_addr, @@ -392,7 +392,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None): logger.info( f"Sending email to {sul_to_email_addr} for {len(sul_invoices):,} invoices" ) - send_email( + send_email_with_server_name( to=[ sul_to_email_addr, devs_to_email_addr, @@ -409,7 +409,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None): logger.info( f"Sending email to {bus_to_email_addr} for {len(bus_invoices):,} invoices" ) - send_email( + send_email_with_server_name( to=[bus_to_email_addr, devs_to_email_addr], subject=f"Paid Invoices from {ap_report_name} for Business", html_content=business_html_content, @@ -424,7 +424,7 @@ def generate_ap_paid_report_email(folio_url: str, task_instance=None): logger.info( f"Sending email to {law_to_email_addr} for {len(law_invoices):,} invoices" ) - send_email( + send_email_with_server_name( to=[ law_to_email_addr, devs_to_email_addr, @@ -456,7 +456,7 @@ def generate_summary_email(invoices: list, folio_url: str): logger.info( f"Sending email to {sul_to_email_addr} for {len(sul_invoices)} invoices" ) - send_email( + send_email_with_server_name( to=[ sul_to_email_addr, devs_to_email_addr, @@ -471,7 +471,7 @@ def generate_summary_email(invoices: list, folio_url: str): logger.info( f"Sending email to {bus_to_email_addr} for {len(bus_invoices):,} invoices" ) - send_email( + send_email_with_server_name( to=[bus_to_email_addr, devs_to_email_addr], subject="Approved Invoices Sent to AP for Business", html_content=business_html_content, @@ -483,7 +483,7 @@ def generate_summary_email(invoices: list, folio_url: str): logger.info( f"Sending email to {law_to_email_addr} for {len(law_invoices):,} invoices" ) - send_email( + send_email_with_server_name( to=[ law_to_email_addr, devs_to_email_addr, diff --git a/libsys_airflow/plugins/shared/utils.py b/libsys_airflow/plugins/shared/utils.py index 9fa222ae2..18a135a8c 100644 --- a/libsys_airflow/plugins/shared/utils.py +++ b/libsys_airflow/plugins/shared/utils.py @@ -2,9 +2,11 @@ import json import logging import pymarc +import re from typing import Union from airflow.models import Variable +from airflow.utils.email import send_email from libsys_airflow.plugins.shared.folio_client import folio_client @@ -12,7 +14,33 @@ def is_production(): - return bool(Variable.get("OKAPI_URL").find("prod") > 0) + return Variable.get("OKAPI_URL").find("prod") > 0 + + +def send_email_with_server_name(**kwargs): + """ + send_email wrapper to include subject with server name + when not run in production + """ + devs_to_email_addr = Variable.get("EMAIL_DEVS") + to_addresses = kwargs.get("to", devs_to_email_addr) + subject = kwargs.get("subject") + html_content = kwargs.get("html_content") + send_email( + to=to_addresses, + subject=_subject_with_server_name(subject=subject), + html_content=html_content, + ) + + +def _subject_with_server_name(**kwargs): + subject = kwargs.get("subject") + folio_url = Variable.get("FOLIO_URL", "folio-test/stage") + if is_production(): + return subject + else: + folio_url = re.sub('https?://', '', folio_url) + return f"{folio_url} - {subject}" class FolioAddMarcTags(object): diff --git a/libsys_airflow/plugins/vendor/emails.py b/libsys_airflow/plugins/vendor/emails.py index 37434addd..516eaac79 100644 --- a/libsys_airflow/plugins/vendor/emails.py +++ b/libsys_airflow/plugins/vendor/emails.py @@ -5,7 +5,6 @@ from airflow.configuration import conf from airflow.decorators import task from airflow.models import Variable -from airflow.utils.email import send_email from airflow.providers.postgres.hooks.postgres import PostgresHook from sqlalchemy.orm import Session @@ -17,6 +16,8 @@ extract_double_zero_one_field_values, ) from libsys_airflow.plugins.vendor.edi import invoice_count +from libsys_airflow.plugins.shared.utils import send_email_with_server_name + logger = logging.getLogger(__name__) @@ -38,10 +39,10 @@ def send_files_fetched_email(**kwargs): subject = Template( "{{vendor_interface_name}} ({{vendor_code}}) - Daily Fetch Report ({{date}}) [{{environment}}]" ).render(kwargs) - send_email( - Variable.get('VENDOR_LOADS_TO_EMAIL'), - subject, - _files_fetched_html_content(**kwargs), + send_email_with_server_name( + to=Variable.get('VENDOR_LOADS_TO_EMAIL'), + subject=subject, + html_content=_files_fetched_html_content(**kwargs), ) @@ -103,10 +104,10 @@ def send_file_loaded_email(**kwargs): subject = Template( "{{vendor_interface_name}} ({{vendor_code}}) - ({{filename}}) - File Load Report [{{environment}}]" ).render(kwargs) - send_email( - Variable.get('VENDOR_LOADS_TO_EMAIL'), - subject, - html_content, + send_email_with_server_name( + to=Variable.get('VENDOR_LOADS_TO_EMAIL'), + subject=subject, + html_content=html_content, ) @@ -193,12 +194,12 @@ def file_not_loaded_email_task(**kwargs): def send_file_not_loaded_email(**kwargs): - send_email( - Variable.get('VENDOR_LOADS_TO_EMAIL'), - Template( + send_email_with_server_name( + to=Variable.get('VENDOR_LOADS_TO_EMAIL'), + subject=Template( "{{vendor_interface_name}} ({{vendor_code}}) - ({{filename}}) - File Processed [{{environment}}]" ).render(kwargs), - _file_not_loaded_html_content(**kwargs), + html_content=_file_not_loaded_html_content(**kwargs), ) diff --git a/tests/data_exports/test_data_exports_emails.py b/tests/data_exports/test_data_exports_emails.py index a21716998..4151cd199 100644 --- a/tests/data_exports/test_data_exports_emails.py +++ b/tests/data_exports/test_data_exports_emails.py @@ -56,7 +56,7 @@ def mock_dag_run(mocker): def test_multiple_oclc_email(mocker, mock_folio_variables): mock_send_email = mocker.patch( - "libsys_airflow.plugins.data_exports.email.send_email" + "libsys_airflow.plugins.data_exports.email.send_email_with_server_name" ) generate_multiple_oclc_identifiers_email( @@ -99,7 +99,9 @@ def test_multiple_oclc_email(mocker, mock_folio_variables): def test_no_multiple_oclc_code_email(mocker, mock_folio_variables, caplog): - mocker.patch("libsys_airflow.plugins.data_exports.email.send_email") + mocker.patch( + "libsys_airflow.plugins.data_exports.email.send_email_with_server_name" + ) generate_multiple_oclc_identifiers_email([]) @@ -108,7 +110,7 @@ def test_no_multiple_oclc_code_email(mocker, mock_folio_variables, caplog): def test_nonprod_oclc_email(mocker, mock_folio_variables): mock_send_email = mocker.patch( - "libsys_airflow.plugins.data_exports.email.send_email" + "libsys_airflow.plugins.data_exports.email.send_email_with_server_name" ) mocker.patch( @@ -149,7 +151,7 @@ def test_no_failed_transmission_email(mock_dag_run, caplog): def test_failed_transmission_email(mocker, mock_dag_run, mock_folio_variables, caplog): mock_send_email = mocker.patch( - "libsys_airflow.plugins.data_exports.email.send_email" + "libsys_airflow.plugins.data_exports.email.send_email_with_server_name" ) files = [ @@ -200,7 +202,7 @@ def test_failed_full_dump_transmission_email( mock_dag_run.id = "send_all_records" mock_send_email = mocker.patch( - "libsys_airflow.plugins.data_exports.email.send_email" + "libsys_airflow.plugins.data_exports.email.send_email_with_server_name" ) files = [ diff --git a/tests/digital_bookplates/test_bookplates_emails.py b/tests/digital_bookplates/test_bookplates_emails.py index 68632577d..b27b7a362 100644 --- a/tests/digital_bookplates/test_bookplates_emails.py +++ b/tests/digital_bookplates/test_bookplates_emails.py @@ -31,7 +31,7 @@ @pytest.fixture def mock_folio_variables(monkeypatch): - def mock_get(key): + def mock_get(key, *args): value = None match key: case "FOLIO_URL": @@ -81,9 +81,7 @@ def test_bookplates_metadata_email( mocker, mock_bookplate_metadata, mock_folio_variables ): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") bookplates_metadata_email.function( new=mock_bookplate_metadata["new"], updated=mock_bookplate_metadata["updated"] @@ -121,9 +119,7 @@ def test_bookplates_metadata_email( def test_bookplates_metadata_email_none(mocker, mock_folio_variables, caplog): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") bookplates_metadata_email.function(new=None, updated=None) @@ -138,9 +134,7 @@ def test_no_new_bookplates_metadata_email( mocker, mock_bookplate_metadata, mock_folio_variables, caplog ): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") bookplates_metadata_email.function( new=[], updated=mock_bookplate_metadata["updated"] @@ -173,9 +167,7 @@ def test_no_updated_bookplates_metadata_email( mocker, mock_bookplate_metadata, mock_folio_variables, caplog ): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") bookplates_metadata_email.function( new=mock_bookplate_metadata["new"], @@ -201,9 +193,7 @@ def test_no_updated_bookplates_metadata_email( def test_deleted_from_argo_email(mocker, mock_folio_variables): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") deleted_druids_info = [ {"title": "The Happy Fund", "druid": "ab123xy4567", "fund_name": "HAPY"} @@ -226,9 +216,7 @@ def test_deleted_from_argo_email(mocker, mock_folio_variables): def test_deleted_from_argo_email_no_druids(mocker, caplog): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") deleted_from_argo_email.function(deleted_druids=[]) @@ -237,12 +225,10 @@ def test_deleted_from_argo_email_no_druids(mocker, caplog): def test_deleted_from_argo_email_prod(mocker, mock_folio_variables, caplog): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.is_production", + "libsys_airflow.plugins.shared.utils.is_production", return_value=True, ) @@ -254,9 +240,7 @@ def test_deleted_from_argo_email_prod(mocker, mock_folio_variables, caplog): def test_missing_fields_email(mocker, mock_folio_variables): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") missing_fields_email.function(failures=failures) @@ -284,12 +268,10 @@ def test_missing_fields_email_no_failures(mock_folio_variables, caplog): def test_missing_fields_email_prod(mocker, mock_folio_variables): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.is_production", + "libsys_airflow.plugins.shared.utils.is_production", return_value=True, ) @@ -301,9 +283,7 @@ def test_missing_fields_email_prod(mocker, mock_folio_variables): def test_summary_add_979_dag_runs(mocker, mock_folio_variables): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.patch("libsys_airflow.plugins.shared.utils.send_email") mocker.patch( "libsys_airflow.plugins.digital_bookplates.email.conf.get", @@ -357,15 +337,21 @@ def test_summary_add_979_dag_runs(mocker, mock_folio_variables): def test_summary_add_979_dag_runs_prod(mocker, mock_folio_variables): - mock_send_email = mocker.patch( - "libsys_airflow.plugins.digital_bookplates.email.send_email" - ) + mock_send_email = mocker.MagicMock() + + mock_is_production = lambda: True # noqa mocker.patch( "libsys_airflow.plugins.digital_bookplates.email.is_production", return_value=True, ) + mocker.patch.multiple( + "libsys_airflow.plugins.shared.utils", + send_email=mock_send_email, + is_production=mock_is_production, + ) + summary_add_979_dag_runs.function(dag_runs={}, email="dscully@stanford.edu") assert mock_send_email.call_args[1]["subject"].startswith( @@ -374,6 +360,6 @@ def test_summary_add_979_dag_runs_prod(mocker, mock_folio_variables): assert mock_send_email.call_args[1]["to"] == [ 'test@example.com', - 'dscully@stanford.edu', 'nobody@example.com', + 'dscully@stanford.edu', ] diff --git a/tests/helpers/test_boundwiths.py b/tests/helpers/test_boundwiths.py index c39d814ba..d2607e42b 100644 --- a/tests/helpers/test_boundwiths.py +++ b/tests/helpers/test_boundwiths.py @@ -229,7 +229,9 @@ def mock_get(*args): """ return response - mock_send_email = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.folio.helpers.bw.send_email_with_server_name" + ) mock_requests = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.requests") mock_requests.get = mock_get mock_variable = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.Variable") @@ -265,7 +267,9 @@ def mock_get(*args): response.text = """Internal Server Error""" return response - mock_send_email = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.folio.helpers.bw.send_email_with_server_name" + ) mock_requests = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.requests") mock_requests.get = mock_get mock_variable = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.Variable") @@ -293,7 +297,9 @@ def mock_get(*args): response.text = """
""" return response - mock_send_email = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.folio.helpers.bw.send_email_with_server_name" + ) mock_requests = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.requests") mock_requests.get = mock_get mock_variable = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.Variable") @@ -315,7 +321,9 @@ def mock_get(*args): def test_email_bw_summary(mocker, mock_task_instance, mock_context): - mock_send_email = mocker.patch("libsys_airflow.plugins.folio.helpers.bw.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.folio.helpers.bw.send_email_with_server_name" + ) email_bw_summary('libsys-lists@example.com', mock_task_instance) diff --git a/tests/orafin/test_generated_emails.py b/tests/orafin/test_generated_emails.py index 86c43cd9f..a768d530b 100644 --- a/tests/orafin/test_generated_emails.py +++ b/tests/orafin/test_generated_emails.py @@ -144,7 +144,9 @@ def mock_retrieve_invoice_task_xcom_pull(**kwargs): def test_generate_ap_error_report_email(mocker): - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", @@ -211,7 +213,9 @@ def _no_missing_xcom(**kwargs): return [{"invoice_id": "9cf2899a-c7a6-4101-bf8e-c5996ded5fd1"}] return None - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", @@ -260,7 +264,9 @@ def _paid_xcom_pull(**kwargs): }, ] - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", @@ -290,7 +296,9 @@ def _paid_xcom_pull(**kwargs): def test_generate_excluded_email(mocker): - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", @@ -382,7 +390,9 @@ def mock_xcom_pull(*args, **kwargs): 'PoNumber': None, } - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", @@ -421,7 +431,9 @@ def mock_xcom_pull(*args, **kwargs): def test_generate_summary_email(mocker): - mock_send_email = mocker.patch("libsys_airflow.plugins.orafin.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.orafin.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.orafin.emails.Variable.get", diff --git a/tests/test_fix_encumbrances_run.py b/tests/test_fix_encumbrances_run.py index 808fd724f..4b3610bde 100644 --- a/tests/test_fix_encumbrances_run.py +++ b/tests/test_fix_encumbrances_run.py @@ -55,7 +55,8 @@ def test_fix_encumbrances_log_file_params( def test_fix_encumbrances_email_subject(): + from libsys_airflow.plugins.shared.utils import _subject_with_server_name from libsys_airflow.plugins.folio.encumbrances.email import subject - subj = subject(library="SUL2024") + subj = _subject_with_server_name(subject=subject(library="SUL2024")) assert subj == "okapi-test - Fix Encumbrances for SUL2024" diff --git a/tests/vendor/test_emails.py b/tests/vendor/test_emails.py index 8412b4168..0187b7058 100644 --- a/tests/vendor/test_emails.py +++ b/tests/vendor/test_emails.py @@ -89,7 +89,9 @@ def marc_path(tmp_path): def test_send_files_fetched_email(pg_hook, mocker, mock_folio_variables): mock_date = mocker.patch("libsys_airflow.plugins.vendor.emails.date") mock_date.today.return_value = date(2021, 1, 1) - mock_send_email = mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.vendor.emails.send_email_with_server_name" + ) send_files_fetched_email( vendor_uuid='375C6E33-2468-40BD-A5F2-73F82FE56DB0', @@ -102,9 +104,9 @@ def test_send_files_fetched_email(pg_hook, mocker, mock_folio_variables): ) mock_send_email.assert_called_once_with( - 'test@stanford.edu', - "Acme FTP (ACME) - Daily Fetch Report (2021-01-01) [development]", - """ + to='test@stanford.edu', + subject="Acme FTP (ACME) - Daily Fetch Report (2021-01-01) [development]", + html_content="""
Acme FTP (ACME) - 140530EB-EE54-4302-81EE-D83B9DAC9B6E

@@ -124,7 +126,7 @@ def test_send_files_fetched_email(pg_hook, mocker, mock_folio_variables): def test_files_fetched_email_task(pg_hook, mocker, mock_folio_variables, caplog): mock_date = mocker.patch("libsys_airflow.plugins.vendor.emails.date") mock_date.today.return_value = date(2021, 1, 1) - mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mocker.patch("libsys_airflow.plugins.vendor.emails.send_email_with_server_name") files_fetched_email_task.function( [], @@ -153,7 +155,9 @@ def test_send_file_loaded_bib_email_no_001s( ) mock_date = mocker.patch("libsys_airflow.plugins.vendor.emails.date") mock_date.today.return_value = date(2021, 1, 1) - mock_send_email = mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.vendor.emails.send_email_with_server_name" + ) record = pymarc.Record() record.add_field( pymarc.Field( @@ -200,9 +204,9 @@ def test_send_file_loaded_bib_email_no_001s( ) mock_send_email.assert_called_once_with( - 'test@stanford.edu', - "Acme FTP (ACME) - (123456.mrc) - File Load Report [development]", - f""" + to='test@stanford.edu', + subject="Acme FTP (ACME) - (123456.mrc) - File Load Report [development]", + html_content=f"""

FOLIO Catalog MARC Load started on {now}
Acme FTP (ACME) - 140530EB-EE54-4302-81EE-D83B9DAC9B6E
@@ -228,7 +232,9 @@ def test_send_file_loaded_bib_email_with_001s( mock_date = mocker.patch("libsys_airflow.plugins.vendor.emails.date") mock_date.today.return_value = date(2021, 1, 1) - mock_send_email = mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.vendor.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.vendor.emails.is_marc", return_value=True, @@ -277,9 +283,9 @@ def test_send_file_loaded_bib_email_with_001s( ) mock_send_email.assert_called_once_with( - 'test@stanford.edu', - "Acme FTP (ACME) - (123456.mrc) - File Load Report [development]", - f""" + to='test@stanford.edu', + subject="Acme FTP (ACME) - (123456.mrc) - File Load Report [development]", + html_content=f"""
FOLIO Catalog MARC Load started on {now}
Acme FTP (ACME) - 140530EB-EE54-4302-81EE-D83B9DAC9B6E
@@ -316,7 +322,9 @@ def test_send_file_loaded_edi_email(pg_hook, mocker, mock_folio_variables, tmp_p return_value="https://sul-libsys-airflow-stage.stanford.edu/", ) - mock_send_email = mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.vendor.emails.send_email_with_server_name" + ) now = datetime.now() @@ -349,9 +357,9 @@ def test_send_file_loaded_edi_email(pg_hook, mocker, mock_folio_variables, tmp_p ) mock_send_email.assert_called_once_with( - 'test@stanford.edu', - "Acme FTP (ACME) - (inv574076.edi.txt) - File Load Report [development]", - f""" + to='test@stanford.edu', + subject="Acme FTP (ACME) - (inv574076.edi.txt) - File Load Report [development]", + html_content=f"""
FOLIO Catalog EDI Load started on {now}
Acme FTP (ACME) - 140530EB-EE54-4302-81EE-D83B9DAC9B6E
@@ -364,7 +372,9 @@ def test_send_file_loaded_edi_email(pg_hook, mocker, mock_folio_variables, tmp_p def test_send_file_not_loaded_email(pg_hook, mocker, mock_folio_variables): - mock_send_email = mocker.patch("libsys_airflow.plugins.vendor.emails.send_email") + mock_send_email = mocker.patch( + "libsys_airflow.plugins.vendor.emails.send_email_with_server_name" + ) mocker.patch( "libsys_airflow.plugins.vendor.emails.conf.get", return_value="https://sul-libsys-airflow-stage.stanford.edu/", @@ -381,9 +391,9 @@ def test_send_file_not_loaded_email(pg_hook, mocker, mock_folio_variables): ) mock_send_email.assert_called_once_with( - 'test@stanford.edu', - "Acme FTP (ACME) - (123456.mrc) - File Processed [development]", - f""" + to='test@stanford.edu', + subject="Acme FTP (ACME) - (123456.mrc) - File Processed [development]", + html_content=f"""
Acme FTP (ACME) - 140530EB-EE54-4302-81EE-D83B9DAC9B6E