Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1185 from coderbydesign/fix-version…
Browse files Browse the repository at this point in the history
…-pagination

Ensure paginated links in v2 APIs are supported/consistent with v1
  • Loading branch information
coderbydesign authored Sep 6, 2024
2 parents d8051fd + 4ffe9d1 commit 3663016
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
19 changes: 8 additions & 11 deletions rbac/api/common/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

"""Common pagination class."""
import logging
import re
from urllib.parse import urlparse

from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
from rest_framework.utils.urls import replace_query_param

from api import API_VERSION

PATH_INFO = "PATH_INFO"
logger = logging.getLogger(__name__) # pylint: disable=invalid-name

Expand All @@ -38,16 +38,13 @@ class StandardResultsSetPagination(LimitOffsetPagination):
def link_rewrite(request, link):
"""Rewrite the link based on the path header to only provide partial url."""
url = link
version = "v{}/".format(API_VERSION)
if PATH_INFO in request.META:
try:
local_api_index = link.index(version)
path = request.META.get(PATH_INFO)
path_api_index = path.index(version)
path_link = "{}{}"
url = path_link.format(path[:path_api_index], link[local_api_index:])
except ValueError:
logger.warning('Unable to rewrite link as "{}" was not found.'.format(version))
url_components = urlparse(link)
path_and_query = url_components.path + (f"?{url_components.query}" if url_components.query else "")
if bool(re.search("/v[0-9]/", path_and_query)):
url = path_and_query
else:
logger.warning(f"Unable to rewrite link as no version was not found in {path_and_query}.")
return url

def get_first_link(self):
Expand Down
18 changes: 18 additions & 0 deletions tests/api/common/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ def test_link_rewrite(self):
result = StandardResultsSetPagination.link_rewrite(request, link)
self.assertEqual(expected, result)

def test_link_rewrite_rbac_prefix(self):
"""Test the link rewrite for RBAC prefix."""
request = Mock()
request.META = {PATH_INFO: "/api/rbac/v1/roles/"}
link = "http://localhost:8000/api/rbac/v1/roles/?limit=10&offset=0"
expected = "/api/rbac/v1/roles/?limit=10&offset=0"
result = StandardResultsSetPagination.link_rewrite(request, link)
self.assertEqual(expected, result)

def test_link_rewrite_rbac_prefix_v2(self):
"""Test the link rewrite for RBAC prefix for v2 APIs."""
request = Mock()
request.META = {PATH_INFO: "/api/rbac/v2/workspaces/"}
link = "http://localhost:8000/api/rbac/v2/workspaces/?limit=10&offset=0"
expected = "/api/rbac/v2/workspaces/?limit=10&offset=0"
result = StandardResultsSetPagination.link_rewrite(request, link)
self.assertEqual(expected, result)

def test_link_rewrite_err(self):
"""Test the link rewrite."""
request = Mock()
Expand Down

0 comments on commit 3663016

Please sign in to comment.