From e5885f674b35e1c03630ec4e4fdfabe5d1146699 Mon Sep 17 00:00:00 2001 From: Beverly Nguyen Date: Tue, 29 Jul 2025 10:25:43 -0700 Subject: [PATCH] Fixed pagination logic to prevent infinite next button --- app/main/views/activity.py | 4 +++- app/main/views/jobs.py | 3 +++ tests/app/main/views/test_activity.py | 31 ++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/main/views/activity.py b/app/main/views/activity.py index 9dd60b35b..1d6d12d98 100644 --- a/app/main/views/activity.py +++ b/app/main/views/activity.py @@ -97,10 +97,12 @@ def handle_pagination(jobs, service_id, page): else None ) total_items = jobs.get("total", 0) + page_size = jobs.get("page_size", 50) + total_pages = (total_items + page_size - 1) // page_size has_next_link = jobs.get("links", {}).get("next") is not None next_page = ( generate_next_dict("main.all_jobs_activity", service_id, page) - if has_next_link and total_items > 50 + if has_next_link and total_items > 50 and page < total_pages else None ) pagination = generate_pagination_pages( diff --git a/app/main/views/jobs.py b/app/main/views/jobs.py index 938a0849d..afe0504bf 100644 --- a/app/main/views/jobs.py +++ b/app/main/views/jobs.py @@ -254,10 +254,13 @@ def get_notifications(service_id, message_type, status_override=None): # noqa next_page = None total_items = notifications.get("total", 0) + page_size = notifications.get("page_size", 50) + total_pages = (total_items + page_size - 1) // page_size if ( "links" in notifications and notifications["links"].get("next", None) and total_items > 50 + and page < total_pages ): next_page = generate_next_dict( "main.view_notifications", service_id, page, url_args diff --git a/tests/app/main/views/test_activity.py b/tests/app/main/views/test_activity.py index 9fd5118db..b9dc3d94a 100644 --- a/tests/app/main/views/test_activity.py +++ b/tests/app/main/views/test_activity.py @@ -562,7 +562,7 @@ def test_should_show_notifications_for_a_service_with_next_previous( "app.notification_api_client.get_notifications_for_service", return_value=notification_json( service_one["id"], rows=50, with_links=True - ) | {"total": 100}, + ) | {"total": 150}, ) page = client_request.get( "main.view_notifications", @@ -597,6 +597,35 @@ def test_should_show_notifications_for_a_service_with_next_previous( assert "page 1" in prev_page_link.text.strip() +def test_doesnt_show_next_button_on_last_page( + client_request, + service_one, + active_user_with_permissions, + mock_get_service_statistics, + mock_get_service_data_retention, + mock_get_no_api_keys, + mocker, +): + mocker.patch( + "app.notification_api_client.get_notifications_for_service", + return_value=notification_json( + service_one["id"], rows=50, with_links=True + ) | {"total": 100}, + ) + page = client_request.get( + "main.view_notifications", + service_id=service_one["id"], + message_type="sms", + page=2, + ) + + next_page_link = page.find("a", {"rel": "next"}) + prev_page_link = page.find("a", {"rel": "previous"}) + + assert next_page_link is None + assert prev_page_link is not None + + def test_doesnt_show_pagination_when_50_or_fewer_items( client_request, service_one,