Skip to content

Commit

Permalink
fix: modify get_full_url to omit adding base_url in inaplicable cases (
Browse files Browse the repository at this point in the history
  • Loading branch information
Swojak-A authored and gasman committed Apr 18, 2023
1 parent cc223da commit 0447b25
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -76,6 +76,7 @@ Changelog
* Fix: Avoid showing scrollbars in the block picker unless necessary (Babitha Kumari)
* Fix: Always show Add buttons, guide lines, Move up/down, Duplicate, Delete; in StreamField and Inline Panel (Thibaud Colas)
* Fix: Make admin JS i18n endpoint accessible to non-authenticated users (Matt Westcott)
* Fix: Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)
* Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
* Docs: Add code block to make it easier to understand contribution docs (Suyash Singh)
* Docs: Add new "Icons" page for icons customisation and reuse across the admin interface (Coen van der Kamp, Thibaud Colas)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Expand Up @@ -706,6 +706,7 @@ Contributors
* Mansi Gundre
* Hanoon
* Steve Steinwand
* Swojak-A

Translators
===========
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.0.md
Expand Up @@ -118,6 +118,7 @@ Those improvements were implemented by Albina Starykova as part of an [Outreachy
* Always show Add buttons, guide lines, Move up/down, Duplicate, Delete; in StreamField and Inline Panel (Thibaud Colas)
* Make admin JS i18n endpoint accessible to non-authenticated users (Matt Westcott)
* Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
* Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)

### Documentation

Expand Down
2 changes: 2 additions & 0 deletions wagtail/api/v2/utils.py
Expand Up @@ -27,6 +27,8 @@ def get_base_url(request=None):


def get_full_url(request, path):
if path.startswith(("http://", "https://")):
return path
base_url = get_base_url(request) or ""
return base_url + path

Expand Down
59 changes: 59 additions & 0 deletions wagtail/documents/tests/test_serializers.py
@@ -0,0 +1,59 @@
from django.core.files.base import ContentFile
from django.test import TestCase
from django.test.utils import override_settings
from django.urls import reverse

from wagtail.documents import models


class TestCorrectDownloadUrlSerialization(TestCase):

"""Test asserts that in case of both `redirect` and `direct`
WAGTAILDOCS_SERVE_METHOD settings `download_url` field
is correctly serialized by DocumentDownloadUrlField."""

def setUp(self):
self.document = models.Document(title="Test document", file_hash="123456")
self.document.file.save("example.doc", ContentFile("A boring example document"))

def tearDown(self):
# delete the FieldFile directly because the TestCase does not commit
# transactions to trigger transaction.on_commit() in the signal handler
self.document.file.delete()

def get_response(self, document_id, **params):
return self.client.get(
reverse("wagtailapi_v2:documents:detail", args=(document_id,)), params
)

@override_settings(
WAGTAILDOCS_SERVE_METHOD="redirect",
DEFAULT_FILE_STORAGE="wagtail.test.dummy_external_storage.DummyExternalStorage",
WAGTAILAPI_BASE_URL="http://example.com/",
)
def test_serializer_wagtaildocs_serve_redirect(self):
response = self.get_response(self.document.id)
data = response.json()
self.assertIn("meta", data)
meta = data["meta"]
self.assertIn("download_url", meta)
download_url = meta["download_url"]
expected_url = "http://example.com/documents/%d/example.doc" % self.document.pk
self.assertEqual(download_url, expected_url)

@override_settings(
WAGTAILDOCS_SERVE_METHOD="direct",
DEFAULT_FILE_STORAGE="wagtail.test.dummy_external_storage.DummyExternalStorage",
MEDIA_URL="http://remotestorage.com/media/",
WAGTAILAPI_BASE_URL="http://example.com/",
)
def test_serializer_wagtaildocs_serve_direct(self):
response = self.get_response(self.document.id)
data = response.json()
self.assertIn("meta", data)
meta = data["meta"]
self.assertIn("download_url", meta)
download_url = meta["download_url"]
self.assertEqual(
download_url, "http://remotestorage.com/media/documents/example.doc"
)

0 comments on commit 0447b25

Please sign in to comment.