From 937a1d59c592e2effc4d1131a06897df19ee8cfa Mon Sep 17 00:00:00 2001
From: Antoine Lambert <anlambert@softwareheritage.org>
Date: Mon, 21 Feb 2022 17:38:13 +0100
Subject: [PATCH] common/swh_templatetags: Fix parsing issue in
 urlize_header_links

If one of the URL links to parse contains a comma character, the parsing
was incorrect.

So fix that issue by updating the regexp use to format the links for
display in the API Web UI.

Related to T3967
---
 swh/web/common/swh_templatetags.py        | 12 ++++-------
 swh/web/tests/common/test_templatetags.py | 26 +++++++++++++++++------
 2 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/swh/web/common/swh_templatetags.py b/swh/web/common/swh_templatetags.py
index 1edbd6327..627b20503 100644
--- a/swh/web/common/swh_templatetags.py
+++ b/swh/web/common/swh_templatetags.py
@@ -57,14 +57,10 @@ def urlize_header_links(text):
         The text as is otherwise.
 
     """
-    links = text.split(",")
-    ret = ""
-    for i, link in enumerate(links):
-        ret += re.sub(r"<(http.*)>", r'<<a href="\1">\1</a>>', link)
-        # add one link per line and align them
-        if i != len(links) - 1:
-            ret += "\n     "
-    return ret
+    ret = re.sub(
+        r'<(http[^<>]+)>; rel="([^,]+)"', r'<<a href="\1">\1</a>>; rel="\2"\n', text
+    ).replace("\n,", "\n")
+    return ret[:-1]
 
 
 @register.filter
diff --git a/swh/web/tests/common/test_templatetags.py b/swh/web/tests/common/test_templatetags.py
index 1e1e99e32..a3f600252 100644
--- a/swh/web/tests/common/test_templatetags.py
+++ b/swh/web/tests/common/test_templatetags.py
@@ -3,6 +3,9 @@
 # License: GNU Affero General Public License version 3, or any later version
 # See top-level LICENSE file for more information
 
+import pytest
+
+from swh.web.api.apiresponse import compute_link_header
 from swh.web.common.swh_templatetags import (
     docstring_display,
     urlize_header_links,
@@ -24,19 +27,30 @@ def test_urlize_email():
     assert urlize_links_and_mails(email) == expected_content
 
 
-def test_urlize_header_links():
+@pytest.mark.parametrize(
+    "next_link, prev_link",
+    [
+        ("https://example.org/api/1/abc/", "https://example.org/api/1/def/"),
+        ("https://example.org/api/1/0,5/", "https://example.org/api/1/5,10/"),
+    ],
+)
+def test_urlize_header_links(next_link, prev_link):
 
-    next_link = "https://example.com/api/1/abc/"
-    prev_link = "https://example.com/api/1/def/"
+    link_header = f'<{next_link}>; rel="next",<{prev_link}>; rel="previous"'
 
-    content = f'<{next_link}>; rel="next"\n<{prev_link}>; rel="prev"'
+    assert (
+        link_header
+        == compute_link_header(
+            {"headers": {"link-next": next_link, "link-prev": prev_link}}, options={}
+        )["Link"]
+    )
 
     expected_content = (
         f'<<a href="{next_link}">{next_link}</a>>; rel="next"\n'
-        f'<<a href="{prev_link}">{prev_link}</a>>; rel="prev"'
+        f'<<a href="{prev_link}">{prev_link}</a>>; rel="previous"'
     )
 
-    assert urlize_header_links(content) == expected_content
+    assert urlize_header_links(link_header) == expected_content
 
 
 def test_docstring_display():
-- 
GitLab