Skip to content

Commit

Permalink
Remove the publishing step when trying to republish HTML attachments …
Browse files Browse the repository at this point in the history
…for unpublished or withdrawn documents

The publishing of the HTML attachment could leave the document in a bad state and especially dangerous around sensitive documents where they should be unpublished or withdrawn, but are then inadvertently published during the republishing process.
We should be able to reapply the redirects for unpublishing without the need to publish the article first.
  • Loading branch information
minhngocd committed Jan 30, 2024
1 parent d23404f commit 9c3ea00
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
32 changes: 13 additions & 19 deletions app/services/service_listeners/publishing_api_html_attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ def publish
alias_method :unwithdraw, :publish

def republish
update_publishing_api_content
unpublish_if_required
if Edition::PRE_PUBLICATION_STATES.include?(edition.state)
update_draft(update_type: "republish")
elsif edition.unpublishing
update_draft(update_type: "republish")
patch_links
edition.withdrawn? ? withdraw : unpublish(allow_draft: true)
else
do_publish("republish")
discard_drafts(deleted_html_attachments)
end
end

def update_draft(update_type: nil)
Expand All @@ -32,7 +40,6 @@ def update_draft(update_type: nil)
end
discard_drafts(deleted_html_attachments)
end

# We don't care whether this is a translation or the main
# document, we just send the correct html attachments regardless.
alias_method :update_draft_translation, :update_draft
Expand Down Expand Up @@ -81,22 +88,9 @@ def discard_drafts(html_attachments)
end
end

def update_publishing_api_content
if Edition::PRE_PUBLICATION_STATES.include?(edition.state)
update_draft(update_type: "republish")
else
do_publish("republish")
discard_drafts(deleted_html_attachments)
end
end

def unpublish_if_required
if edition.unpublishing
if edition.withdrawn?
withdraw
else
unpublish(allow_draft: false)
end
def patch_links
current_html_attachments.each do |html_attachment|
Whitehall::PublishingApi.patch_links(html_attachment, bulk_publishing: false)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ class PublishingApiDocumentRepublishingWorkerIntegrationTest < ActiveSupport::Te
}),
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
stub_publishing_api_publish(html_attachment_presenter.content_id, locale: html_attachment_presenter.content[:locale], update_type: nil),
stub_publishing_api_unpublish(html_attachment_presenter.content_id, body: {
type: "redirect",
alternative_path: edition.base_path,
discard_drafts: true,
allow_draft: true,
locale: "en",
}),
]
Expand Down Expand Up @@ -199,11 +198,10 @@ class PublishingApiDocumentRepublishingWorkerIntegrationTest < ActiveSupport::Te
}),
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
stub_publishing_api_publish(html_attachment_presenter.content_id, locale: html_attachment_presenter.content[:locale], update_type: nil),
stub_publishing_api_unpublish(html_attachment_presenter.content_id, body: {
type: "redirect",
alternative_path: edition.base_path,
discard_drafts: true,
allow_draft: true,
locale: "en",
}),
stub_publishing_api_put_content(draft_publication_presenter.content_id, with_locale(:en) { draft_publication_presenter.content }),
Expand Down Expand Up @@ -236,11 +234,10 @@ class PublishingApiDocumentRepublishingWorkerIntegrationTest < ActiveSupport::Te
}),
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
stub_publishing_api_publish(html_attachment_presenter.content_id, locale: html_attachment_presenter.content[:locale], update_type: nil),
stub_publishing_api_unpublish(html_attachment_presenter.content_id, body: {
type: "redirect",
alternative_path: edition.base_path,
discard_drafts: true,
allow_draft: true,
locale: "en",
}),
]
Expand Down Expand Up @@ -278,7 +275,6 @@ class PublishingApiDocumentRepublishingWorkerIntegrationTest < ActiveSupport::Te
repeated_requests = [
stub_publishing_api_put_content(html_attachment_presenter.content_id, html_attachment_presenter.content),
stub_publishing_api_patch_links(html_attachment_presenter.content_id, links: html_attachment_presenter.links),
stub_publishing_api_publish(html_attachment_presenter.content_id, locale: "en", update_type: nil),
stub_publishing_api_unpublish(html_attachment_presenter.content_id, body: withdrawal_content.merge(locale: "en")),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,6 @@ class Republish < PublishingApiHtmlAttachmentsTest
test "for a withdrawn publicaton with an attachment withdraws the attachment" do
publication = create(:withdrawn_publication)
attachment = publication.html_attachments.first
PublishingApiWorker.any_instance.expects(:perform).with(
"HtmlAttachment",
attachment.id,
"republish",
"en",
)
PublishingApiWithdrawalWorker.any_instance.expects(:perform).with(
attachment.content_id,
"content was withdrawn",
Expand Down Expand Up @@ -490,6 +484,13 @@ class Republish < PublishingApiHtmlAttachmentsTest
)
call(publication)
end

test "for a publication that has been unpublished does not publish the attachment in order to unpublish again" do
publication = create(:unpublished_publication_in_error_no_redirect)
attachment = publication.html_attachments.first

Check failure on line 490 in test/unit/app/services/service_listeners/publishing_api_html_attachments_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Lint/UselessAssignment: Useless assignment to variable - `attachment`. (https://rubystyle.guide#underscore-unused-vars)
PublishingApiWorker.any_instance.expects(:perform).never
call(publication)
end
end

class Unwithdraw < PublishingApiHtmlAttachmentsTest
Expand Down

0 comments on commit 9c3ea00

Please sign in to comment.