Skip to content

Commit

Permalink
Add link to preview draft and published versions
Browse files Browse the repository at this point in the history
  • Loading branch information
syed-ali-tw committed Jul 26, 2024
1 parent ad57dd9 commit 792fd9d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
18 changes: 18 additions & 0 deletions app/helpers/paths_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ def preview_edition_path(edition)
path
end

def preview_homepage_path(edition)
path = "#{preview_url(edition)}" # rubocop:disable Style/RedundantInterpolation

if should_have_auth_bypass_id?(edition)
path << "?token=#{jwt_token(edition)}"
end

path
end

def view_homepage_path
gov_uk_root_url
end

def start_work_path(edition)
send("start_work_edition_path", edition)
end
Expand Down Expand Up @@ -49,6 +63,10 @@ def preview_url(_edition)
Plek.external_url_for("draft-origin")
end

def gov_uk_root_url
Plek.website_root
end

private

def should_have_auth_bypass_id?(edition)
Expand Down
18 changes: 14 additions & 4 deletions app/views/homepage/popular_links/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
padding: true
} %>
<% if @latest_popular_links.state == "published" %>
<%= primary_button_for(@latest_popular_links, create_popular_links_path, "Create new edition") %>
<%= render "govuk_publishing_components/components/list", {
items: [
primary_button_for(@latest_popular_links, create_popular_links_path, "Create new edition"),
link_to("View on GOV.UK (opens in new tab)", view_homepage_path, class: "govuk-link")
]
} %>
<% else %>
<%= primary_link_button_for(edit_popular_links_path(@latest_popular_links), "Edit popular links") %>
<%= secondary_button_for(@latest_popular_links, publish_popular_links_path(@latest_popular_links), "Publish") %>
<%= render "govuk_publishing_components/components/list", {
items: [
primary_link_button_for(edit_popular_links_path(@latest_popular_links), "Edit popular links") ,
secondary_button_for(@latest_popular_links, publish_popular_links_path(@latest_popular_links), "Publish"),
link_to("Preview (opens in new tab)", preview_homepage_path(@latest_popular_links), class: "govuk-link")
]
} %>
<% end %>
</div>
</div>
10 changes: 10 additions & 0 deletions test/integration/homepage_popular_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest
assert page.has_css?(".govuk-tag--green")
end

should "have link to view 'GOV.UK'" do
assert page.has_link?("View on GOV.UK (opens in new tab)", href: Plek.website_root)
end

should "have 'Create new edition' button" do
assert page.has_text?("Create new edition")
end
Expand Down Expand Up @@ -59,6 +63,12 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest
assert page.has_text?("Edit popular links")
end

should "have link to preview on draft-origin" do
click_button("Create new edition")

assert page.has_link?("Preview (opens in new tab)", href: /#{Plek.external_url_for('draft-origin')}/)
end

should "create a new record with next version and 'draft' status" do
row = find_all(".govuk-summary-list__row")
assert row[0].has_text?("Edition")
Expand Down
35 changes: 35 additions & 0 deletions test/unit/helpers/admin/paths_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ class PathsHelperTest < ActionView::TestCase
end
end

context "#homepage popular links paths" do
setup do
Rails.stubs(:application).returns(
stub(
config: stub(jwt_auth_secret: JWT_AUTH_SECRET),
),
)
end

context "when navigated to preview path" do
should "append a valid JWT token" do
popular_links = FactoryBot.build(:popular_links, auth_bypass_id: "123")
result = preview_homepage_path(popular_links)

path = result.gsub(/^(.*)\?.*$/, '\1')
assert_equal draft_origin.to_s, path

token = result.gsub(/.*token=(.*)$/, '\1')
payload = decoded_token_payload(token)

assert_equal payload["sub"], "123"
assert_equal payload["content_id"], "ad7968d0-0339-40b2-80bc-3ea1db8ef1b7"
assert_equal payload["iat"], Time.zone.now.to_i
assert_equal payload["exp"], 1.month.from_now.to_i
end
end

context "when navigated to homepage path" do
should "not append the JWT token" do
result = view_homepage_path
assert_no_match %r{&token=}, result
end
end
end

def draft_origin
Plek.find("draft-origin")
end
Expand Down

0 comments on commit 792fd9d

Please sign in to comment.