Skip to content

Commit

Permalink
Refs #37772 - allow for clearing of CVEs on AK
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylenz committed Sep 17, 2024
1 parent d4ca55d commit 534e69d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
31 changes: 25 additions & 6 deletions app/controllers/katello/api/v2/activation_keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def index
param_group :activation_key
def create
@activation_key = ActivationKey.new(activation_key_params) do |activation_key|
activation_key.content_view_environments = @content_view_environments if @content_view_environments
activation_key.content_view_environments = @content_view_environments if update_cves?
activation_key.organization = @organization
activation_key.user = current_user
end
Expand All @@ -80,7 +80,7 @@ def create
param_group :activation_key
param :id, :number, :desc => N_("ID of the activation key"), :required => true
def update
@activation_key.update!(content_view_environments: @content_view_environments) if @content_view_environments.present?
@activation_key.update!(content_view_environments: @content_view_environments) if update_cves?
sync_task(::Actions::Katello::ActivationKey::Update, @activation_key, activation_key_params)
respond_for_show(:resource => @activation_key)
end
Expand Down Expand Up @@ -298,17 +298,36 @@ def find_content_view_environments
find_cve_for_single
elsif params[:content_view_environments] || params[:content_view_environment_ids]
@content_view_environments = ::Katello::ContentViewEnvironment.fetch_content_view_environments(
candlepin_names: params[:content_view_environments],
ids: params[:content_view_environment_ids],
organization: @organization || @activation_key&.organization)
candlepin_names: params[:content_view_environments],
ids: params[:content_view_environment_ids],
organization: @organization || @activation_key&.organization)
if @content_view_environments.blank?
handle_errors(candlepin_names: params[:content_view_environments],
ids: params[:content_view_environment_ids])
ids: params[:content_view_environment_ids])
end
end
handle_blank_cve_params
@organization ||= @content_view_environments.first&.organization
end

def handle_blank_cve_params
# Activation keys do not require CVEs to be associated. So it's possible the user intends to clear them.
if params.key?(:environment_id) && params[:environment_id].blank? && params.key?(:content_view_id) && params[:content_view_id].blank?
@content_view_environments = []
elsif params.key?(:content_view_environments) && params[:content_view_environments].blank?
@content_view_environments = []
elsif params.key?(:content_view_environment_ids) && params[:content_view_environment_ids].blank?
@content_view_environments = []
end
end

def update_cves?
(params.key?(:environment_id) && params.key?(:content_view_id)) || # single
(params.key?(:environment) && params.key?(:content_view)) ||
params.key?(:content_view_environments) || # multi
params.key?(:content_view_environment_ids)
end

def find_host_collections
ids = params[:activation_key][:host_collection_ids] if params[:activation_key]
@host_collections = []
Expand Down
55 changes: 55 additions & 0 deletions test/controllers/api/v2/activation_keys_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,61 @@ def test_should_not_update_with_invalid_content_view_environment_ids_param
assert_response :unprocessable_entity
end

def test_update_with_cleared_cves
cve = katello_content_view_environments(:library_dev_view_library)
cve.update(organization: @organization)
assert_sync_task(::Actions::Katello::ActivationKey::Update) do |activation_key, _activation_key_params|
assert_valid activation_key
end
assert_operator @activation_key.content_view_environments.size, :>, 0

put :update, params: {
:organization_id => @organization.id,
:id => @activation_key.id,
:content_view_environments => []
}

assert_response :success
assert_equal 0, @activation_key.content_view_environments.size
end

def test_update_with_cleared_cve_ids
cve = katello_content_view_environments(:library_dev_view_library)
cve.update(organization: @organization)
assert_sync_task(::Actions::Katello::ActivationKey::Update) do |activation_key, _activation_key_params|
assert_valid activation_key
end
assert_operator @activation_key.content_view_environments.size, :>, 0

put :update, params: {
:organization_id => @organization.id,
:id => @activation_key.id,
:content_view_environment_ids => []
}

assert_response :success
assert_equal 0, @activation_key.content_view_environments.size
end

def test_update_with_cleared_cv_lce_ids
cve = katello_content_view_environments(:library_dev_view_library)
cve.update(organization: @organization)
assert_sync_task(::Actions::Katello::ActivationKey::Update) do |activation_key, _activation_key_params|
assert_valid activation_key
end
assert_operator @activation_key.content_view_environments.size, :>, 0

put :update, params: {
:organization_id => @organization.id,
:id => @activation_key.id,
:content_view_id => nil,
:environment_id => nil
}

assert_response :success
assert_equal 0, @activation_key.content_view_environments.size
end

test_attributes :pid => 'ec225dad-2d27-4b37-989d-1ba2c7f74ac4'
def test_update_auto_attach
new_auto_attach = !@activation_key.auto_attach
Expand Down

0 comments on commit 534e69d

Please sign in to comment.