|
10 | 10 | from rest_framework.test import APIClient
|
11 | 11 |
|
12 | 12 | from apps.api.permissions import LegacyAccessControlRole
|
13 |
| -from apps.schedules.models import OnCallScheduleWeb, ShiftSwapRequest |
| 13 | +from apps.schedules.models import CustomOnCallShift, OnCallScheduleWeb, ShiftSwapRequest |
14 | 14 | from common.api_helpers.utils import serialize_datetime_as_utc_timestamp
|
15 | 15 | from common.insight_log import EntityEvent
|
16 | 16 |
|
@@ -466,6 +466,53 @@ def test_partial_update_time_related_fields(ssr_setup, make_user_auth_headers):
|
466 | 466 | assert response.json() == expected_response
|
467 | 467 |
|
468 | 468 |
|
| 469 | +@pytest.mark.django_db |
| 470 | +def test_related_shifts(ssr_setup, make_on_call_shift, make_user_auth_headers): |
| 471 | + ssr, beneficiary, token, _ = ssr_setup() |
| 472 | + |
| 473 | + schedule = ssr.schedule |
| 474 | + organization = schedule.organization |
| 475 | + user = beneficiary |
| 476 | + |
| 477 | + today = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) |
| 478 | + start = today + timezone.timedelta(days=2) |
| 479 | + duration = timezone.timedelta(hours=8) |
| 480 | + data = { |
| 481 | + "start": start, |
| 482 | + "rotation_start": start, |
| 483 | + "duration": duration, |
| 484 | + "priority_level": 1, |
| 485 | + "frequency": CustomOnCallShift.FREQUENCY_DAILY, |
| 486 | + "schedule": schedule, |
| 487 | + } |
| 488 | + on_call_shift = make_on_call_shift( |
| 489 | + organization=organization, shift_type=CustomOnCallShift.TYPE_ROLLING_USERS_EVENT, **data |
| 490 | + ) |
| 491 | + on_call_shift.add_rolling_users([[user]]) |
| 492 | + |
| 493 | + client = APIClient() |
| 494 | + url = reverse("api-internal:shift_swap-shifts", kwargs={"pk": ssr.public_primary_key}) |
| 495 | + auth_headers = make_user_auth_headers(beneficiary, token) |
| 496 | + response = client.get(url, **auth_headers) |
| 497 | + |
| 498 | + assert response.status_code == status.HTTP_200_OK |
| 499 | + response_json = response.json() |
| 500 | + expected = [ |
| 501 | + # start, end, user, swap request ID |
| 502 | + ( |
| 503 | + start.strftime("%Y-%m-%dT%H:%M:%SZ"), |
| 504 | + (start + duration).strftime("%Y-%m-%dT%H:%M:%SZ"), |
| 505 | + user.public_primary_key, |
| 506 | + ssr.public_primary_key, |
| 507 | + ), |
| 508 | + ] |
| 509 | + returned_events = [ |
| 510 | + (e["start"], e["end"], e["users"][0]["pk"], e["users"][0]["swap_request"]["pk"]) |
| 511 | + for e in response_json["events"] |
| 512 | + ] |
| 513 | + assert returned_events == expected |
| 514 | + |
| 515 | + |
469 | 516 | @pytest.mark.django_db
|
470 | 517 | @pytest.mark.parametrize(
|
471 | 518 | "role,expected_status",
|
@@ -714,3 +761,28 @@ def test_take_permissions(
|
714 | 761 |
|
715 | 762 | response = client.post(url, format="json", **make_user_auth_headers(benefactor, token))
|
716 | 763 | assert response.status_code == expected_status
|
| 764 | + |
| 765 | + |
| 766 | +@patch("apps.api.views.shift_swap.ShiftSwapViewSet.shifts", return_value=mock_success_response) |
| 767 | +@pytest.mark.django_db |
| 768 | +@pytest.mark.parametrize( |
| 769 | + "role,expected_status", |
| 770 | + [ |
| 771 | + (LegacyAccessControlRole.ADMIN, status.HTTP_200_OK), |
| 772 | + (LegacyAccessControlRole.EDITOR, status.HTTP_200_OK), |
| 773 | + (LegacyAccessControlRole.VIEWER, status.HTTP_200_OK), |
| 774 | + ], |
| 775 | +) |
| 776 | +def test_list_shifts_permissions( |
| 777 | + mock_endpoint_handler, |
| 778 | + ssr_setup, |
| 779 | + make_user_auth_headers, |
| 780 | + role, |
| 781 | + expected_status, |
| 782 | +): |
| 783 | + ssr, beneficiary, token, _ = ssr_setup(beneficiary_role=role) |
| 784 | + client = APIClient() |
| 785 | + url = reverse("api-internal:shift_swap-shifts", kwargs={"pk": ssr.public_primary_key}) |
| 786 | + |
| 787 | + response = client.get(url, format="json", **make_user_auth_headers(beneficiary, token)) |
| 788 | + assert response.status_code == expected_status |
0 commit comments