Skip to content

Commit 192753b

Browse files
committed
Give the share preview section explicit token states
Render the "Share preview link with someone else" section in two states: when a token exists, show the copy link with options to regenerate or delete it; when no token exists, show only a button to generate one. Deleting a token flips the summary page to the empty state.
1 parent 9495708 commit 192753b

5 files changed

Lines changed: 121 additions & 107 deletions

File tree

app/components/admin/editions/show/preview_component.html.erb

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,46 @@
2929
<% end %>
3030

3131
<% if local_edition.has_enabled_shareable_preview? %>
32-
<% shareable_preview_link = show_url_with_auth_bypass_options(local_edition, draft: true, locale: local_edition.primary_locale) %>
33-
3432
<%= render "govuk_publishing_components/components/details", {
35-
title: "Share document preview",
33+
title: "Share preview link with someone else",
3634
} do %>
37-
<p class="govuk-body">Send this preview link to someone so they can see the content and how the document will appear on GOV.UK.</p>
38-
<p class="govuk-body">No password is needed and anyone with the preview link can view it. You're responsible for who you share draft documents with. </p>
39-
<p class="govuk-body">The preview link will expire on <%= Time.zone.today.next_month.strftime("%-d %B %Y") %> or when the document is published.</p>
40-
41-
<%= render "govuk_publishing_components/components/copy_to_clipboard", {
42-
label: "Copy and send this link to someone and they’ll be able to preview your draft on GOV.UK.",
43-
copyable_content: shareable_preview_link,
44-
button_text: "Copy link",
45-
} %>
46-
47-
<p class="govuk-body govuk-!-margin-top-7">Reset and generate a new preview link if you've shared the preview with the wrong person or if the link has expired. This will disable the previous preview link.</p>
48-
49-
<%= form_with(url: update_bypass_id_admin_edition_path(local_edition), method: :patch) do |f| %>
50-
<input type="hidden" name="_method" value="patch">
51-
<%= render "govuk_publishing_components/components/button", {
52-
text: "Generate new link",
53-
secondary_quiet: true,
35+
<p class="govuk-body">Anyone with a link can preview the content on GOV.UK. No password is needed. You’re responsible for who you share the link with.</p>
36+
37+
<% if token_generated? %>
38+
<% shareable_preview_link = show_url_with_auth_bypass_options(local_edition, draft: true, locale: local_edition.primary_locale) %>
39+
40+
<p class="govuk-body">The link will expire on <%= Time.zone.today.next_month.strftime("%-d %B %Y") %> or when the document is published.</p>
41+
42+
<%= render "govuk_publishing_components/components/copy_to_clipboard", {
43+
label: "Preview link",
44+
copyable_content: shareable_preview_link,
45+
button_text: "Copy link",
5446
} %>
47+
48+
<p class="govuk-body govuk-!-margin-top-7">Generate a new link if it’s expired or shared by mistake. The old link will no longer work.</p>
49+
50+
<div class="govuk-button-group">
51+
<%= form_with(url: update_bypass_id_admin_edition_path(local_edition), method: :patch, data: { module: "prevent-multiple-form-submissions" }) do %>
52+
<%= render "govuk_publishing_components/components/button", {
53+
text: "Generate new link",
54+
secondary_quiet: true,
55+
} %>
56+
<% end %>
57+
58+
<%= form_with(url: destroy_bypass_id_admin_edition_path(local_edition), method: :delete, data: { module: "prevent-multiple-form-submissions" }) do %>
59+
<%= render "govuk_publishing_components/components/button", {
60+
text: "Delete link",
61+
secondary_quiet: true,
62+
} %>
63+
<% end %>
64+
</div>
65+
<% else %>
66+
<%= form_with(url: update_bypass_id_admin_edition_path(local_edition), method: :patch, data: { module: "prevent-multiple-form-submissions" }) do %>
67+
<%= render "govuk_publishing_components/components/button", {
68+
text: "Generate link",
69+
secondary_quiet: true,
70+
} %>
71+
<% end %>
5572
<% end %>
5673
<% end %>
5774
<% end %>

app/components/admin/editions/show/preview_component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def versioning_completed
1919
@versioning_completed ||= edition.versioning_completed?
2020
end
2121

22+
def token_generated?
23+
edition.auth_bypass_id.present?
24+
end
25+
2226
def preview_link(link_text, href)
2327
link_to(link_text,
2428
href,

test/components/admin/editions/show/preview_component_test.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,39 @@ class Admin::Editions::Show::PreviewComponentTest < ViewComponent::TestCase
5858

5959
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
6060

61-
assert_selector ".govuk-details__summary-text", text: "Share document preview"
61+
assert_selector ".govuk-details__summary-text", text: "Share preview link with someone else"
62+
end
63+
64+
test "renders the copy link, regenerate and delete controls when the edition has a preview token" do
65+
edition = build_stubbed(:publication, document: @document)
66+
67+
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
68+
69+
assert_text "Anyone with a link can preview the content on GOV.UK."
70+
assert_text "The link will expire on"
71+
assert_text "The old link will no longer work."
72+
assert_button "Copy link", exact: true, visible: :all
73+
assert_button "Generate new link", exact: true, visible: :all
74+
assert_button "Delete link", exact: true, visible: :all
75+
end
76+
77+
test "renders only a generate control and no link when the edition has no preview token" do
78+
edition = build_stubbed(:publication, document: @document, auth_bypass_id: nil)
79+
80+
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
81+
82+
assert_text "Anyone with a link can preview the content on GOV.UK."
83+
assert_button "Generate link", exact: true, visible: :all
84+
assert_no_button "Copy link", exact: true, visible: :all
85+
assert_no_button "Delete link", exact: true, visible: :all
6286
end
6387

6488
test "does not render sharable preview functionality when edition is in a post-published state" do
6589
edition = build(:published_publication, document: @document)
6690

6791
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
6892

69-
assert_selector ".govuk-details__summary-text", text: "Share document preview", count: 0
93+
assert_selector ".govuk-details__summary-text", text: "Share preview link with someone else", count: 0
7094
end
7195

7296
test "does not render preview or sharable preview functionality and informs the user when versioning needs to be completed" do
@@ -76,7 +100,7 @@ class Admin::Editions::Show::PreviewComponentTest < ViewComponent::TestCase
76100
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
77101

78102
assert_selector "a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)", count: 0
79-
assert_selector ".govuk-details__summary-text", text: "Share document preview", count: 0
103+
assert_selector ".govuk-details__summary-text", text: "Share preview link with someone else", count: 0
80104
assert_selector ".govuk-inset-text", text: "To see the changes and share a document preview link, add a change note or mark the change type to minor."
81105
end
82106

test/integration/shareable_preview_generate_new_link_test.rb

Lines changed: 0 additions & 65 deletions
This file was deleted.

test/integration/shareable_preview_test.rb

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,76 @@
33

44
class ShareablePreviewIntegrationTest < ActionDispatch::IntegrationTest
55
extend Minitest::Spec::DSL
6+
include Capybara::DSL
67
include TaxonomyHelper
78
include Admin::EditionRoutesHelper
89

910
describe "shareable preview feature" do
10-
context "for draft documents" do
11+
before do
12+
login_as create(:gds_editor)
13+
topic_taxon = build(:taxon_hash)
14+
stub_publishing_api_expanded_links_with_taxons(edition.content_id, [])
15+
stub_publishing_api_links_with_taxons(edition.content_id, [topic_taxon["content_id"]])
16+
visit admin_edition_path(edition)
17+
end
18+
19+
context "for a draft with a preview link" do
1120
let(:edition) { create(:draft_publication) }
1221

13-
before do
14-
create_setup(edition)
22+
test "it shows the preview link with copy, regenerate and delete controls" do
23+
open_share_preview_section
24+
25+
assert page.has_button?("Copy link")
26+
assert page.has_button?("Generate new link")
27+
assert page.has_button?("Delete link")
1528
end
1629

17-
test "it shows shareable preview feature" do
18-
get admin_edition_path(edition)
19-
assert_select ".govuk-details__summary-text", text: "Share document preview"
30+
test "it regenerates the preview link, replacing the previous one" do
31+
open_share_preview_section
32+
previous_token = preview_token_in_page
33+
34+
click_button "Generate new link"
35+
assert page.has_content?("New document preview link generated")
36+
37+
open_share_preview_section
38+
assert_not_equal previous_token, preview_token_in_page
39+
end
40+
41+
test "it deletes the preview link and can generate a new one from the empty state" do
42+
open_share_preview_section
43+
44+
click_button "Delete link"
45+
assert page.has_content?("Document preview link deleted")
46+
47+
open_share_preview_section
48+
assert page.has_button?("Generate link")
49+
assert page.has_no_button?("Copy link")
50+
assert page.has_no_button?("Delete link")
51+
52+
click_button "Generate link"
53+
assert page.has_content?("New document preview link generated")
54+
55+
open_share_preview_section
56+
assert page.has_button?("Copy link")
57+
assert page.has_button?("Delete link")
2058
end
2159
end
2260

23-
context "for published documents" do
61+
context "for a published document" do
2462
let(:edition) { create(:published_publication) }
2563

26-
before do
27-
create_setup(edition)
64+
test "it does not show the shareable preview feature" do
65+
assert_not page.has_content?("Share preview link with someone else")
2866
end
67+
end
2968

30-
test "it does not show shareable preview feature" do
31-
get admin_edition_path(edition)
32-
refute_select ".govuk-details__summary-text", text: "Share document preview"
33-
end
69+
def open_share_preview_section
70+
find(".govuk-details__summary", text: "Share preview link with someone else").click
3471
end
3572

36-
def create_setup(edition)
37-
@user = create(:gds_editor)
38-
login_as @user
39-
topic_taxon = build(:taxon_hash)
40-
stub_publishing_api_expanded_links_with_taxons(edition.content_id, [])
41-
stub_publishing_api_links_with_taxons(edition.content_id, [topic_taxon["content_id"]])
73+
def preview_token_in_page
74+
preview_url = all("input").first.value
75+
Rack::Utils.parse_query(URI(preview_url).query)["token"]
4276
end
4377
end
4478
end

0 commit comments

Comments
 (0)