Skip to content

Commit

Permalink
Merge pull request #8998 from alphagov/improve-republishing-worker-te…
Browse files Browse the repository at this point in the history
…st-coverage

Improve republishing worker test coverage
  • Loading branch information
JonathanHallam authored Apr 29, 2024
2 parents 607d5d1 + 097e631 commit 39df09d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 16 deletions.
8 changes: 1 addition & 7 deletions app/workers/publishing_api_document_republishing_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def the_document_has_non_superseded_editions_to_republish?
end

def the_document_has_been_unpublished?
@latest_unpublished_edition.present?
latest_unpublished_edition.present?
end

def the_document_has_been_withdrawn?
Expand Down Expand Up @@ -113,12 +113,6 @@ def send_unpublish(edition)
handle_attachments_for(edition)
end

def locales_for(edition)
Whitehall::PublishingApi.locales_for(edition).each do |locale|
yield locale.to_s
end
end

def handle_attachments_for(edition)
ServiceListeners::PublishingApiAssociatedDocuments.process(
edition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,108 @@
require "gds_api/test_helpers/publishing_api"

class PublishingApiDocumentRepublishingWorkerTest < ActiveSupport::TestCase
test "should ignore old superseded editions when doing bulk republishing" do
document = create(:document, editions: [build(:superseded_edition)])
extend Minitest::Spec::DSL

Whitehall::PublishingApi.expects(:publish).never
Whitehall::PublishingApi.expects(:save_draft).never
Whitehall::PublishingApi.expects(:locales_for).never
Whitehall::PublishingApi.expects(:patch_links).never
PublishingApiUnpublishingWorker.any_instance.expects(:perform).never
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).never
context "#perform" do
test "does nothing when the document only has superseded editions" do
document = create(:document, editions: [build(:superseded_edition)])

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
Whitehall::PublishingApi.expects(:publish).never
Whitehall::PublishingApi.expects(:save_draft).never
Whitehall::PublishingApi.expects(:patch_links).never
PublishingApiUnpublishingWorker.any_instance.expects(:perform).never
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

context "when there are non-superseded editions" do
test "unpublishes the latest unpublished edition when the document has been unpublished" do
first_unpublished_edition = build(:unpublished_edition)
last_unpublished_edition = build(:unpublished_edition)
document = create(:document, editions: [first_unpublished_edition, last_unpublished_edition])

PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(last_unpublished_edition.unpublishing.id, last_unpublished_edition.draft?).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(last_unpublished_edition, "republish").once
Whitehall::PublishingApi.expects(:save_draft).never
Whitehall::PublishingApi.expects(:publish).never
Whitehall::PublishingApi.expects(:patch_links).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

test "unpublishes the latest unpublished edition and draft edition when the document has been unpublished and there's also a draft edition" do
first_unpublished_edition = build(:unpublished_edition)
last_unpublished_edition = build(:unpublished_edition)
draft_edition = build(:draft_edition)
document = create(:document, editions: [first_unpublished_edition, last_unpublished_edition, draft_edition])

PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(last_unpublished_edition.unpublishing.id, last_unpublished_edition.draft?).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(last_unpublished_edition, "republish").once
Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once
Whitehall::PublishingApi.expects(:publish).never
Whitehall::PublishingApi.expects(:patch_links).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

test "republishes then unpublishes the live edition when the document has not been unpublished but has been withdrawn" do
live_withdrawn_edition = build(:withdrawn_edition)
document = create(:document, live_edition: live_withdrawn_edition, editions: [live_withdrawn_edition])

Whitehall::PublishingApi.expects(:publish).with(live_withdrawn_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_withdrawn_edition, "republish").once
PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(live_withdrawn_edition.unpublishing.id, live_withdrawn_edition.draft?).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_withdrawn_edition, "republish").once
Whitehall::PublishingApi.expects(:save_draft).never
Whitehall::PublishingApi.expects(:patch_links).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

context "when the document has not been unpublished or withdrawn" do
test "patches links then republishes the draft edition when there's only a draft edition" do
draft_edition = build(:draft_edition)
document = create(:document, editions: [draft_edition])

Whitehall::PublishingApi.expects(:patch_links).with(draft_edition, bulk_publishing: false).once
Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once
Whitehall::PublishingApi.expects(:publish).never
PublishingApiUnpublishingWorker.any_instance.expects(:perform).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

test "patches links then republishes the live edition when there's only a live edition" do
live_edition = build(:published_edition)
document = create(:document, live_edition:, editions: [live_edition])

Whitehall::PublishingApi.expects(:patch_links).with(live_edition, bulk_publishing: false).once
Whitehall::PublishingApi.expects(:publish).with(live_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_edition, "republish").once
Whitehall::PublishingApi.expects(:save_draft).never
PublishingApiUnpublishingWorker.any_instance.expects(:perform).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end

test "patches links with the live edition then republishes the live edition then republishes the draft edition when there's both a live and draft edition" do
live_edition = build(:published_edition)
draft_edition = build(:draft_edition)
document = create(:document, live_edition:, editions: [live_edition, draft_edition])

Whitehall::PublishingApi.expects(:patch_links).with(live_edition, bulk_publishing: false).once
Whitehall::PublishingApi.expects(:publish).with(live_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_edition, "republish").once
Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once
ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once
PublishingApiUnpublishingWorker.any_instance.expects(:perform).never

PublishingApiDocumentRepublishingWorker.new.perform(document.id)
end
end
end
end
end

0 comments on commit 39df09d

Please sign in to comment.