Skip to content

Commit

Permalink
Refs #37861 - handle mixed validity CVE params
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Oct 3, 2024
1 parent 8dc5099 commit d40c273
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
31 changes: 24 additions & 7 deletions app/models/katello/content_view_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,37 @@ def priority(content_object)
end

def self.fetch_content_view_environments(labels: [], ids: [], organization:)
# Must do maps here to ensure CVEs remain in the same order.
# Must ensure CVEs remain in the same order.
# Using ActiveRecord .where will return them in a different order.
id_errors = []
label_errors = []
cves = []
if ids.present?
cves = ids.map do |id|
::Katello::ContentViewEnvironment.find_by(id: id)
ids.each do |id|
cve = ::Katello::ContentViewEnvironment.find_by(id: id)
if cve.blank?
id_errors << id
else
cves << cve
end
end
cves.compact
elsif labels.present?
environment_names = labels.map(&:strip)
environment_names.map! do |name|
with_candlepin_name(name, organization: organization)
environment_names.each do |name|
cve = with_candlepin_name(name, organization: organization)
if cve.blank?
label_errors << name
else
cves << cve
end
end
environment_names.compact
end
if labels.present? && labels.length != cves.length
fail HttpErrors::UnprocessableEntity, _("No content view environments found with names: %{names}") % {names: label_errors.join(', ')} if label_errors.present?
elsif ids.present? && ids.length != cves.length
fail HttpErrors::UnprocessableEntity, _("No content view environments found with ids: %{ids}") % {ids: id_errors.join(', ')} if id_errors.present?
end
cves
end

private
Expand Down
22 changes: 21 additions & 1 deletion test/models/content_view_environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,28 @@ def test_fetch_content_view_environments_ids
def test_fetch_content_view_environments_invalid_ids_does_not_mutate_array
dev = katello_environments(:dev)
input_ids = [0, 999]
assert_equal [], ContentViewEnvironment.fetch_content_view_environments(ids: input_ids, organization: dev.organization)
assert_raises(HttpErrors::UnprocessableEntity) do
ContentViewEnvironment.fetch_content_view_environments(ids: input_ids, organization: dev.organization)
end
assert_equal [0, 999], input_ids # should not have a map! which mutates the input array
end

def test_fetch_content_view_environments_mixed_validity_candlepin_names
dev = katello_environments(:dev)
view = katello_content_views(:library_dev_view)
cve = Katello::ContentViewEnvironment.where(:environment_id => dev, :content_view_id => view).first
assert_raises(HttpErrors::UnprocessableEntity) do
ContentViewEnvironment.fetch_content_view_environments(labels: ['published_dev_view_dev, bogus'], organization: dev.organization)
end
end

def test_fetch_content_view_environments_mixed_validity_ids
dev = katello_environments(:dev)
view = katello_content_views(:library_dev_view)
cve = Katello::ContentViewEnvironment.where(:environment_id => dev, :content_view_id => view).first
assert_raises(HttpErrors::UnprocessableEntity) do
ContentViewEnvironment.fetch_content_view_environments(ids: [cve.id, 9999], organization: dev.organization)
end
end
end
end

0 comments on commit d40c273

Please sign in to comment.