Skip to content

Commit 63bdfd2

Browse files
committed
Stop auto-generating auth bypass tokens on edition creation
Remove the before_create callback that assigned every edition an auth_bypass_id, making preview token generation opt-in: a draft has no token until a publisher generates one. Move the factory's auth_bypass_id into a :with_auth_bypass_id trait so test editions match production (no token by default), give that trait only to the tests asserting a token is present, and have the canonical presenter snapshots assert an empty auth_bypass_ids array.
1 parent dc3e859 commit 63bdfd2

30 files changed

Lines changed: 91 additions & 94 deletions

app/models/edition.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class Edition < ApplicationRecord
7777
POST_PUBLICATION_STATES = %w[published superseded withdrawn unpublished].freeze
7878
PUBLICLY_VISIBLE_STATES = %w[published withdrawn].freeze
7979

80-
before_create :set_auth_bypass_id
8180
before_save :set_public_timestamp
8281
after_validation :update_revalidated_at, if: -> { validation_context == :publish }
8382
after_create :update_document_edition_references

features/preview-unpublished-editions.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Feature: Previewing unpublished editions
1515
Scenario: Generating and deleting a shareable document preview link
1616
Given I am an editor
1717
And a draft publication "Test publication" exists
18-
Then there should be a shareable preview link for the publication "Test publication"
19-
When I delete the shareable preview link for the publication "Test publication"
2018
Then there should not be a shareable preview link for the publication "Test publication"
2119
When I generate a shareable preview link for the publication "Test publication"
2220
Then there should be a shareable preview link for the publication "Test publication"
21+
When I delete the shareable preview link for the publication "Test publication"
22+
Then there should not be a shareable preview link for the publication "Test publication"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Admin::Editions::Show::PreviewComponentTest < ViewComponent::TestCase
6262
end
6363

6464
test "renders the copy link, regenerate and delete controls when the edition has a preview token" do
65-
edition = build_stubbed(:publication, document: @document)
65+
edition = build_stubbed(:publication, :with_auth_bypass_id, document: @document)
6666

6767
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
6868

test/factories/editions.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
change_note { "change-note" }
99
summary { "edition-summary" }
1010
previously_published { false }
11-
auth_bypass_id { SecureRandom.uuid }
11+
12+
trait(:with_auth_bypass_id) do
13+
auth_bypass_id { SecureRandom.uuid }
14+
end
1215

1316
trait(:with_organisations) do
1417
transient do

test/functional/admin/editions_controller_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,18 @@ class Admin::EditionsControllerTest < ActionController::TestCase
369369
assert_not flash["html_safe"]
370370
end
371371

372-
test "update_bypass_id generates a new preview token and redirects with a notice" do
372+
test "update_bypass_id generates a preview token and redirects with a notice" do
373373
edition = create(:draft_publication)
374-
previous_auth_bypass_id = edition.auth_bypass_id
375374

376375
patch :update_bypass_id, params: { id: edition }
377376

378377
assert_not_nil edition.reload.auth_bypass_id
379-
assert_not_equal previous_auth_bypass_id, edition.auth_bypass_id
380378
assert_redirected_to admin_publication_path(edition)
381379
assert_equal "New document preview link generated", flash[:notice]
382380
end
383381

384382
test "destroy_bypass_id deletes the preview token and redirects with a notice" do
385-
edition = create(:draft_publication)
383+
edition = create(:draft_publication, :with_auth_bypass_id)
386384
assert_not_nil edition.auth_bypass_id
387385

388386
delete :destroy_bypass_id, params: { id: edition }

test/functional/admin/file_attachments_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def valid_file_attachment_params
8484
attachment = create(:file_attachment, attachable: @edition)
8585
model_type = attachment.attachment_data.class.to_s
8686

87-
AssetManagerCreateAssetJob.expects(:perform_async).with(anything, has_entries("assetable_id" => kind_of(Integer), "asset_variant" => Asset.variants[:original], "assetable_type" => model_type), anything, @edition.class.to_s, @edition.id, [@edition.auth_bypass_id])
87+
AssetManagerCreateAssetJob.expects(:perform_async).with(anything, has_entries("assetable_id" => kind_of(Integer), "asset_variant" => Asset.variants[:original], "assetable_type" => model_type), anything, @edition.class.to_s, @edition.id, anything)
8888

8989
put :update,
9090
params: {

test/functional/admin/uploads_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def post_to_upload_files(*files)
120120
test "POST :create triggers a job to be queued to store the attachments in Asset Manager" do
121121
model_type = AttachmentData.to_s
122122

123-
AssetManagerCreateAssetJob.expects(:perform_async).with(anything, has_entries("assetable_id" => kind_of(Integer), "asset_variant" => Asset.variants[:original], "assetable_type" => model_type), anything, @edition.class.to_s, @edition.id, [@edition.auth_bypass_id]).twice
123+
AssetManagerCreateAssetJob.expects(:perform_async).with(anything, has_entries("assetable_id" => kind_of(Integer), "asset_variant" => Asset.variants[:original], "assetable_type" => model_type), anything, @edition.class.to_s, @edition.id, anything).twice
124124

125125
post :create, params: { edition_id: @edition, upload: valid_create_params }
126126
end

test/integration/asset_access_options_integration_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
5050
end
5151

5252
context "given a draft document with an image attachment" do
53-
let(:edition) { create(:draft_detailed_guide) }
53+
let(:edition) { create(:draft_detailed_guide, :with_auth_bypass_id) }
5454

5555
before do
5656
visit admin_edition_path(edition)
@@ -71,7 +71,7 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
7171
end
7272

7373
context "given an access-limited draft document" do
74-
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
74+
let(:edition) { create(:detailed_guide, :with_auth_bypass_id, organisations: [organisation], access_limited: true) }
7575

7676
context "when an attachment is added to the draft document" do
7777
before do
@@ -97,7 +97,7 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
9797
end
9898

9999
context "when an html attachment is added to the draft document" do
100-
let(:edition) { create(:publication, :policy_paper) }
100+
let(:edition) { create(:publication, :policy_paper, :with_auth_bypass_id) }
101101

102102
before do
103103
visit admin_edition_path(edition)
@@ -153,7 +153,7 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
153153
end
154154

155155
context "given an access-limited draft document and a file attachment" do
156-
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
156+
let(:edition) { create(:detailed_guide, :with_auth_bypass_id, organisations: [organisation], access_limited: true) }
157157

158158
before do
159159
add_file_attachment_with_asset("sample.docx", to: edition)
@@ -201,7 +201,7 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
201201

202202
context "given a draft access-limited consultation" do
203203
# the edition has to have same organisation as logged in user, otherwise it's not visible when access_limited = true
204-
let(:edition) { create(:consultation, organisations: [organisation], access_limited: true) }
204+
let(:edition) { create(:consultation, :with_auth_bypass_id, organisations: [organisation], access_limited: true) }
205205
let(:outcome_attributes) { FactoryBot.attributes_for(:consultation_outcome) }
206206
let!(:outcome) { edition.create_outcome!(outcome_attributes) }
207207

test/integration/jobs/publishing_api_document_republishing_job_integration_test.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,30 @@ class PublishingApiDocumentRepublishingJobIntegrationTest < ActiveSupport::TestC
8484
publication_presenter = PublishingApiPresenters.presenter_for(edition, update_type: "republish")
8585
draft_publication_presenter = PublishingApiPresenters.presenter_for(draft_edition, update_type: "republish")
8686
html_attachment_presenter = PublishingApiPresenters.presenter_for(edition.attachments.first, update_type: "republish")
87-
draft_html_attachment_presenter = PublishingApiPresenters.presenter_for(draft_edition.attachments.first, update_type: "republish")
8887

8988
WebMock.reset!
9089

90+
# The live edition and its new draft share the HTML attachment's content_id and,
91+
# now that neither carries an auth bypass token, present identical content, so the
92+
# attachment content is PUT once per edition version.
93+
html_attachment_content_request = stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content)
94+
9195
requests = [
9296
stub_publishing_api_patch_links(publication_presenter.content_id, links: publication_presenter.links),
9397
stub_publishing_api_put_content(publication_presenter.content_id, with_locale(:en) { publication_presenter.content }),
9498
stub_publishing_api_put_content(publication_presenter.content_id, with_locale(:es) { publication_presenter.content }),
9599
stub_publishing_api_publish(publication_presenter.content_id, locale: "en", update_type: nil),
96100
stub_publishing_api_publish(publication_presenter.content_id, locale: "es", update_type: nil),
97-
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
98101
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
99102
stub_publishing_api_publish(html_attachment_presenter.content_id, locale: "en", update_type: nil),
100103
stub_publishing_api_put_content(draft_publication_presenter.content_id, with_locale(:en) { draft_publication_presenter.content }),
101104
stub_publishing_api_put_content(draft_publication_presenter.content_id, with_locale(:es) { draft_publication_presenter.content }),
102-
stub_publishing_api_put_content(draft_html_attachment_presenter.content_id, draft_html_attachment_presenter.content),
103105
]
104106

105107
PublishingApiDocumentRepublishingJob.new.perform(edition.document.id, false)
106108

107109
assert_all_requested(requests)
110+
assert_requested(html_attachment_content_request, times: 2)
108111
end
109112

110113
test "Should only publish live edition when document is published with invalid draft" do
@@ -195,10 +198,14 @@ class PublishingApiDocumentRepublishingJobIntegrationTest < ActiveSupport::TestC
195198
publication_presenter = PublishingApiPresenters.presenter_for(edition, update_type: "republish")
196199
draft_publication_presenter = PublishingApiPresenters.presenter_for(draft_edition, update_type: "republish")
197200
html_attachment_presenter = PublishingApiPresenters.presenter_for(edition.attachments.first, update_type: "republish")
198-
draft_html_attachment_presenter = PublishingApiPresenters.presenter_for(draft_edition.attachments.first, update_type: "republish")
199201

200202
WebMock.reset!
201203

204+
# The live edition and its new draft share the HTML attachment's content_id and,
205+
# now that neither carries an auth bypass token, present identical content, so the
206+
# attachment content is PUT once per edition version.
207+
html_attachment_content_request = stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content)
208+
202209
requests = [
203210
stub_publishing_api_put_content(publication_presenter.content_id, publication_presenter.content),
204211
stub_publishing_api_put_content(publication_presenter.content_id, with_locale(:es) { publication_presenter.content }),
@@ -212,7 +219,6 @@ class PublishingApiDocumentRepublishingJobIntegrationTest < ActiveSupport::TestC
212219
locale: "es",
213220
allow_draft: true,
214221
}),
215-
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
216222
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
217223
stub_publishing_api_unpublish(html_attachment_presenter.content_id, body: {
218224
type: "redirect",
@@ -222,12 +228,12 @@ class PublishingApiDocumentRepublishingJobIntegrationTest < ActiveSupport::TestC
222228
}),
223229
stub_publishing_api_put_content(draft_publication_presenter.content_id, with_locale(:en) { draft_publication_presenter.content }),
224230
stub_publishing_api_put_content(draft_publication_presenter.content_id, with_locale(:es) { draft_publication_presenter.content }),
225-
stub_publishing_api_put_content(draft_html_attachment_presenter.content_id, draft_html_attachment_presenter.content),
226231
]
227232

228233
PublishingApiDocumentRepublishingJob.new.perform(edition.document.id, false)
229234

230235
assert_all_requested(requests)
236+
assert_requested(html_attachment_content_request, times: 2)
231237
end
232238

233239
test "Should only unpublish live edition when document is unpublished with invalid draft" do

test/integration/shareable_preview_generate_new_link_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ShareablePreviewGenerateNewLinkIntegrationTest < ActionDispatch::Integrati
88
include Admin::EditionRoutesHelper
99

1010
describe "shareable preview generate new link feature" do
11-
context "for draft documents" do
12-
let(:edition) { create(:draft_fatality_notice) }
11+
context "for draft documents with an existing shareable link" do
12+
let(:edition) { create(:draft_fatality_notice, :with_auth_bypass_id) }
1313

1414
before do
1515
create_setup(edition)

0 commit comments

Comments
 (0)