diff --git a/engine/apps/public_api/tests/test_incidents.py b/engine/apps/public_api/tests/test_incidents.py index 360a5e5e..73685c25 100644 --- a/engine/apps/public_api/tests/test_incidents.py +++ b/engine/apps/public_api/tests/test_incidents.py @@ -1,4 +1,5 @@ from unittest import mock +from unittest.mock import patch import pytest from django.urls import reverse @@ -186,6 +187,24 @@ def test_delete_incident_invalid_request(incident_public_api_setup): assert response.status_code == status.HTTP_400_BAD_REQUEST +@pytest.mark.django_db +def test_pagination(settings, incident_public_api_setup): + settings.BASE_URL = "https://test.com/test/prefixed/urls" + + token, incidents, _, _ = incident_public_api_setup + client = APIClient() + + url = reverse("api-public:alert_groups-list") + + with patch("common.api_helpers.paginators.PathPrefixedPagination.get_page_size", return_value=1): + response = client.get(url, HTTP_AUTHORIZATION=f"{token}") + + assert response.status_code == status.HTTP_200_OK + result = response.json() + + assert result["next"].startswith("https://test.com/test/prefixed/urls") + + # This is test from old django-based tests # TODO: uncomment with date checking in delete mode # def test_delete_incident_invalid_date(self): diff --git a/engine/common/api_helpers/paginators.py b/engine/common/api_helpers/paginators.py index 01ce2cc6..2a3ad974 100644 --- a/engine/common/api_helpers/paginators.py +++ b/engine/common/api_helpers/paginators.py @@ -1,19 +1,33 @@ from rest_framework.pagination import CursorPagination, PageNumberPagination +from common.api_helpers.utils import create_engine_url -class HundredPageSizePaginator(PageNumberPagination): + +class PathPrefixedPagination(PageNumberPagination): + def paginate_queryset(self, queryset, request, view=None): + request.build_absolute_uri = lambda: create_engine_url(request.get_full_path()) + return super().paginate_queryset(queryset, request, view) + + +class PathPrefixedCursorPagination(CursorPagination): + def paginate_queryset(self, queryset, request, view=None): + request.build_absolute_uri = lambda: create_engine_url(request.get_full_path()) + return super().paginate_queryset(queryset, request, view) + + +class HundredPageSizePaginator(PathPrefixedPagination): page_size = 100 -class FiftyPageSizePaginator(PageNumberPagination): +class FiftyPageSizePaginator(PathPrefixedPagination): page_size = 50 -class TwentyFivePageSizePaginator(PageNumberPagination): +class TwentyFivePageSizePaginator(PathPrefixedPagination): page_size = 25 -class TwentyFiveCursorPaginator(CursorPagination): +class TwentyFiveCursorPaginator(PathPrefixedCursorPagination): page_size = 25 max_page_size = 100 page_size_query_param = "perpage"