Skip to content

Commit ba50c8f

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. Editions now have no token by default: the factory carries a :with_auth_bypass_id trait for the cases that still need one, and the tests assert the empty-token default (presenter payloads serialise an empty auth_bypass_ids array).
1 parent d35f18e commit ba50c8f

29 files changed

Lines changed: 166 additions & 209 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

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ 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 do |_path, asset_params, _draft, attachable_class, attachable_id, _auth_bypass_ids|
88+
asset_params["assetable_id"].is_a?(Integer) &&
89+
asset_params["asset_variant"] == Asset.variants[:original] &&
90+
asset_params["assetable_type"] == model_type &&
91+
attachable_class == @edition.class.to_s &&
92+
attachable_id == @edition.id
93+
end
8894

8995
put :update,
9096
params: {

test/functional/admin/uploads_controller_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ 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 { |_path, asset_params, _draft, attachable_class, attachable_id, _auth_bypass_ids|
124+
asset_params["assetable_id"].is_a?(Integer) &&
125+
asset_params["asset_variant"] == Asset.variants[:original] &&
126+
asset_params["assetable_type"] == model_type &&
127+
attachable_class == @edition.class.to_s &&
128+
attachable_id == @edition.id
129+
}.twice
124130

125131
post :create, params: { edition_id: @edition, upload: valid_create_params }
126132
end

test/integration/asset_access_options_integration_test.rb

Lines changed: 71 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,83 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
2323
stub_asset(asset_manager_id, draft: true)
2424
end
2525

26-
context "given a draft document with file attachment" do
27-
let(:edition) { create(:detailed_guide, organisations: [organisation]) }
28-
26+
context "forwarding the preview token to Asset Manager" do
2927
before do
30-
add_file_attachment_with_asset("sample.docx", to: edition)
28+
add_file_attachment_with_asset("logo.png", to: edition)
3129
edition.save!
3230
end
3331

34-
context "when document is marked as access limited in Whitehall" do
32+
context "when the draft has a preview token" do
33+
let(:edition) { create(:detailed_guide, :with_auth_bypass_id) }
34+
35+
it "sends the attachment with the document's auth_bypass_id" do
36+
Services.asset_manager.expects(:create_asset).at_least_once.with(
37+
has_entries(auth_bypass_ids: [edition.auth_bypass_id]),
38+
).returns(asset_manager_response)
39+
40+
AssetManagerCreateAssetJob.drain
41+
end
42+
end
43+
44+
context "when the draft has no preview token" do
45+
let(:edition) { create(:detailed_guide) }
46+
47+
it "sends the attachment with an empty auth_bypass_ids" do
48+
Services.asset_manager.expects(:create_asset).at_least_once.with(
49+
has_entries(auth_bypass_ids: []),
50+
).returns(asset_manager_response)
51+
52+
AssetManagerCreateAssetJob.drain
53+
end
54+
end
55+
end
56+
57+
context "applying access limiting to a draft's attachments" do
58+
context "when a draft is marked as access limited" do
59+
let(:edition) { create(:detailed_guide, organisations: [organisation]) }
60+
3561
before do
62+
add_file_attachment_with_asset("sample.docx", to: edition)
63+
edition.save!
3664
visit edit_admin_edition_path(edition)
3765
check "Limit access"
3866
click_button "Save"
3967
assert_text "Your document has been saved"
4068
end
4169

42-
it "marks attachment as access limited in Asset Manager" do
70+
it "marks the attachment as access limited in Asset Manager" do
4371
Services.asset_manager
4472
.expects(:update_asset)
4573
.at_least_once.with(asset_manager_id, has_entry("access_limited_organisation_ids", [organisation.content_id]))
4674

4775
AssetManagerAttachmentMetadataJob.drain
4876
end
4977
end
50-
end
5178

52-
context "given a draft document with an image attachment" do
53-
let(:edition) { create(:draft_detailed_guide) }
79+
context "when a draft is unmarked as access limited" do
80+
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
5481

55-
before do
56-
visit admin_edition_path(edition)
57-
click_link "Edit draft"
58-
click_link "Images"
59-
attach_file "images[][image_data_attributes][file]", path_to_attachment("minister-of-funk.960x640.jpg")
60-
click_button "Upload"
61-
end
82+
before do
83+
add_file_attachment_with_asset("sample.docx", to: edition)
84+
edition.save!
85+
visit edit_admin_edition_path(edition)
86+
uncheck "Limit access"
87+
click_button "Save"
88+
assert_text "Your document has been saved"
89+
end
6290

63-
# Note that there is no access limiting applied to non attachments. This is existing behaviour that probably needs changing.
64-
it "sends an image to asset manager with the document's auth_bypass_id" do
65-
Services.asset_manager.expects(:create_asset).at_least_once.with(
66-
has_entry(auth_bypass_ids: [edition.auth_bypass_id]),
67-
).returns(asset_manager_response)
91+
it "unmarks the attachment as access limited in Asset Manager" do
92+
Services.asset_manager
93+
.expects(:update_asset)
94+
.at_least_once.with(asset_manager_id, has_entry("access_limited_organisation_ids", []))
6895

69-
AssetManagerCreateAssetJob.drain
96+
AssetManagerAttachmentMetadataJob.drain
97+
end
7098
end
71-
end
7299

73-
context "given an access-limited draft document" do
74-
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
100+
context "when an attachment is added to an access-limited draft" do
101+
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
75102

76-
context "when an attachment is added to the draft document" do
77103
before do
78104
visit admin_edition_path(edition)
79105
click_link "Add attachments"
@@ -84,46 +110,18 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
84110
assert_text "Attachment 'logo.png' uploaded"
85111
end
86112

87-
it "marks attachment as access limited and sends it with an auth_bypass_id in Asset Manager" do
113+
it "marks the attachment as access limited in Asset Manager" do
88114
Services.asset_manager.expects(:create_asset).with(
89-
has_entries(
90-
access_limited_organisation_ids: [organisation.content_id],
91-
auth_bypass_ids: [edition.auth_bypass_id],
92-
),
115+
has_entries(access_limited_organisation_ids: [organisation.content_id]),
93116
).returns(asset_manager_response)
94117

95118
AssetManagerCreateAssetJob.drain
96119
end
97120
end
98121

99-
context "when an html attachment is added to the draft document" do
100-
let(:edition) { create(:publication, :policy_paper) }
101-
102-
before do
103-
visit admin_edition_path(edition)
104-
click_link "Edit attachments"
105-
click_link "Add new HTML attachment"
106-
fill_in "Title", with: "html-attachment"
107-
fill_in "Body", with: "some html content"
108-
end
109-
110-
it "sends an html attachment to publishing api with its edition's auth_bypass_id" do
111-
Services.publishing_api.expects(:put_content)
112-
.with(anything, has_entries(title: edition.title))
113-
Services.publishing_api.expects(:put_content)
114-
.with(anything, has_entries(title: edition.attachments.first.title))
122+
context "when multiple files are uploaded to an access-limited draft" do
123+
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
115124

116-
Services.publishing_api.expects(:put_content).at_least_once
117-
.with(anything, has_entries(
118-
title: "html-attachment",
119-
auth_bypass_ids: [edition.auth_bypass_id],
120-
))
121-
122-
click_button "Save"
123-
end
124-
end
125-
126-
context "when uploaded to draft document" do
127125
before do
128126
visit admin_edition_path(edition)
129127
click_link "Add attachments"
@@ -136,49 +134,24 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
136134
assert find("li", text: "logo.png")
137135
end
138136

139-
it "marks attachment as access limited in Asset Manager" do
137+
it "marks each attachment as access limited in Asset Manager" do
140138
Services.asset_manager
141139
.expects(:create_asset)
142140
.at_least(2)
143141
.with(
144-
has_entries(
145-
access_limited_organisation_ids: [organisation.content_id],
146-
auth_bypass_ids: [edition.auth_bypass_id],
147-
),
142+
has_entries(access_limited_organisation_ids: [organisation.content_id]),
148143
).returns(asset_manager_response)
149144

150145
AssetManagerCreateAssetJob.drain
151146
end
152147
end
153-
end
154148

155-
context "given an access-limited draft document and a file attachment" do
156-
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
157-
158-
before do
159-
add_file_attachment_with_asset("sample.docx", to: edition)
160-
edition.save!
161-
end
149+
context "when an attachment is replaced on an access-limited draft" do
150+
let(:edition) { create(:detailed_guide, organisations: [organisation], access_limited: true) }
162151

163-
context "when document is unmarked as access limited in Whitehall" do
164-
before do
165-
visit edit_admin_edition_path(edition)
166-
uncheck "Limit access"
167-
click_button "Save"
168-
assert_text "Your document has been saved"
169-
end
170-
171-
it "unmarks attachment as access limited in Asset Manager" do
172-
Services.asset_manager
173-
.expects(:update_asset)
174-
.at_least_once.with(asset_manager_id, has_entry("access_limited_organisation_ids", []))
175-
176-
AssetManagerAttachmentMetadataJob.drain
177-
end
178-
end
179-
180-
context "when attachment is replaced" do
181152
before do
153+
add_file_attachment_with_asset("sample.docx", to: edition)
154+
edition.save!
182155
visit admin_edition_path(edition)
183156
click_link "Edit attachments"
184157
click_link "Edit"
@@ -187,25 +160,20 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
187160
assert_text "Attachment 'sample.docx' updated"
188161
end
189162

190-
it "marks replacement attachment as access limited in Asset Manager" do
163+
it "marks the replacement attachment as access limited in Asset Manager" do
191164
Services.asset_manager.stubs(:create_asset).returns(asset_manager_response)
192-
Services.asset_manager.expects(:create_asset).with { |params|
193-
params[:access_limited_organisation_ids] == [organisation.content_id] &&
194-
params[:auth_bypass_ids] == [edition.auth_bypass_id]
195-
}.returns(asset_manager_response)
165+
Services.asset_manager.expects(:create_asset).with { |params| params[:access_limited_organisation_ids] == [organisation.content_id] }.returns(asset_manager_response)
196166

197167
AssetManagerCreateAssetJob.drain
198168
end
199169
end
200-
end
201170

202-
context "given a draft access-limited consultation" do
203-
# 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) }
205-
let(:outcome_attributes) { FactoryBot.attributes_for(:consultation_outcome) }
206-
let!(:outcome) { edition.create_outcome!(outcome_attributes) }
171+
context "when an attachment is added to an access-limited consultation outcome" do
172+
# the edition has to have same organisation as logged in user, otherwise it's not visible when access_limited = true
173+
let(:edition) { create(:consultation, organisations: [organisation], access_limited: true) }
174+
let(:outcome_attributes) { FactoryBot.attributes_for(:consultation_outcome) }
175+
let!(:outcome) { edition.create_outcome!(outcome_attributes) }
207176

208-
context "when an attachment is added to the consultation's outcome" do
209177
before do
210178
visit admin_consultation_path(edition)
211179
click_link "Edit draft"
@@ -217,33 +185,13 @@ class AssetAccessOptionsIntegrationTest < ActionDispatch::IntegrationTest
217185
assert_text "Attachment 'asset-title' uploaded"
218186
end
219187

220-
it "marks attachment as access limited in Asset Manager and sends with the consultation's auth_bypass_id" do
188+
it "marks the attachment as access limited in Asset Manager" do
221189
Services.asset_manager.expects(:create_asset).with(
222-
has_entries(
223-
access_limited_organisation_ids: [organisation.content_id],
224-
auth_bypass_ids: [edition.auth_bypass_id],
225-
),
190+
has_entries(access_limited_organisation_ids: [organisation.content_id]),
226191
).returns(asset_manager_response)
227192
AssetManagerCreateAssetJob.drain
228193
end
229194
end
230-
231-
it "sends a consultation form to asset manager with the consultation's auth_bypass_id" do
232-
visit admin_consultation_path(edition)
233-
click_link "Edit draft"
234-
name_of_form_uploader = "edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][file]"
235-
fill_in "edition[consultation_participation_attributes][consultation_response_form_attributes][title]", with: "Consultation response form"
236-
attach_file name_of_form_uploader, path_to_attachment("simple.pdf")
237-
click_button "Save"
238-
239-
# Note that there is no access limiting applied to non attachments. This is existing behaviour that probably needs changing.
240-
Services.asset_manager.expects(:create_asset).with { |args|
241-
args[:file].path =~ /simple\.pdf/
242-
args[:auth_bypass_ids] == [edition.auth_bypass_id]
243-
}.returns(asset_manager_response)
244-
245-
AssetManagerCreateAssetJob.drain
246-
end
247195
end
248196

249197
private

0 commit comments

Comments
 (0)