diff --git a/app/controllers/insights_cloud/settings_controller.rb b/app/controllers/insights_cloud/settings_controller.rb index 97942eee..dc5d4dab 100644 --- a/app/controllers/insights_cloud/settings_controller.rb +++ b/app/controllers/insights_cloud/settings_controller.rb @@ -1,7 +1,7 @@ module InsightsCloud class SettingsController < ::ApplicationController def show - if SETTINGS[:insights][:use_insights_on_premise] + if ForemanRhCloud.with_insights_on_premise? render json: { error: _('This Foreman is configured to use Insights on Premise. Syncing to Insights Cloud is disabled.'), }, status: :unprocessable_entity diff --git a/lib/foreman_rh_cloud/engine.rb b/lib/foreman_rh_cloud/engine.rb index 10cd06c4..89f7428f 100644 --- a/lib/foreman_rh_cloud/engine.rb +++ b/lib/foreman_rh_cloud/engine.rb @@ -113,7 +113,7 @@ def self.register_scheduled_task(task_class, cronline) url: '/foreman_rh_cloud/inventory_upload', url_hash: { controller: :react, action: :index }, parent: :configure_menu, - if: -> { !SETTINGS[:insights][:use_insights_on_premise] } + if: -> { !ForemanRhCloud.with_insights_on_premise? } menu :top_menu, :insights_hits, caption: N_('Recommendations'), url: '/foreman_rh_cloud/insights_cloud', url_hash: { controller: :react, action: :index }, parent: :configure_menu register_facet InsightsFacet, :insights do @@ -199,4 +199,8 @@ def self.register_scheduled_task(task_class, cronline) end end end + + def self.with_insights_on_premise? + SETTINGS&.[](:insights)&.[](:use_insights_on_premise) || false + end end diff --git a/test/controllers/insights_sync/settings_controller_test.rb b/test/controllers/insights_sync/settings_controller_test.rb index 23301a8f..05814e19 100644 --- a/test/controllers/insights_sync/settings_controller_test.rb +++ b/test/controllers/insights_sync/settings_controller_test.rb @@ -2,6 +2,19 @@ class SettingsControllerTest < ActionController::TestCase tests InsightsCloud::SettingsController + def setup + ForemanRhCloud.stubs(:with_insights_on_premise?).returns(false) + end + + test 'should return error if insights on premise' do + ForemanRhCloud.stubs(:with_insights_on_premise?).returns(true) + + get :show, session: set_session_user + + assert_response :unprocessable_entity + actual = JSON.parse(response.body) + assert_equal 'This Foreman is configured to use Insights on Premise. Syncing to Insights Cloud is disabled.', actual['error'] + end test 'should return allow_auto_insights_sync setting' do Setting[:allow_auto_insights_sync] = false