Skip to content

Commit

Permalink
Fixes #27259 - Fix hostgroup-asset association (#568)
Browse files Browse the repository at this point in the history
Because of the wrong cardinality, only first asset got removed on
hostgroup destroy, leading to orphaned assets being left around.
  • Loading branch information
adamruzicka authored May 14, 2024
1 parent 776104b commit 4b607c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/concerns/foreman_openscap/hostgroup_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module HostgroupExtensions
include InheritedPolicies

included do
has_one :asset, :as => :assetable, :class_name => "::ForemanOpenscap::Asset", dependent: :destroy
has_many :asset_policies, :through => :asset, :class_name => "::ForemanOpenscap::AssetPolicy"
has_many :assets, :as => :assetable, :class_name => "::ForemanOpenscap::Asset", dependent: :destroy
has_many :asset_policies, :through => :assets, :class_name => "::ForemanOpenscap::AssetPolicy"
has_many :policies, :through => :asset_policies, :class_name => "::ForemanOpenscap::Policy"
end

Expand Down
10 changes: 10 additions & 0 deletions test/unit/concerns/hostgroup_extensions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'test_plugin_helper'

class HostgroupExtensionsTest < ActiveSupport::TestCase
test "should remove all linked assets on hostgroup destroy" do
hostgroup = FactoryBot.create(:hostgroup)
FactoryBot.create_list(:asset, 3, :assetable_id => hostgroup.id, :assetable_type => 'Hostgroup')
asset_scope = ::ForemanOpenscap::Asset.where(:assetable_id => hostgroup.id, :assetable_type => 'Hostgroup')
assert_difference("asset_scope.count", -3) { hostgroup.destroy }
end
end

0 comments on commit 4b607c8

Please sign in to comment.