From 79d684b3485f5ac26659cea7debedb589f42d55a Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Mon, 19 Aug 2024 14:55:35 +0100 Subject: [PATCH] Refactor save before publish Save only if state is draft Improve error wordings to reflect publishing api error response --- app/controllers/homepage_controller.rb | 4 +-- app/models/popular_links_edition.rb | 8 ++++- test/functional/homepage_controller_test.rb | 40 ++++++++++++++------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/app/controllers/homepage_controller.rb b/app/controllers/homepage_controller.rb index 5af775c40..91d424e2d 100644 --- a/app/controllers/homepage_controller.rb +++ b/app/controllers/homepage_controller.rb @@ -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 diff --git a/app/models/popular_links_edition.rb b/app/models/popular_links_edition.rb index 080f7136f..7704b53a9 100644 --- a/app/models/popular_links_edition.rb +++ b/app/models/popular_links_edition.rb @@ -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 @@ -61,6 +61,12 @@ def locale end def can_delete? + is_draft? + end + +private + + def is_draft? state == "draft" end end diff --git a/test/functional/homepage_controller_test.rb b/test/functional/homepage_controller_test.rb index 2727eeacf..24fe06dc3 100644 --- a/test/functional/homepage_controller_test.rb +++ b/test/functional/homepage_controller_test.rb @@ -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 @@ -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) @@ -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 @@ -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") @@ -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, )