44
55from unittest import mock
66
7+ from django .test import override_settings
8+
79import pytest
810import wagtail_factories
911from wagtail .models import Page
1517
1618
1719@pytest .mark .django_db
20+ @override_settings (WAGTAILADMIN_BASE_URL = "https://cms.example.com" )
1821def test_visual_context__for_page (client ):
1922 top_level_page = SimpleRichTextPageFactory ()
2023
@@ -38,9 +41,41 @@ def test_visual_context__for_page(client):
3841 # light checks because we're not testing wagtaildraftsharing itself
3942 assert "<body" in html
4043 assert "Test SimpleRichTextPage" in html
44+ assert '<base href="https://www.mozilla.org/">' in html
4145
4246 sharing_link_key = WagtaildraftsharingLink .objects .get ().key
43- assert url == f"http://testserver:81/_internal_draft_preview/{ sharing_link_key } /"
47+ assert url == f"https://cms.example.com/_internal_draft_preview/{ sharing_link_key } /"
48+
49+
50+ @pytest .mark .django_db
51+ @override_settings (WAGTAILADMIN_BASE_URL = "https://cms.example.com" )
52+ @mock .patch ("bedrock.cms.wagtail_localize_smartling.callbacks._get_html_for_sharing_link" )
53+ def test_visual_context__cms_hostname_stripped_and_base_tag_injected (mock_get_html , client ):
54+ top_level_page = SimpleRichTextPageFactory ()
55+ page = SimpleRichTextPageFactory (parent = top_level_page , slug = "visual-context-text-page" )
56+ page .save_revision ()
57+
58+ site = wagtail_factories .SiteFactory (
59+ root_page = top_level_page ,
60+ is_default_site = True ,
61+ hostname = "cms-internal.example.com" ,
62+ port = 8080 ,
63+ )
64+ cms_root_url = site .root_url # e.g. "http://cms-internal.example.com:8080"
65+
66+ mock_get_html .return_value = f'<html><head></head><body><img src="{ cms_root_url } /media/image.jpg"></body></html>'
67+
68+ user = WagtailUserFactory ()
69+ mock_job = mock .Mock ()
70+ mock_job .translation_source .get_source_instance .return_value = page
71+ mock_job .user = user
72+
73+ _ , html = visual_context (smartling_job = mock_job )
74+
75+ assert cms_root_url not in html
76+ assert "https://cms.example.com" not in html
77+ assert '<base href="https://www.mozilla.org/">' in html
78+ assert '<img src="/media/image.jpg">' in html
4479
4580
4681def test_visual_context__for_inviable_object (client ):
@@ -88,6 +123,27 @@ def test_visual_context__for_page__with_no_revision(mock_capture_message, client
88123 mock_capture_message .assert_called_once_with (f"Unable to get a latest_revision for { page } so unable to send visual context." )
89124
90125
126+ @override_settings (WAGTAILADMIN_BASE_URL = "https://cms.example.com" )
127+ @mock .patch ("bedrock.cms.wagtail_localize_smartling.callbacks.SharingLinkView.as_view" )
128+ @mock .patch ("bedrock.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+
91147# The happy path is implicitly tested in test_visual_context__*, above
92148@mock .patch ("bedrock.cms.wagtail_localize_smartling.callbacks.capture_exception" )
93149@mock .patch ("bedrock.cms.wagtail_localize_smartling.callbacks.SharingLinkView.as_view" )
0 commit comments