Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test email failure #117

Merged
merged 16 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions harvester/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def report(self) -> None:
if hasattr(self, "notification_emails") and self.notification_emails:
self.send_notification_emails(actual_results_action)

def send_notification_emails(self, results: dict) -> None:
def send_notification_emails(self, results: dict) -> str:
try:
job_url = f'{SMTP_CONFIG["base_url"]}/harvest_job/{self.job_id}'

Expand Down Expand Up @@ -438,9 +438,12 @@ def send_notification_emails(self, results: dict) -> None:
server.sendmail(SMTP_CONFIG["default_sender"], [recipient],
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved
msg.as_string())
logger.info(f"Notification email sent to: {recipient}")
return "Emails sent successfully"
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved

except Exception as e:
logger.error(f"Error preparing or sending notification emails: {e}")
error_message = "Error preparing or sending notification emails"
logger.error(error_message + f": {e}")
return error_message
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved


@dataclass
Expand Down
36 changes: 35 additions & 1 deletion tests/integration/harvest/test_harvest_full_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
from unittest.mock import patch

from harvester.harvest import HarvestSource


Expand Down Expand Up @@ -162,3 +161,38 @@ def test_validate_same_title(
assert kwargs["title"] == "Commitment of Traders"
assert kwargs["id"] == "5678"
assert kwargs["identifier"] == "cftc-dc2"

@patch("harvester.harvest.smtplib.SMTP")
def test_harvest_send_notification_failure(
mock_smtp,
CKANMock,
interface,
organization_data,
source_data_dcatus_single_record,
):
CKANMock.action.package_create.return_value = {"id": 1234}
CKANMock.action.package_update = "ok"
CKANMock.action.dataset_purge = "ok"
interface.add_organization(organization_data)
interface.add_harvest_source(source_data_dcatus_single_record)
harvest_job = interface.add_harvest_job(
{
"status": "new",
"harvest_source_id": source_data_dcatus_single_record["id"],
}
)
job_id = harvest_job.id
harvest_source = HarvestSource(job_id)
harvest_source.notification_emails = source_data_dcatus_single_record[
"notification_emails"
]

results = {"create": 10, "update": 5, "delete": 3, None: 2}
mock_smtp.side_effect = Exception("SMTP connection failed")
error_message = harvest_source.send_notification_emails(results)
Jin-Sun-tts marked this conversation as resolved.
Show resolved Hide resolved

assert mock_smtp.side_effect is not None, "Mock SMTP side_effect was not set!"

assert error_message == "Error preparing or sending notification emails"