From ce4a6b0412545d08940a420e25af3efc1470d662 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 10 Jun 2019 12:06:29 +0200
Subject: [PATCH] Fix #851: wrong og:image url when using S3 storage

---
 api/funkwhale_api/common/utils.py |  3 +++
 api/tests/common/test_utils.py    | 14 ++++++++++++++
 changes/changelog.d/851.bugfix    |  1 +
 3 files changed, 18 insertions(+)
 create mode 100644 changes/changelog.d/851.bugfix

diff --git a/api/funkwhale_api/common/utils.py b/api/funkwhale_api/common/utils.py
index 57bcba932f..7763e9b7f8 100644
--- a/api/funkwhale_api/common/utils.py
+++ b/api/funkwhale_api/common/utils.py
@@ -113,6 +113,9 @@ def chunk_queryset(source_qs, chunk_size):
 
 
 def join_url(start, end):
+    if end.startswith("http://") or end.startswith("https://"):
+        # alread a full URL, joining makes no sense
+        return end
     if start.endswith("/") and end.startswith("/"):
         return start + end[1:]
 
diff --git a/api/tests/common/test_utils.py b/api/tests/common/test_utils.py
index ea64ed9d28..74a3d0bca6 100644
--- a/api/tests/common/test_utils.py
+++ b/api/tests/common/test_utils.py
@@ -85,3 +85,17 @@ def test_get_updated_fields(conf, mock_args, data, expected, mocker):
     obj = mocker.Mock(**mock_args)
 
     assert utils.get_updated_fields(conf, data, obj) == expected
+
+
+@pytest.mark.parametrize(
+    "start, end, expected",
+    [
+        ("https://domain", "/api", "https://domain/api"),
+        ("https://domain/", "/api", "https://domain/api"),
+        ("https://domain", "api", "https://domain/api"),
+        ("https://domain", "https://api", "https://api"),
+        ("https://domain", "http://api", "http://api"),
+    ],
+)
+def test_join_url(start, end, expected):
+    assert utils.join_url(start, end) == expected
diff --git a/changes/changelog.d/851.bugfix b/changes/changelog.d/851.bugfix
new file mode 100644
index 0000000000..e6866b3e1f
--- /dev/null
+++ b/changes/changelog.d/851.bugfix
@@ -0,0 +1 @@
+Fixed wrong og:image url when using S3 storage (#851)
-- 
GitLab