Skip to content

Commit cba8239

Browse files
Send attachable_url for non-edition attachables
The previous code did not work at all for policy groups, and consultation and call for evidence responses, which are not editions. Since the access limitation restrictions should already kick in before this asset payload is constructed, there is no reason to check for visibility at this level. We have removed the calls to `visible_edition` in favour of just using the `significant_attachable`. This should correctly return either visible attachables or the draft attachable. This works correctly for all attachable types, including responses and policy groups. In order to correctly construct the draft or live attachable URL, we need to determine if it is in draft state. Policy groups are immediately published, so this does not apply to them. For responses, we want to send the parent edition's URL, so we can check the parent state. I have made the `parent_attachable` method public, so we can identify response-like classes - this method is only defined on `ConsultationResponse` and `CallForEvidenceResponse` at the moment. For context, the method was previously never called with a user argument to check the visibility for. The line `visible_edition.blank? && draft_edition` was particularly confusing, since the visible edition should also return the draft_edition, if called with a user. It also invalidated the whole point of ever checking for visibility, when falling back to the draft_edition anyway.
1 parent 1145146 commit cba8239

5 files changed

Lines changed: 167 additions & 10 deletions

File tree

app/models/attachment_data.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,17 @@ def redirect_url
169169
unpublished_attachable.unpublishing.document_url
170170
end
171171

172-
def attachable_url(user = nil)
173-
visible_edition = visible_edition_for(user)
174-
if visible_edition.blank? && draft_edition
175-
draft_edition.public_url(draft: true)
176-
elsif visible_edition.present?
177-
visible_edition.public_url
172+
def attachable_url
173+
return nil if significant_attachable.blank?
174+
175+
if significant_attachable.is_a?(Edition)
176+
attachable_in_pre_publication_state = Edition::PRE_PUBLICATION_STATES.include?(significant_attachable.state)
177+
attachable_in_pre_publication_state ? significant_attachable.public_url(draft: true) : significant_attachable.public_url
178+
elsif significant_attachable.respond_to?(:parent_attachable) && significant_attachable.parent_attachable.is_a?(Edition)
179+
parent_attachable_in_pre_publication_state = Edition::PRE_PUBLICATION_STATES.include?(significant_attachable.parent_attachable.state)
180+
parent_attachable_in_pre_publication_state ? significant_attachable.parent_attachable.public_url(draft: true) : significant_attachable.parent_attachable.public_url
181+
elsif significant_attachable.is_a?(PolicyGroup)
182+
significant_attachable.public_url
178183
end
179184
end
180185

app/models/call_for_evidence_response.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def path_name
5757

5858
delegate :public_timestamp, :first_published_version?, :slug, :document, :images, :content_id, to: :call_for_evidence
5959

60-
private
61-
6260
def parent_attachable
6361
call_for_evidence || Attachable::Null.new
6462
end

app/models/consultation_response.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def path_name
5757

5858
delegate :public_timestamp, :first_published_version?, :slug, :document, :images, :content_id, to: :consultation
5959

60-
private
61-
6260
def parent_attachable
6361
consultation || Attachable::Null.new
6462
end

test/unit/app/models/attachment_data_test.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,101 @@ class AttachmentDataTest < ActiveSupport::TestCase
445445

446446
assert_not attachment_data.all_asset_variants_uploaded?
447447
end
448+
449+
context "#attachable_url" do
450+
context "when the attachable is an edition" do
451+
# This test never passed
452+
# We never call it with a user so no need to worry about visibility - can remove
453+
# it "returns draft url if edition in draft, when called with specific user" do
454+
# edition = create(:draft_standard_edition, :with_file_attachment)
455+
# attachment_data = edition.attachments.first.attachment_data
456+
# user = create(:user)
457+
#
458+
# assert_equal edition.public_url(draft: true), attachment_data.attachable_url(user)
459+
# end
460+
461+
# Works because visible_to?(user) returns true because !draft
462+
# We never call it with a user so no need to worry about visibility - can remove
463+
# it "returns live url if edition is published, when called with specific user" do
464+
# edition = create(:published_standard_edition, :with_file_attachment)
465+
# attachment_data = edition.attachments.first.attachment_data
466+
# user = create(:user)
467+
#
468+
# assert_equal edition.public_url, attachment_data.attachable_url(user)
469+
# end
470+
471+
it "returns draft url for pre-publication edition" do
472+
edition = create(:draft_standard_edition, :with_file_attachment)
473+
attachment_data = edition.attachments.first.attachment_data
474+
475+
assert_equal edition.public_url(draft: true), attachment_data.attachable_url
476+
end
477+
478+
it "returns live url for published edition" do
479+
# Works because visible_to?(user) returns true because !draft
480+
edition = create(:published_standard_edition, :with_file_attachment)
481+
attachment_data = edition.attachments.first.attachment_data
482+
483+
assert_equal edition.public_url, attachment_data.attachable_url
484+
end
485+
486+
it "returns nil for deleted edition" do
487+
edition = create(:draft_standard_edition, :with_file_attachment)
488+
attachment_data = edition.attachments.first.attachment_data
489+
assert attachment_data.attachments.first.attachable
490+
491+
edition.destroy!
492+
edition.delete_all_attachments
493+
# Tests would still pass without this line which is executed as part of the EditionDeleter, but wanted to make sure it doesn't have anything to do with the attachment being deleted.
494+
# This indicates we could write these as controller or other integration tests.
495+
496+
assert_equal true, attachment_data.reload.attachments.first.deleted?
497+
assert_nil attachment_data.attachments.first.attachable
498+
assert_nil attachment_data.attachable_url
499+
end
500+
end
501+
502+
context "when the attachable is a consultation outcome" do
503+
it "returns draft url for pre-publication consultation" do
504+
consultation = create(:draft_consultation)
505+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
506+
attachment_data = outcome.attachments.first.attachment_data
507+
508+
assert_equal consultation.public_url(draft: true), attachment_data.attachable_url
509+
end
510+
511+
it "returns live url for published consultation" do
512+
consultation = create(:published_consultation)
513+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
514+
attachment_data = outcome.attachments.first.attachment_data
515+
516+
assert_equal consultation.public_url, attachment_data.attachable_url
517+
end
518+
519+
it "returns nil for deleted consultation" do
520+
consultation = create(:consultation)
521+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
522+
attachment_data = outcome.attachments.first.attachment_data
523+
assert attachment_data.attachments.first.attachable
524+
525+
consultation.destroy!
526+
consultation.delete_all_attachments
527+
# Tests would still pass without this line which is executed as part of the EditionDeleter, but wanted to make sure it doesn't have anything to do with the attachment being deleted.
528+
# This indicates we could write these as controller or other integration tests.
529+
530+
assert_equal true, attachment_data.reload.attachments.first.deleted?
531+
assert_nil attachment_data.attachments.first.attachable
532+
assert_nil attachment_data.attachable_url
533+
end
534+
end
535+
536+
context "when the attachable is a policy group" do
537+
it "returns live url" do
538+
policy_group = create(:policy_group, :with_file_attachment)
539+
attachment_data = policy_group.attachments.first.attachment_data
540+
541+
assert_equal policy_group.public_url, attachment_data.attachable_url
542+
end
543+
end
544+
end
448545
end

test/unit/app/services/asset_manager/attachment_updater_test.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,64 @@ class AssetManager::AttachmentUpdaterTest < ActiveSupport::TestCase
191191
end
192192
end
193193
end
194+
195+
context "when the attachment belongs to a draft consultation's outcome" do
196+
let(:consultation) { create(:draft_consultation) }
197+
let(:outcome) { create(:consultation_outcome, :with_file_attachment, consultation:) }
198+
let(:attachment_data) { outcome.attachments.first.attachment_data }
199+
let(:asset_manager_id) { attachment_data.assets.first.asset_manager_id }
200+
201+
it "sets the expected attributes" do
202+
AssetManager::AssetUpdater.expects(:call).with(
203+
asset_manager_id,
204+
{
205+
"draft" => true,
206+
"access_limited_organisation_ids" => [],
207+
"parent_document_url" => consultation.public_url(draft: true),
208+
},
209+
)
210+
211+
AssetManager::AttachmentUpdater.call(attachment_data)
212+
end
213+
end
214+
215+
context "when the attachment belongs to a published consultation's outcome" do
216+
let(:consultation) { create(:published_consultation) }
217+
let(:outcome) { create(:consultation_outcome, :with_file_attachment, consultation:) }
218+
let(:attachment_data) { outcome.attachments.first.attachment_data }
219+
let(:asset_manager_id) { attachment_data.assets.first.asset_manager_id }
220+
221+
it "sets the expected attributes" do
222+
AssetManager::AssetUpdater.expects(:call).with(
223+
asset_manager_id,
224+
{
225+
"draft" => false,
226+
"access_limited_organisation_ids" => [],
227+
"parent_document_url" => consultation.public_url,
228+
},
229+
)
230+
231+
AssetManager::AttachmentUpdater.call(attachment_data)
232+
end
233+
end
234+
235+
context "when the attachment belongs to a policy group" do
236+
let(:policy_group) { create(:policy_group, :with_file_attachment) }
237+
let(:attachment_data) { policy_group.attachments.first.attachment_data }
238+
let(:asset_manager_id) { attachment_data.assets.first.asset_manager_id }
239+
240+
it "sets the expected attributes" do
241+
AssetManager::AssetUpdater.expects(:call).with(
242+
asset_manager_id,
243+
{
244+
"draft" => false,
245+
"access_limited_organisation_ids" => [],
246+
"parent_document_url" => policy_group.public_url,
247+
},
248+
)
249+
250+
AssetManager::AttachmentUpdater.call(attachment_data)
251+
end
252+
end
194253
end
195254
end

0 commit comments

Comments
 (0)