Skip to content

Commit

Permalink
Fixes #38011 - Apply environment filter for content override
Browse files Browse the repository at this point in the history
  • Loading branch information
nadjaheitmann committed Nov 20, 2024
1 parent 02d271b commit 3bcc7e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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")
Expand All @@ -205,8 +205,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
Expand Down
32 changes: 32 additions & 0 deletions test/controllers/api/v2/host_subscriptions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,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)
Expand Down

0 comments on commit 3bcc7e6

Please sign in to comment.