From 2a46152477568a2cefd0f1b0a157b1d0e78066f3 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Fri, 9 Feb 2024 17:36:12 -0300 Subject: [PATCH] Include all upcoming shifts in upcoming shifts internal endpoint (#3871) Related to https://github.com/grafana/oncall/issues/3859 --- CHANGELOG.md | 1 + engine/apps/api/tests/test_user.py | 14 +++++++++++++- engine/apps/api/views/user.py | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e51f8065..d1a42a7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improved zvonok verification call @sreway ([#3768](https://github.com/grafana/oncall/pull/3768)) - Add permission checks for Slack paging and shift swaps actions ([#3861](https://github.com/grafana/oncall/pull/3861)) +- Include all upcoming shifts in upcoming shifts internal endpoint ([#3871](https://github.com/grafana/oncall/pull/3871)) ### Changed diff --git a/engine/apps/api/tests/test_user.py b/engine/apps/api/tests/test_user.py index 9aa6bf66..f286ffe8 100644 --- a/engine/apps/api/tests/test_user.py +++ b/engine/apps/api/tests/test_user.py @@ -12,6 +12,7 @@ from rest_framework.test import APIClient from apps.api.permissions import GrafanaAPIPermission, LegacyAccessControlRole, RBACPermission from apps.api.serializers.user import UserHiddenFieldsSerializer +from apps.api.views.user import UPCOMING_SHIFTS_DEFAULT_DAYS from apps.base.models import UserNotificationPolicy from apps.phone_notifications.exceptions import FailedToFinishVerification from apps.schedules.models import CustomOnCallShift, OnCallScheduleWeb @@ -1918,6 +1919,9 @@ def test_upcoming_shifts_oncall( assert returned_data["current_shift"]["start"] == on_call_shift.start next_shift_start = on_call_shift.start + timezone.timedelta(days=1) assert returned_data["next_shift"]["start"] == next_shift_start + assert len(returned_data["upcoming_shifts"]) == UPCOMING_SHIFTS_DEFAULT_DAYS + for i, shift in enumerate(returned_data["upcoming_shifts"], start=1): + assert shift["start"] == on_call_shift.start + timezone.timedelta(days=i) # empty response for other user url = reverse("api-internal:user-upcoming-shifts", kwargs={"pk": other_user.public_primary_key}) @@ -1971,6 +1975,7 @@ def test_upcoming_shifts_override( assert returned_data["is_oncall"] is False assert returned_data["current_shift"] is None assert returned_data["next_shift"]["start"] == override.start + assert returned_data["upcoming_shifts"] == [returned_data["next_shift"]] @pytest.mark.django_db @@ -2018,7 +2023,8 @@ def test_upcoming_shifts_multiple_schedules( client = APIClient() url = reverse("api-internal:user-upcoming-shifts", kwargs={"pk": admin.public_primary_key}) - response = client.get(url, format="json", **make_user_auth_headers(admin, token)) + num_days = 2 + response = client.get(url + f"?days={num_days}", format="json", **make_user_auth_headers(admin, token)) assert response.status_code == status.HTTP_200_OK returned_data = response.data @@ -2029,10 +2035,16 @@ def test_upcoming_shifts_multiple_schedules( assert returned_data[i]["is_oncall"] assert returned_data[i]["current_shift"]["start"] == expected_start assert returned_data[i]["next_shift"]["start"] == expected_start + timezone.timedelta(days=1) + assert len(returned_data[i]["upcoming_shifts"]) == 2 + for delta, shift in enumerate(returned_data[i]["upcoming_shifts"], start=1): + assert shift["start"] == expected_start + timezone.timedelta(days=delta) else: assert returned_data[i]["is_oncall"] is False assert returned_data[i]["current_shift"] is None assert returned_data[i]["next_shift"]["start"] == expected_start + assert len(returned_data[i]["upcoming_shifts"]) == num_days - i + 1 + for delta, shift in enumerate(returned_data[i]["upcoming_shifts"], start=0): + assert shift["start"] == expected_start + timezone.timedelta(days=delta) @pytest.mark.django_db diff --git a/engine/apps/api/views/user.py b/engine/apps/api/views/user.py index 47aa0244..8c1715cd 100644 --- a/engine/apps/api/views/user.py +++ b/engine/apps/api/views/user.py @@ -746,6 +746,7 @@ class UserView( "is_oncall": len(current_shifts) > 0, "current_shift": current_shifts[0] if current_shifts else None, "next_shift": upcoming_shifts[0] if upcoming_shifts else None, + "upcoming_shifts": upcoming_shifts or None, } )