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..faf9f0f7298 100644 --- a/test/controllers/api/v2/host_subscriptions_controller_test.rb +++ b/test/controllers/api/v2/host_subscriptions_controller_test.rb @@ -273,6 +273,31 @@ 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 + # 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) + + # Create fake product with content_id and stub ProductContentFinder + content = katello_contents(:deb_content_v1) + pc = [FactoryBot.build(:katello_product_content, content: content)] + 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_content_view_repositories.yml b/test/fixtures/models/katello_content_view_repositories.yml index 137b1e61bcd..7e80607c1b5 100644 --- a/test/fixtures/models/katello_content_view_repositories.yml +++ b/test/fixtures/models/katello_content_view_repositories.yml @@ -6,6 +6,10 @@ library_view_debian_10_amd64: content_view_id: <%= ActiveRecord::FixtureSet.identify(:library_view) %> repository_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64) %> +library_view_acme_debian_10_amd64: + content_view_id: <%= ActiveRecord::FixtureSet.identify(:acme_default) %> + repository_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_dev) %> + library_view_rhel_6_x86_64: content_view_id: <%= ActiveRecord::FixtureSet.identify(:acme_default) %> repository_id: <%= ActiveRecord::FixtureSet.identify(:rhel_6_x86_64) %> diff --git a/test/fixtures/models/katello_contents.yml b/test/fixtures/models/katello_contents.yml index 04deeb7a666..7534889692c 100644 --- a/test/fixtures/models/katello_contents.yml +++ b/test/fixtures/models/katello_contents.yml @@ -1,3 +1,35 @@ +deb_content: + 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_v1: + name: debian + label: debian + cp_content_id: 111 + 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" + +deb_content_v3: + name: debian + label: debian + cp_content_id: 113 + organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %> + content_url: /custom/Debian_12/Debian_10_amd64_main/?comp=main&rel=buster + content_type: "deb" + some_content: name: Fedora cp_content_id: 1 @@ -12,4 +44,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_pool_products.yml b/test/fixtures/models/katello_pool_products.yml index 2591ca53850..e405e0f2504 100644 --- a/test/fixtures/models/katello_pool_products.yml +++ b/test/fixtures/models/katello_pool_products.yml @@ -6,6 +6,10 @@ basic_redhat: pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_one) %> product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %> +basic_debian: + pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_one) %> + product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %> + other_sub_fedora: pool_id: <%= ActiveRecord::FixtureSet.identify(:pool_two) %> - product_id: <%= ActiveRecord::FixtureSet.identify(:fedora) %> \ No newline at end of file + product_id: <%= ActiveRecord::FixtureSet.identify(:fedora) %> diff --git a/test/fixtures/models/katello_product_contents.yml b/test/fixtures/models/katello_product_contents.yml index b15542c2de3..d066c2bbf4e 100644 --- a/test/fixtures/models/katello_product_contents.yml +++ b/test/fixtures/models/katello_product_contents.yml @@ -7,3 +7,23 @@ rhel_content: content_id: <%= ActiveRecord::FixtureSet.identify(:rhel_content) %> product_id: <%= ActiveRecord::FixtureSet.identify(:redhat) %> enabled: false + +debian_content: + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content) %> + product_id: <%= ActiveRecord::FixtureSet.identify(:debian) %> + 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 + +debian_content_v3: + content_id: <%= ActiveRecord::FixtureSet.identify(:deb_content_v3) %> + 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..06e9e53db38 100644 --- a/test/fixtures/models/katello_repositories.yml +++ b/test/fixtures/models/katello_repositories.yml @@ -15,6 +15,7 @@ debian_9_amd64: debian_10_amd64: root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %> pulp_id: Debian_10 + content_id: 110 relative_path: 'ACME_Corporation/library/debian_10_label' environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %> content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %> @@ -36,6 +37,7 @@ debian_10_dev_library_view: library_instance: debian_10_amd64 root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %> pulp_id: debian_10_dev_library_view + content_id: 112 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) %> @@ -44,6 +46,7 @@ debian_10_amd64_dev: library_instance: debian_10_amd64 root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %> pulp_id: 12 + content_id: 111 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) %> @@ -310,6 +313,15 @@ busybox_view2: environment_id: content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_view_version_2) %> +debian_10_amd64_composite_view_version_1: + library_instance: debian_10_amd64 + root_id: <%= ActiveRecord::FixtureSet.identify(:debian_10_amd64_root) %> + pulp_id: 8_deb_composite_version1 + content_id: 113 + relative_path: 'ACME_Corporation/library/composite/debian_10_label' + environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %> + content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:composite_view_version_1) %> + rhel_6_x86_64_composite_view_version_1: library_instance: rhel_6_x86_64 root_id: <%= ActiveRecord::FixtureSet.identify(:rhel_6_x86_64_root) %>