Skip to content

Commit b5f5455

Browse files
authored
fix: ensure the context renders correctly for draftsharing (#1361)
We want to avoid a DisallowedHost so we don't end up sending the 400 Page to Smartling, which will be treated as "no context"
1 parent 184b553 commit b5f5455

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

springfield/cms/tests/test_callbacks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ def test_visual_context__for_page__with_no_revision(mock_capture_message, client
123123
mock_capture_message.assert_called_once_with(f"Unable to get a latest_revision for {page} so unable to send visual context.")
124124

125125

126+
@override_settings(WAGTAILADMIN_BASE_URL="https://cms.example.com")
127+
@mock.patch("springfield.cms.wagtail_localize_smartling.callbacks.SharingLinkView.as_view")
128+
@mock.patch("springfield.cms.wagtail_localize_smartling.callbacks.RequestFactory")
129+
def test__get_html_for_sharing_link__uses_cms_hostname_and_path_only(mock_factory_cls, mock_as_view):
130+
# sharing_link.url is a full URL — we should extract just the path and use
131+
# the CMS hostname as SERVER_NAME so make_preview_request doesn't fall back
132+
# to 'localhost' / 'testserver' and return a 400 error page.
133+
mock_response = mock.Mock()
134+
mock_response.text = "<html><body>page content</body></html>"
135+
mock_as_view.return_value = mock.Mock(return_value=mock_response)
136+
137+
sharing_link = mock.Mock()
138+
sharing_link.url = "http://localhost/_internal_draft_preview/abc123/"
139+
140+
result = _get_html_for_sharing_link(sharing_link)
141+
142+
mock_factory_cls.assert_called_once_with(SERVER_NAME="cms.example.com")
143+
mock_factory_cls.return_value.get.assert_called_once_with("/_internal_draft_preview/abc123/")
144+
assert result == "<html><body>page content</body></html>"
145+
146+
126147
# The happy path is implicitly tested in test_visual_context__*, above
127148
@mock.patch("springfield.cms.wagtail_localize_smartling.callbacks.capture_exception")
128149
@mock.patch("springfield.cms.wagtail_localize_smartling.callbacks.SharingLinkView.as_view")

springfield/cms/wagtail_localize_smartling/callbacks.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# provided and what outputs are needed
1111
import logging
1212
from typing import TYPE_CHECKING
13+
from urllib.parse import urlparse
1314

1415
from django.conf import settings
1516
from django.test import RequestFactory
@@ -27,7 +28,14 @@
2728

2829

2930
def _get_html_for_sharing_link(sharing_link: WagtaildraftsharingLink) -> str:
30-
request = RequestFactory().get(sharing_link.url)
31+
# Use the CMS hostname (guaranteed to be in ALLOWED_HOSTS) so that
32+
# Wagtail's make_preview_request doesn't fall back to 'testserver' /
33+
# 'localhost', which would cause DisallowedHost inside the middleware
34+
# chain and return a 400 error page instead of the real page HTML.
35+
# Also extract just the path from sharing_link.url in case it's absolute.
36+
cms_hostname = urlparse(settings.WAGTAILADMIN_BASE_URL).hostname
37+
path = urlparse(sharing_link.url).path
38+
request = RequestFactory(SERVER_NAME=cms_hostname).get(path)
3139
view_func = SharingLinkView.as_view()
3240
try:
3341
resp = view_func(

0 commit comments

Comments
 (0)