diff --git a/app/controllers/katello/api/v2/host_subscriptions_controller.rb b/app/controllers/katello/api/v2/host_subscriptions_controller.rb index 9b8244bb39e..408cbd957a7 100644 --- a/app/controllers/katello/api/v2/host_subscriptions_controller.rb +++ b/app/controllers/katello/api/v2/host_subscriptions_controller.rb @@ -242,20 +242,18 @@ def action_permission def find_content_overrides if !params.dig(:content_overrides_search, :search).nil? - content_labels = ::Katello::Content.joins(:product_contents) - .where("#{Katello::ProductContent.table_name}.product_id": @host.organization.products.subscribable.enabled) - .search_for(params[:content_overrides_search][:search]) - .pluck(:label) - - if Foreman::Cast.to_bool(params.dig(:content_overrides_search, :limit_to_env)) - env_content = ProductContentFinder.new( - :match_subscription => false, - :match_environment => true, - :consumable => @host.subscription_facet - ).product_content - env_content_labels = ::Katello::Content.find(env_content.pluck(:content_id)).pluck(:label) - content_labels &= env_content_labels - end + content = ::Katello::Content.joins(:product_contents) + .where("#{Katello::ProductContent.table_name}.product_id": @host.organization.products.subscribable.enabled) + + env_content = ProductContentFinder.new( + :match_subscription => false, + :match_environment => Foreman::Cast.to_bool(params.dig(:content_overrides_search, :limit_to_env)), + :consumable => @host.subscription_facet + ).product_content + content = content.where(id: env_content.pluck(:content_id)) + + content_labels = content.search_for(params[:content_overrides_search][:search]) + .pluck(:label) @content_overrides = content_labels.map do |label| { content_label: label, diff --git a/test/controllers/api/v2/host_subscriptions_controller_test.rb b/test/controllers/api/v2/host_subscriptions_controller_test.rb index 0b8a3e5d235..7ff5ca61197 100644 --- a/test/controllers/api/v2/host_subscriptions_controller_test.rb +++ b/test/controllers/api/v2/host_subscriptions_controller_test.rb @@ -273,6 +273,38 @@ def test_find_content_overrides_with_empty_string_search_limited_to_environment assert_equal(label, result[0][:content_label]) end + def test_find_content_overrides_with_inverse_search_limited_to_environment_with_structured_apt + # enable structured_apt + Foreman.settings.set_user_value('deb_enable_structured_apt', true) + # Create Host with "deb" as content + content_view = katello_content_views(:library_dev_view) + library = katello_environments(:library) + activation_key = katello_activation_keys(:library_dev_staging_view_key) + host_collection = katello_host_collections(:simple_host_collection) + activation_key.host_collections << host_collection + + host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => content_view, + :lifecycle_environment => library, :organization => content_view.organization) + + # Get content_id and label of first product of host + products = ::Katello::Content.joins(:product_contents) + .where("#{Katello::ProductContent.table_name}.product_id": host.organization.products.subscribable.enabled) + in_env_id = products.pluck(:content_id)[2] + + # Create fake product with content_id and stub ProductContentFinder + content = katello_contents(:deb_content_v1) + pc = [FactoryBot.build(:katello_product_content, content: content, content_id: in_env_id)] + ProductContentFinder.any_instance.stubs(:product_content).returns(pc) + + controller = ::Katello::Api::V2::HostSubscriptionsController.new + controller.params = { :host_id => host.id, :content_overrides_search => { :search => "cp_content_id !^ (#{content.cp_content_id})", :limit_to_env => true} } + controller.instance_variable_set(:@host, host) + controller.send(:find_content_overrides) + + result = controller.instance_variable_get(:@content_overrides) + assert_equal(0, result.length) + end + def test_content_override_bulk content_overrides = [{:content_label => 'some-content', :value => 1}] expected_content_labels = content_overrides.map { |co| co[:content_label] } diff --git a/test/fixtures/models/katello_contents.yml b/test/fixtures/models/katello_contents.yml index 04deeb7a666..19cdc4ca4ae 100644 --- a/test/fixtures/models/katello_contents.yml +++ b/test/fixtures/models/katello_contents.yml @@ -1,3 +1,19 @@ +deb_content_v1: + name: debian + label: debian + cp_content_id: 110 + organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %> + content_url: /custom/Debian_12/Debian_12_amd64_main/?comp=main&rel=bookworm + content_type: "deb" + +deb_content_v2: + name: debian + label: debian + cp_content_id: 112 + organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %> + content_url: /custom/Debian_12/Debian_12_amd64_main/?comp=main&rel=bookworm + content_type: "deb" + some_content: name: Fedora cp_content_id: 1 @@ -12,4 +28,4 @@ rhel_content: cp_content_id: 69 organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %> content_url: /content/dist/rhel/server/$releasever/$basearch/os - content_type: "yum" \ No newline at end of file + content_type: "yum" diff --git a/test/fixtures/models/katello_product_contents.yml b/test/fixtures/models/katello_product_contents.yml index b15542c2de3..537071c31e0 100644 --- a/test/fixtures/models/katello_product_contents.yml +++ b/test/fixtures/models/katello_product_contents.yml @@ -7,3 +7,13 @@ rhel_content: content_id: <%= ActiveRecord::FixtureSet.identify(:rhel_content) %> product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %> enabled: false + +debian_content_v1: + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v1) %> + product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %> + enabled: false + +debian_content_v2: + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v2) %> + product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %> + enabled: false diff --git a/test/fixtures/models/katello_repositories.yml b/test/fixtures/models/katello_repositories.yml index a706767cfc1..54ca879da38 100644 --- a/test/fixtures/models/katello_repositories.yml +++ b/test/fixtures/models/katello_repositories.yml @@ -39,6 +39,7 @@ debian_10_dev_library_view: relative_path: 'ACME_Corporation/dev/debian_10_library_library_view_label' environment_id: <%= ActiveRecord::FixtureSet.identify(:dev) %> content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_view_version_2) %> + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v2) %> debian_10_amd64_dev: library_instance: debian_10_amd64 @@ -47,6 +48,7 @@ debian_10_amd64_dev: relative_path: 'ACME_Corporation/dev/debian_10_dev_label' environment_id: <%= ActiveRecord::FixtureSet.identify(:dev) %> content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_dev_view_version) %> + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v1) %> distribution_arch: "amd64" distribution_version: "10.1" distribution_family: "Debian"