Skip to content

Commit 5ae5179

Browse files
lauraghiorghisor-tweYinka
authored andcommitted
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 ae7833c commit 5ae5179

5 files changed

Lines changed: 231 additions & 11 deletions

File tree

app/models/attachment_data.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,23 @@ 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+
url_for(significant_attachable)
177+
elsif significant_attachable.respond_to?(:parent_attachable) && significant_attachable.parent_attachable.is_a?(Edition)
178+
url_for(significant_attachable.parent_attachable)
179+
elsif significant_attachable.is_a?(PolicyGroup)
180+
significant_attachable.public_url
181+
end
182+
end
183+
184+
def url_for(edition)
185+
if Edition::PRE_PUBLICATION_STATES.include?(edition.state)
186+
edition.public_url(draft: true)
187+
elsif edition.publicly_visible?
188+
edition.public_url
178189
end
179190
end
180191

@@ -188,7 +199,7 @@ def keep_existing_file?
188199
to_replace_id.present? && keep_or_replace != "replace"
189200
end
190201

191-
private
202+
private
192203

193204
def filtered_attachments(include_deleted_attachables: false)
194205
if include_deleted_attachables

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: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,158 @@ 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+
it "returns draft url for pre-publication edition" do
452+
edition = create(:draft_standard_edition, :with_file_attachment)
453+
attachment_data = edition.attachments.first.attachment_data
454+
455+
assert_equal edition.public_url(draft: true), attachment_data.attachable_url
456+
end
457+
458+
Edition::PUBLICLY_VISIBLE_STATES.each do |state|
459+
it "returns live url for #{state} edition" do
460+
edition = create(:"#{state}_standard_edition", :with_file_attachment)
461+
attachment_data = edition.attachments.first.attachment_data
462+
463+
assert_equal edition.public_url, attachment_data.attachable_url
464+
end
465+
end
466+
467+
it "returns nil for deleted edition" do
468+
edition = create(:draft_standard_edition, :with_file_attachment)
469+
attachment_data = edition.attachments.first.attachment_data
470+
assert attachment_data.attachments.first.attachable
471+
472+
edition.destroy!
473+
edition.delete_all_attachments
474+
# 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.
475+
# This indicates we could write these as controller or other integration tests.
476+
477+
assert_equal true, attachment_data.reload.attachments.first.deleted?
478+
assert_nil attachment_data.attachments.first.attachable
479+
assert_nil attachment_data.attachable_url
480+
end
481+
482+
it "returns nil for unpublished edition" do
483+
edition = create(:unpublished_standard_edition, :with_file_attachment)
484+
attachment_data = edition.attachments.first.attachment_data
485+
486+
assert_nil attachment_data.attachable_url
487+
end
488+
end
489+
490+
context "when the attachable is a consultation outcome" do
491+
Edition::PRE_PUBLICATION_STATES.each do |state|
492+
it "returns draft url for #{state} consultation" do
493+
consultation = create(:"#{state}_consultation")
494+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
495+
attachment_data = outcome.attachments.first.attachment_data
496+
497+
assert_equal consultation.public_url(draft: true), attachment_data.attachable_url
498+
end
499+
end
500+
501+
Edition::PUBLICLY_VISIBLE_STATES.each do |state|
502+
it "returns live url for #{state} consultation" do
503+
consultation = create(:"#{state}_consultation")
504+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
505+
attachment_data = outcome.attachments.first.attachment_data
506+
507+
assert_equal consultation.public_url, attachment_data.attachable_url
508+
end
509+
end
510+
511+
it "returns nil for deleted consultation" do
512+
consultation = create(:consultation)
513+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
514+
attachment_data = outcome.attachments.first.attachment_data
515+
assert attachment_data.attachments.first.attachable
516+
517+
consultation.destroy!
518+
consultation.delete_all_attachments
519+
# 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.
520+
# This indicates we could write these as controller or other integration tests.
521+
522+
assert_equal true, attachment_data.reload.attachments.first.deleted?
523+
assert_nil attachment_data.attachments.first.attachable
524+
assert_nil attachment_data.attachable_url
525+
end
526+
527+
it "returns nil for unpublished consultation" do
528+
consultation = create(:unpublished_consultation)
529+
outcome = create(:consultation_outcome, :with_file_attachment, consultation:)
530+
attachment_data = outcome.attachments.first.attachment_data
531+
532+
assert_nil attachment_data.attachable_url
533+
end
534+
end
535+
536+
context "when the attachable is a call for evidence outcome" do
537+
Edition::PRE_PUBLICATION_STATES.each do |state|
538+
it "returns draft url for #{state} call for evidence" do
539+
call_for_evidence = create(:"#{state}_call_for_evidence")
540+
outcome = create(:call_for_evidence_outcome, :with_file_attachment, call_for_evidence:)
541+
attachment_data = outcome.attachments.first.attachment_data
542+
543+
assert_equal call_for_evidence.public_url(draft: true), attachment_data.attachable_url
544+
end
545+
end
546+
547+
Edition::PUBLICLY_VISIBLE_STATES.each do |state|
548+
it "returns live url for #{state} call for evidence" do
549+
call_for_evidence = create(:"#{state}_call_for_evidence")
550+
outcome = create(:call_for_evidence_outcome, :with_file_attachment, call_for_evidence:)
551+
attachment_data = outcome.attachments.first.attachment_data
552+
553+
assert_equal call_for_evidence.public_url, attachment_data.attachable_url
554+
end
555+
end
556+
557+
it "returns nil for deleted call for evidence" do
558+
call_for_evidence = create(:call_for_evidence)
559+
outcome = create(:call_for_evidence_outcome, :with_file_attachment, call_for_evidence:)
560+
attachment_data = outcome.attachments.first.attachment_data
561+
assert attachment_data.attachments.first.attachable
562+
563+
call_for_evidence.destroy!
564+
call_for_evidence.delete_all_attachments
565+
566+
assert_equal true, attachment_data.reload.attachments.first.deleted?
567+
assert_nil attachment_data.attachments.first.attachable
568+
assert_nil attachment_data.attachable_url
569+
end
570+
571+
it "returns nil for unpublished call for evidence" do
572+
call_for_evidence = create(:unpublished_call_for_evidence)
573+
outcome = create(:call_for_evidence_outcome, :with_file_attachment, call_for_evidence:)
574+
attachment_data = outcome.attachments.first.attachment_data
575+
576+
assert_nil attachment_data.attachable_url
577+
end
578+
end
579+
580+
context "when the attachable is a policy group" do
581+
it "returns live url" do
582+
policy_group = create(:policy_group, :with_file_attachment)
583+
attachment_data = policy_group.attachments.first.attachment_data
584+
585+
assert_equal policy_group.public_url, attachment_data.attachable_url
586+
end
587+
588+
it "returns nil for deleted policy group" do
589+
policy_group = create(:policy_group, :with_file_attachment)
590+
attachment_data = policy_group.attachments.first.attachment_data
591+
assert attachment_data.attachments.first.attachable
592+
593+
policy_group.destroy!
594+
policy_group.delete_all_attachments
595+
596+
assert_equal true, attachment_data.reload.attachments.first.deleted?
597+
assert_nil attachment_data.attachments.first.attachable
598+
assert_nil attachment_data.attachable_url
599+
end
600+
end
601+
end
448602
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)