From 998b2e2121480b2c9cebedc0cb9901cfe83307f6 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Thu, 21 Aug 2025 00:21:11 -0600 Subject: [PATCH] - Fix unit tests for hook email trigger conditional based on end date --- radicale/hook/email/__init__.py | 11 +++++--- radicale/tests/test_hook_email.py | 46 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/radicale/hook/email/__init__.py b/radicale/hook/email/__init__.py index 62bf3195..2defaa95 100644 --- a/radicale/hook/email/__init__.py +++ b/radicale/hook/email/__init__.py @@ -981,10 +981,13 @@ class Hook(BaseHook): return email_event_end_time = email_event_event.datetime_end # type: ignore # Skip notification if the event end time is more than 1 minute in the past. - if email_event_end_time and email_event_end_time.time and email_event_end_time.time < ( - datetime.now() - timedelta(minutes=1)): - logger.warning("Event end time is in the past, skipping notification for event: %s", - email_event_event.uid) + if email_event_end_time and email_event_end_time.time: + event_end = email_event_end_time.time # type: ignore + now = datetime.now( + event_end.tzinfo) if event_end.tzinfo else datetime.now() # Handle timezone-aware datetime + if event_end < (now - timedelta(minutes=1)): + logger.warning("Event end time is in the past, skipping notification for event: %s", + email_event_event.uid) return if not previous_item_str: diff --git a/radicale/tests/test_hook_email.py b/radicale/tests/test_hook_email.py index af012a6c..b7fef935 100644 --- a/radicale/tests/test_hook_email.py +++ b/radicale/tests/test_hook_email.py @@ -93,16 +93,12 @@ permissions: RrWw""") assert "VEVENT" in answer assert "Event" in answer assert "UID:event" in answer - found = 0 - for line in caplog.messages: - if line.find("notification_item: {'type': 'upsert'") != -1: - found = found | 1 - if line.find("to_addresses=['janedoe@example.com']") != -1: - found = found | 2 - if line.find("to_addresses=['johndoe@example.com']") != -1: - found = found | 4 - if (found != 7): - raise ValueError("Logging misses expected log lines, found=%d", found) + + logs = caplog.messages + # Should have a log saying the notification item was received + assert len([log for log in logs if "received notification_item: {'type': 'upsert'," in log]) == 1 + # Should NOT have a log saying that no email is sent (email won't actually be sent due to dryrun) + assert len([log for log in logs if "skipping notification for event: event1" in log]) == 0 def test_add_event_with_past_end_date(self, caplog) -> None: caplog.set_level(logging.WARNING) @@ -119,8 +115,11 @@ permissions: RrWw""") assert "Event" in answer assert "UID:event" in answer - # Should not trigger an email - assert len(caplog.messages) == 0 + logs = caplog.messages + # Should have a log saying the notification item was received + assert len([log for log in logs if "received notification_item: {'type': 'upsert'," in log]) == 1 + # Should have a log saying that no email is sent due to past end date + assert len([log for log in logs if "Event end time is in the past, skipping notification for event: event1" in log]) == 1 def test_delete_event_with_future_end_date(self, caplog) -> None: caplog.set_level(logging.WARNING) @@ -134,16 +133,12 @@ permissions: RrWw""") assert responses[path] == 200 _, answer = self.get("/calendar.ics/") assert "VEVENT" not in answer - found = 0 - for line in caplog.messages: - if line.find("notification_item: {'type': 'delete'") != -1: - found = found | 1 - if line.find("to_addresses=['janedoe@example.com']") != -1: - found = found | 2 - if line.find("to_addresses=['johndoe@example.com']") != -1: - found = found | 4 - if (found != 7): - raise ValueError("Logging misses expected log lines, found=%d", found) + + logs = caplog.messages + # Should have a log saying the notification item was received + assert len([log for log in logs if "received notification_item: {'type': 'delete'," in log]) == 1 + # Should NOT have a log saying that no email is sent (email won't actually be sent due to dryrun) + assert len([log for log in logs if "skipping notification for event: event1" in log]) == 0 def test_delete_event_with_past_end_date(self, caplog) -> None: caplog.set_level(logging.WARNING) @@ -158,5 +153,8 @@ permissions: RrWw""") _, answer = self.get("/calendar.ics/") assert "VEVENT" not in answer - # Should not trigger an email - assert len(caplog.messages) == 0 + logs = caplog.messages + # Should have a log saying the notification item was received + assert len([log for log in logs if "received notification_item: {'type': 'delete'," in log]) == 1 + # Should have a log saying that no email is sent due to past end date + assert len([log for log in logs if "Event end time is in the past, skipping notification for event: event1" in log]) == 1