diff --git a/engine/apps/alerts/tasks/notify_ical_schedule_shift.py b/engine/apps/alerts/tasks/notify_ical_schedule_shift.py index 1a261880..a23f2a4b 100644 --- a/engine/apps/alerts/tasks/notify_ical_schedule_shift.py +++ b/engine/apps/alerts/tasks/notify_ical_schedule_shift.py @@ -308,7 +308,7 @@ def notify_ical_schedule_shift(schedule_pk): new_shifts = sorted(new_shifts, key=lambda shift: shift["start"]) if len(new_shifts) != 0: - days_to_lookup = (new_shifts[-1]["end"].date() - now.date()).days + days_to_lookup = (new_shifts[-1]["end"].date() - now.date()).days + 1 days_to_lookup = max([days_to_lookup, MIN_DAYS_TO_LOOKUP_FOR_THE_END_OF_EVENT]) else: days_to_lookup = MIN_DAYS_TO_LOOKUP_FOR_THE_END_OF_EVENT diff --git a/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py b/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py index d6cb6398..eae19ef3 100644 --- a/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py +++ b/engine/apps/alerts/tests/test_notify_ical_schedule_shift.py @@ -1,4 +1,9 @@ +from datetime import datetime +from unittest.mock import Mock, patch + import pytest +import pytz +from django.utils import timezone from apps.alerts.tasks.notify_ical_schedule_shift import notify_ical_schedule_shift from apps.schedules.models import OnCallScheduleICal @@ -9,32 +14,35 @@ PRODID:-//Google Inc//Google Calendar 70.9054//EN VERSION:2.0 CALSCALE:GREGORIAN METHOD:PUBLISH -X-WR-CALNAME:t -X-WR-TIMEZONE:Asia/Yekaterinburg -BEGIN:VTIMEZONE -TZID:Asia/Yekaterinburg -X-LIC-LOCATION:Asia/Yekaterinburg -BEGIN:STANDARD -TZOFFSETFROM:+0500 -TZOFFSETTO:+0500 -TZNAME:+05 -DTSTART:19700101T000000 -END:STANDARD -END:VTIMEZONE BEGIN:VEVENT -DTSTART;TZID=Asia/Yekaterinburg:20210124T130000 -DTEND;TZID=Asia/Yekaterinburg:20210124T220000 -RRULE:FREQ=DAILY -DTSTAMP:20210127T143634Z -UID:0i0af8p6p8vfampe3r1vkog0jg@google.com -CREATED:20210127T143553Z +DTSTART;VALUE=DATE:20211005 +DTEND;VALUE=DATE:20211012 +RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=7;BYDAY=WE +DTSTAMP:20210930T125523Z +UID:id1@google.com +CREATED:20210928T202349Z DESCRIPTION: -LAST-MODIFIED:20210127T143553Z +LAST-MODIFIED:20210929T204751Z LOCATION: -SEQUENCE:0 +SEQUENCE:1 STATUS:CONFIRMED -SUMMARY:@Bernard Desruisseaux -TRANSP:OPAQUE +SUMMARY:user1 +TRANSP:TRANSPARENT +END:VEVENT +BEGIN:VEVENT +DTSTART;VALUE=DATE:20210928 +DTEND;VALUE=DATE:20211005 +RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=7;BYDAY=WE +DTSTAMP:20210930T125523Z +UID:id2@google.com +CREATED:20210928T202331Z +DESCRIPTION: +LAST-MODIFIED:20210929T204744Z +LOCATION: +SEQUENCE:2 +STATUS:CONFIRMED +SUMMARY:user2 +TRANSP:TRANSPARENT END:VEVENT END:VCALENDAR """ @@ -61,3 +69,36 @@ def test_current_overrides_ical_schedule_is_none( # this should not raise notify_ical_schedule_shift(ical_schedule.oncallschedule_ptr_id) + + +@pytest.mark.django_db +def test_next_shift_notification_long_shifts( + make_organization_and_user_with_slack_identities, + make_schedule, + make_user, +): + organization, _, _, _ = make_organization_and_user_with_slack_identities() + make_user(organization=organization, username="user1") + make_user(organization=organization, username="user2") + + ical_schedule = make_schedule( + organization, + schedule_class=OnCallScheduleICal, + name="test_ical_schedule", + channel="channel", + ical_url_primary="url", + prev_ical_file_primary=ICAL_DATA, + cached_ical_file_primary=ICAL_DATA, + prev_ical_file_overrides=None, + cached_ical_file_overrides=None, + ) + + with patch.object(timezone, "datetime", Mock(wraps=timezone.datetime)) as mock_tz_datetime: + mock_tz_datetime.now.return_value = datetime(2021, 9, 29, 12, 0, tzinfo=pytz.UTC) + with patch("apps.slack.slack_client.SlackClientWithErrorHandling.api_call") as mock_slack_api_call: + notify_ical_schedule_shift(ical_schedule.oncallschedule_ptr_id) + + slack_blocks = mock_slack_api_call.call_args_list[0][1]["blocks"] + notification = slack_blocks[0]["text"]["text"] + assert "*New on-call shift:*\nuser2" in notification + assert "*Next on-call shift:*\nuser1" in notification