Skip to content

Commit

Permalink
feat: add unit tests for recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Swojak-A committed Mar 29, 2023
1 parent 2c7631a commit e112a04
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions wagtail/documents/tests/test_serializers.py
@@ -0,0 +1,67 @@
from urllib.parse import urlparse

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
)

def _assert_valid_url(self, url):
# NOTE: this assertion will not catch double protocol scheme insertion
parsed_url = urlparse(url)
if not all((parsed_url.scheme, parsed_url.netloc)):
return False
return True

@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"]
self.assertTrue(self._assert_valid_url(download_url))
self.assertEqual(download_url.count("http"), 1)

@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.assertTrue(self._assert_valid_url(download_url))
self.assertEqual(download_url.count("http"), 1)

0 comments on commit e112a04

Please sign in to comment.