From aca18f6a73db25131f6f9257617e1a7be91d9839 Mon Sep 17 00:00:00 2001 From: Andrew Kofink Date: Wed, 6 Jun 2018 14:26:32 -0400 Subject: [PATCH] Fixes #23837: prepend ActionTriggering methods --- .../subscription_facet_host_extensions.rb | 16 ++++++---- .../content_facet_host_extensions_test.rb | 32 +++++++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/models/katello/concerns/subscription_facet_host_extensions.rb b/app/models/katello/concerns/subscription_facet_host_extensions.rb index cb411415641..3eb63a4a907 100644 --- a/app/models/katello/concerns/subscription_facet_host_extensions.rb +++ b/app/models/katello/concerns/subscription_facet_host_extensions.rb @@ -16,6 +16,16 @@ module SubscriptionFacetHostExtensions prepend ForemanTasks::Concerns::ActionTriggering + module Prepended + def update_action + if subscription_facet.try(:backend_update_needed?) + ::Actions::Katello::Host::Update + end + end + end + + prepend Prepended + accepts_nested_attributes_for :subscription_facet, :update_only => true, :reject_if => lambda { |attrs| attrs.values.compact.empty? } has_many :activation_keys, :through => :subscription_facet @@ -43,12 +53,6 @@ module SubscriptionFacetHostExtensions scoped_search :on => :id, :relation => :pools, :rename => :subscription_id, :complete_value => true, :ext_method => :find_by_subscription_id end - def update_action - if subscription_facet.try(:backend_update_needed?) - ::Actions::Katello::Host::Update - end - end - module ClassMethods def find_by_activation_key(_key, operator, value) conditions = sanitize_sql_for_conditions(["#{Katello::ActivationKey.table_name}.name #{operator} ?", value_to_sql(operator, value)]) diff --git a/test/models/concerns/content_facet_host_extensions_test.rb b/test/models/concerns/content_facet_host_extensions_test.rb index e93676ba701..e9c698e5bb1 100644 --- a/test/models/concerns/content_facet_host_extensions_test.rb +++ b/test/models/concerns/content_facet_host_extensions_test.rb @@ -11,8 +11,9 @@ class ContentFacetHostExtensionsBaseTest < ActiveSupport::TestCase let(:dev) { katello_environments(:dev) } let(:empty_host) { ::Host::Managed.create!(:name => 'foobar', :managed => false) } let(:host) do - FactoryBot.create(:host, :with_content, :content_view => view, - :lifecycle_environment => library) + FactoryBot.create(:host, :with_content, + :with_subscription, :content_view => view, + :lifecycle_environment => library) end let(:proxy) { FactoryBot.create(:smart_proxy, :url => 'http://fakepath.com/foo') } end @@ -25,6 +26,33 @@ def test_in_content_view_environment refute_includes ::Host.in_content_view_environment(:content_view => view, :lifecycle_environment => dev), host end + def test_action_not_triggered_on_facet_no_change + ForemanTasks.dynflow.world.expects(:plan).never + host.update_attributes!(:content_facet_attributes => { :content_view_id => view.id }) + end + + def test_action_triggered_on_facet_update + host.stubs(:execute_planned_action) + + ForemanTasks.dynflow.world + .expects(:plan).with(::Actions::Katello::Host::Update, host) + .returns(OpenStruct.new(error: false)) + host.update_attributes!(:content_facet_attributes => { :content_view_id => view2.id }) + + host.reload + host.content_facet.content_view_id = view.id + ForemanTasks.dynflow.world + .expects(:plan).with(::Actions::Katello::Host::Update, host) + .returns(OpenStruct.new(error: false)) + host.save! + + host.reload + ForemanTasks.dynflow.world + .expects(:plan).with(::Actions::Katello::Host::Update, host) + .returns(OpenStruct.new(error: false)) + host.update_attributes!(:content_facet_attributes => { :lifecycle_environment_id => dev.id }) + end + def test_content_facet_update host.update_attributes!(:content_facet_attributes => { :content_view_id => view2.id }) host.reload.content_facet.reload