Permalink
Browse files

Correctly identify internal admin links on public hosts

  • Loading branch information...
1 parent 1c79712 commit 7ae0db819f545d5fa09206030e38b4d3d922aefd @tomstuart tomstuart committed Jan 31, 2012
Showing with 42 additions and 2 deletions.
  1. +6 −2 app/helpers/govspeak_helper.rb
  2. +36 −0 test/unit/helpers/govspeak_helper_test.rb
@@ -74,8 +74,8 @@ def is_internal_admin_link?(href)
end
return false unless %w(http https).include?(uri.scheme)
- truncated_link_uri = [uri.host, uri.path.split("/")[1,2]].join("/")
- truncated_host_uri = [request.host + Whitehall.router_prefix, "admin"].join("/")
+ truncated_link_uri = [normalise_host(uri.host), uri.path.split("/")[1,2]].join("/")
+ truncated_host_uri = [normalise_host(request.host) + Whitehall.router_prefix, "admin"].join("/")
truncated_link_uri == truncated_host_uri
end
@@ -112,4 +112,8 @@ def rewritten_href_for_documents(document, supporting_page)
public_document_url(document)
end
end
+
+ def normalise_host(host)
+ Whitehall.public_host_for(host) || host
+ end
end
@@ -200,6 +200,15 @@ class GovspeakHelperTest < ActionView::TestCase
assert_govspeak %{<p>this and <a href="#{public_url}">that</a> yeah?</p>}, html
end
+ test "should rewrite absolute links to admin previews of Speeches as their public document identity on public preview" do
+ request.host = "www.preview.alphagov.co.uk"
+ ActionController::Base.default_url_options[:host] = "whitehall.preview.alphagov.co.uk"
+ speech = create(:published_speech)
+ public_url = public_document_url(speech)
+ html = govspeak_to_html("this and [that](#{admin_speech_url(speech)}) yeah?")
+ assert_govspeak %{<p>this and <a href="#{public_url}">that</a> yeah?</p>}, html
+ end
+
test "should rewrite absolute links to admin previews of SupportingPages as their public document identity on preview" do
request.host = ActionController::Base.default_url_options[:host] = "whitehall.preview.alphagov.co.uk"
policy = create(:published_policy)
@@ -208,6 +217,15 @@ class GovspeakHelperTest < ActionView::TestCase
assert_govspeak %{<p>this and <a href="#{public_supporting_page_url(policy, supporting_page, host: "www.preview.alphagov.co.uk")}">that</a> yeah?</p>}, html
end
+ test "should rewrite absolute links to admin previews of SupportingPages as their public document identity on public preview" do
+ request.host ="www.preview.alphagov.co.uk"
+ ActionController::Base.default_url_options[:host] = "whitehall.preview.alphagov.co.uk"
+ policy = create(:published_policy)
+ supporting_page = create(:supporting_page, document: policy)
+ html = govspeak_to_html("this and [that](#{admin_supporting_page_url(supporting_page)}) yeah?")
+ assert_govspeak %{<p>this and <a href="#{public_supporting_page_url(policy, supporting_page, host: "www.preview.alphagov.co.uk")}">that</a> yeah?</p>}, html
+ end
+
test "should rewrite absolute links to admin previews of Speeches as their public document identity on production" do
request.host = ActionController::Base.default_url_options[:host] = "whitehall.production.alphagov.co.uk"
speech = create(:published_speech)
@@ -216,6 +234,15 @@ class GovspeakHelperTest < ActionView::TestCase
assert_govspeak %{<p>this and <a href="#{public_url}">that</a> yeah?</p>}, html
end
+ test "should rewrite absolute links to admin previews of Speeches as their public document identity on public production" do
+ request.host = "www.gov.uk"
+ ActionController::Base.default_url_options[:host] = "whitehall.production.alphagov.co.uk"
+ speech = create(:published_speech)
+ public_url = public_document_url(speech)
+ html = govspeak_to_html("this and [that](#{admin_speech_url(speech)}) yeah?")
+ assert_govspeak %{<p>this and <a href="#{public_url}">that</a> yeah?</p>}, html
+ end
+
test "should rewrite absolute links to admin previews of SupportingPages as their public document identity on production" do
request.host = ActionController::Base.default_url_options[:host] = "whitehall.production.alphagov.co.uk"
policy = create(:published_policy)
@@ -224,6 +251,15 @@ class GovspeakHelperTest < ActionView::TestCase
assert_govspeak %{<p>this and <a href="#{public_supporting_page_url(policy, supporting_page, host: "www.gov.uk")}">that</a> yeah?</p>}, html
end
+ test "should rewrite absolute links to admin previews of SupportingPages as their public document identity on public production" do
+ request.host = "www.gov.uk"
+ ActionController::Base.default_url_options[:host] = "whitehall.production.alphagov.co.uk"
+ policy = create(:published_policy)
+ supporting_page = create(:supporting_page, document: policy)
+ html = govspeak_to_html("this and [that](#{admin_supporting_page_url(supporting_page)}) yeah?")
+ assert_govspeak %{<p>this and <a href="#{public_supporting_page_url(policy, supporting_page, host: "www.gov.uk")}">that</a> yeah?</p>}, html
+ end
+
test "should not link to SupportingPages whose documents are not published" do
policy = create(:draft_policy)
supporting_page = create(:supporting_page, document: policy)

0 comments on commit 7ae0db8

Please sign in to comment.