Skip to content

Commit

Permalink
Merge pull request #2292 from alphagov/refactor-save-publish-code
Browse files Browse the repository at this point in the history
Refactor save before publish
  • Loading branch information
syed-ali-tw authored Aug 19, 2024
2 parents 9f1c422 + 79d684b commit 681ba14
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/controllers/homepage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def application_error_message
end

def rescue_already_published_error(error)
already_published_error?(JSON.parse(error.http_body)) ? "Popular links publish was unsuccessful, cannot publish an already published content item.".html_safe : publishing_api_publish_error_message
already_published_error?(JSON.parse(error.http_body)) ? "Popular links publish was unsuccessful, cannot publish an already published edition.".html_safe : publishing_api_publish_error_message
end

def already_published_error?(error_body)
error_body["error"] && error_body["error"]["message"] && error_body["error"]["message"].include?("already published content item")
error_body["error"] && error_body["error"]["message"] && error_body["error"]["message"].include?("Cannot publish an already published")
end

def fetch_latest_popular_link
Expand Down
8 changes: 7 additions & 1 deletion app/models/popular_links_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_draft_popular_links_from_last_record
end

def publish_latest
save_draft
save_draft if is_draft?
Services.publishing_api.publish(content_id, update_type, locale:)
# This publish_popular_links is a new workflow that was introduced for popular links.
publish_popular_links
Expand All @@ -61,6 +61,12 @@ def locale
end

def can_delete?
is_draft?
end

private

def is_draft?
state == "draft"
end
end
40 changes: 27 additions & 13 deletions test/functional/homepage_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ class HomepageControllerTest < ActionController::TestCase
setup do
@popular_links = FactoryBot.create(:popular_links, state: "draft")
end
context "#current is state is draft" do
should "save to publishing API before publish" do
sequence = sequence(:task_order)

Services.publishing_api.expects(:put_content).with(@popular_links.content_id, has_entry(:title, "Homepage Popular Links")).in_sequence(sequence)
Services.publishing_api.expects(:publish).with(@popular_links.content_id, "major", locale: "en").in_sequence(sequence)

post :publish, params: { id: @popular_links.id }
end
end

context "#current is state is published" do
setup do
@popular_links = FactoryBot.create(:popular_links, state: "published", version_number: 2)
end

should "not save to publishing API before publish" do
sequence = sequence(:task_order)

Services.publishing_api.expects(:put_content).times(0)
Services.publishing_api.expects(:publish).with(@popular_links.content_id, "major", locale: "en").in_sequence(sequence)

post :publish, params: { id: @popular_links.id }
end
end

should "publish latest draft popular links and render show template" do
assert_equal "draft", PopularLinksEdition.last.state
Expand All @@ -199,16 +224,6 @@ class HomepageControllerTest < ActionController::TestCase
assert_redirected_to show_popular_links_path
assert_equal "published", PopularLinksEdition.last.state
end

should "save to publishing API before publish" do
sequence = sequence(:task_order)

Services.publishing_api.expects(:put_content).with(@popular_links.content_id, has_entry(:title, "Homepage Popular Links")).in_sequence(sequence)
Services.publishing_api.expects(:publish).with(@popular_links.content_id, "major", locale: "en").in_sequence(sequence)

post :publish, params: { id: @popular_links.id }
end

context "database errors" do
setup do
PopularLinksEdition.any_instance.stubs(:publish_popular_links).raises(Mongoid::Errors::MongoidError.new)
Expand Down Expand Up @@ -241,7 +256,7 @@ class HomepageControllerTest < ActionController::TestCase

post :publish, params: { id: @popular_links.id }

assert_equal "Popular links publish was unsuccessful, cannot publish an already published content item.", flash[:danger]
assert_equal "Popular links publish was unsuccessful, cannot publish an already published edition.", flash[:danger]
end

should "alert 'unsuccessful due to a service problem'" do
Expand All @@ -253,7 +268,6 @@ class HomepageControllerTest < ActionController::TestCase
end
end
end

context "#confirm_destroy" do
should "render confirm destroy page for draft edition" do
popular_links = FactoryBot.create(:popular_links, state: "draft")
Expand Down Expand Up @@ -344,7 +358,7 @@ def stub_publishing_api_publish_already_published_error
status: 409,
body: {
"error" => {
"code" => 409, "message" => "Cannot publish an already published content item"
"code" => 409, "message" => "Cannot publish an already published edition"
},
}.to_json,
)
Expand Down

0 comments on commit 681ba14

Please sign in to comment.