From 272e9f9732e0ad04756ebb61fc55de563cab2d5d Mon Sep 17 00:00:00 2001 From: Nadja Heitmann Date: Mon, 11 Nov 2024 13:44:37 +0000 Subject: [PATCH] Fixes #38011 - Apply environment filter for content override --- .../api/v2/host_subscriptions_controller.rb | 6 ++-- .../v2/host_subscriptions_controller_test.rb | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/controllers/katello/api/v2/host_subscriptions_controller.rb b/app/controllers/katello/api/v2/host_subscriptions_controller.rb index fa747bb9e19..097bb169ef1 100644 --- a/app/controllers/katello/api/v2/host_subscriptions_controller.rb +++ b/app/controllers/katello/api/v2/host_subscriptions_controller.rb @@ -184,7 +184,7 @@ def content_override validate_content_overrides_enabled(override_params) end sync_task(::Actions::Katello::Host::UpdateContentOverrides, @host, content_override_values, false) - fetch_product_content + fetch_product_content(!params.dig(:content_overrides_search, :search).nil? && Foreman::Cast.to_bool(params.dig(:content_overrides_search, :limit_to_env))) end api :GET, "/hosts/:host_id/subscriptions/available_release_versions", N_("Show releases available for the content host") @@ -203,8 +203,8 @@ def enabled_repositories private - def fetch_product_content - content_finder = ProductContentFinder.new(:consumable => @host.subscription_facet) + def fetch_product_content(limit_to_env = false) + content_finder = ProductContentFinder.new(:match_environment => limit_to_env, :consumable => @host.subscription_facet) content = content_finder.presenter_with_overrides(@host.subscription_facet.candlepin_consumer.content_overrides) respond_with_template_collection("index", 'repository_sets', :collection => full_result_response(content)) end diff --git a/test/controllers/api/v2/host_subscriptions_controller_test.rb b/test/controllers/api/v2/host_subscriptions_controller_test.rb index eaad900c11e..7af50624e94 100644 --- a/test/controllers/api/v2/host_subscriptions_controller_test.rb +++ b/test/controllers/api/v2/host_subscriptions_controller_test.rb @@ -239,6 +239,38 @@ def test_find_content_overrides_with_empty_string_search refute_equal result, "wrong" end + def test_fetch_product_content_with_respect_to_environment + # Create fake product content and stub ProductContentFinder + pcf = mock + pcf.stubs(:presenter_with_overrides) + + controller = ::Katello::Api::V2::HostSubscriptionsController.new + controller.stubs(:respond_with_template_collection) + controller.stubs(:sync_task) + controller.stubs(:validate_content_overrides_enabled) + controller.stubs(:full_result_response) + controller.instance_variable_set(:@host, @host) + controller.instance_variable_set(:@content_overrides, [{:content_label => 'some-content', :value => 1}]) + + # Actual tests that expect the correct parameters to be passed to 'content_override' + ProductContentFinder.expects(:new).with(match_environment: true, consumable: @host.subscription_facet).returns(pcf) + ProductContentFinder.expects(:new).with(match_environment: false, consumable: @host.subscription_facet).returns(pcf).times(3) + + # Invoke the call to the controller with a variaty of parameters + # Try with limit_to_env enabled + controller.params = { :host_id => @host.id, :content_overrides_search => { :search => '', :limit_to_env => true} } + controller.send(:content_override) + # Try with limit_to_env disabled + controller.params = { :host_id => @host.id, :content_overrides_search => { :search => '', :limit_to_env => false} } + controller.send(:content_override) + # Try with limit_to_env not set - should be the same as disabled + controller.params = { :host_id => @host.id, :content_overrides_search => { :search => ''} } + controller.send(:content_override) + # Try without search params - should be the same as disabled + controller.params = { :host_id => @host.id} + controller.send(:content_override) + end + def test_find_content_overrides_with_empty_string_search_limited_to_environment # Create Host with "fedora" and "rhel" as content content_view = katello_content_views(:library_dev_view)