From 0b48e84c2c6f7c0d0f8d607e29332f5202c2fc52 Mon Sep 17 00:00:00 2001 From: Ana Botto Date: Tue, 9 Jul 2024 12:25:59 +0100 Subject: [PATCH] Publish popular links --- app/controllers/homepage_controller.rb | 21 +++--------- app/models/popular_links_edition.rb | 11 +++++++ test/functional/homepage_controller_test.rb | 32 +++++++++++-------- .../homepage_popular_links_test.rb | 15 +++++++++ 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/app/controllers/homepage_controller.rb b/app/controllers/homepage_controller.rb index f9c701fc4..19dbf3fee 100644 --- a/app/controllers/homepage_controller.rb +++ b/app/controllers/homepage_controller.rb @@ -16,9 +16,7 @@ def edit end def update - update_link_items - UpdateWorker.perform_async(@latest_popular_links.id.to_s, update_action_is_publish?) - + @latest_popular_links.update_draft(update_link_items) flash[:success] = "Popular links draft saved.".html_safe redirect_to show_popular_links_path rescue StandardError @@ -26,7 +24,8 @@ def update end def publish - publish_latest_popular_links + @latest_popular_links.publish + flash[:success] = "Popular links successfully published.".html_safe render "homepage/popular_links/show" end @@ -38,7 +37,7 @@ def fetch_latest_popular_link def update_link_items @latest_popular_links.link_items = remove_leading_and_trailing_url_spaces(params[:popular_links].values) - @latest_popular_links.save! + @latest_popular_links end def remove_leading_and_trailing_url_spaces(links) @@ -49,16 +48,4 @@ def remove_leading_and_trailing_url_spaces(links) end link_items end - - def publish_latest_popular_links - @latest_popular_links.publish_popular_links - end - - def update_action_is_publish? - attempted_activity == :publish - end - - def attempted_activity - Edition::ACTIONS.invert[params[:commit]] - end end diff --git a/app/models/popular_links_edition.rb b/app/models/popular_links_edition.rb index 693943c85..63d6d9775 100644 --- a/app/models/popular_links_edition.rb +++ b/app/models/popular_links_edition.rb @@ -34,6 +34,17 @@ def create_draft_popular_links_from_last_record popular_links end + def publish + Services.publishing_api.publish(content_id, "major", locale: "en") + # This publish_popular_links is a new workflow that was introduced for popular links. + publish_popular_links + end + + def update_draft(updated_popular_links) + UpdateService.call(updated_popular_links) + updated_popular_links.save! + end + def content_id "ad7968d0-0339-40b2-80bc-3ea1db8ef1b7".freeze end diff --git a/test/functional/homepage_controller_test.rb b/test/functional/homepage_controller_test.rb index 376eccb17..544abc7ef 100644 --- a/test/functional/homepage_controller_test.rb +++ b/test/functional/homepage_controller_test.rb @@ -58,7 +58,7 @@ class HomepageControllerTest < ActionController::TestCase end should "update latest PopularLinksEdition with changed title and url" do - UpdateWorker.stubs(:perform_async) + UpdateService.stubs(:call) assert_equal "title1", @popular_links.link_items[0][:title] assert_equal "https://www.url1.com", @popular_links.link_items[0][:url] @@ -79,22 +79,20 @@ class HomepageControllerTest < ActionController::TestCase end should "update publishing API" do - Sidekiq::Testing.inline! do - Services.publishing_api.expects(:put_content) - - patch :update, params: { id: @popular_links.id, - "popular_links" => - { "1" => { "title" => "title", "url" => "url.com" }, - "2" => { "title" => "title2", "url" => "https://www.url2.com" }, - "3" => { "title" => "title3", "url" => "https://www.url3.com" }, - "4" => { "title" => "title4", "url" => "https://www.url4.com" }, - "5" => { "title" => "title5", "url" => "https://www.url5.com" }, - "6" => { "title" => "title6", "url" => "https://www.url6.com" } } } - end + Services.publishing_api.expects(:put_content) + + patch :update, params: { id: @popular_links.id, + "popular_links" => + { "1" => { "title" => "title", "url" => "url.com" }, + "2" => { "title" => "title2", "url" => "https://www.url2.com" }, + "3" => { "title" => "title3", "url" => "https://www.url3.com" }, + "4" => { "title" => "title4", "url" => "https://www.url4.com" }, + "5" => { "title" => "title5", "url" => "https://www.url5.com" }, + "6" => { "title" => "title6", "url" => "https://www.url6.com" } } } end should "redirect to show path on success" do - UpdateWorker.stubs(:perform_async) + UpdateService.stubs(:call) new_title = "title has changed" patch :update, params: { id: @popular_links.id, @@ -132,5 +130,11 @@ class HomepageControllerTest < ActionController::TestCase assert_template "homepage/popular_links/show" assert_equal "published", PopularLinksEdition.last.state end + + should "publish to publishing API" do + Services.publishing_api.expects(:publish) + + post :publish, params: { id: @popular_links.id } + end end end diff --git a/test/integration/homepage_popular_links_test.rb b/test/integration/homepage_popular_links_test.rb index 617dde2b3..9a870b63e 100644 --- a/test/integration/homepage_popular_links_test.rb +++ b/test/integration/homepage_popular_links_test.rb @@ -78,6 +78,7 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest context "#edit" do setup do + UpdateService.stubs(:call) click_button("Create new edition") click_link("Edit popular links") end @@ -135,6 +136,20 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest end end + context "#publish" do + setup do + Services.publishing_api.stubs(:publish) + click_button("Create new edition") + end + + should "publish latest edition when 'Publish' is clicked" do + click_button("Publish") + + assert page.has_text?("PUBLISHED") + assert page.has_text?("Popular links successfully published.") + end + end + def visit_popular_links visit "/homepage/popular-links" end