diff --git a/app/controllers/api/v2/base_controller.rb b/app/controllers/api/v2/base_controller.rb index 0f414c286c..f2ad7b87e7 100644 --- a/app/controllers/api/v2/base_controller.rb +++ b/app/controllers/api/v2/base_controller.rb @@ -164,10 +164,17 @@ def render_error(error, options = { }) def self.hide_taxonomy_options prepend_before_action :drop_taxonomy_id_from_params - resource_description do - param :location_id, Integer, :show => false - param :organization_id, Integer, :show => false + + original_method = method(:resource_description) + define_singleton_method(:resource_description) do |options = {}, &block| + original_method.call(options) do + instance_exec(&block) if block + param :location_id, Integer, :show => false + param :organization_id, Integer, :show => false + end end + + resource_description end def drop_taxonomy_id_from_params diff --git a/config/routes/test.rb b/config/routes/test.rb index d036e90f91..e7c812afac 100644 --- a/config/routes/test.rb +++ b/config/routes/test.rb @@ -5,6 +5,8 @@ namespace :api do namespace :v2 do resources :testable, :only => [:create, :index, :new] + resources :hide_taxonomy_simple, :only => :index + resources :hide_taxonomy_with_description, :only => :index end resources :testable, :only => :index do diff --git a/test/controllers/api/v2/hide_taxonomy_options_test.rb b/test/controllers/api/v2/hide_taxonomy_options_test.rb new file mode 100644 index 0000000000..9dcf01645f --- /dev/null +++ b/test/controllers/api/v2/hide_taxonomy_options_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' + +# Controller that calls hide_taxonomy_options WITHOUT a subsequent resource_description. +# This simulates existing controllers like ArchitecturesController. +class Api::V2::HideTaxonomySimpleController < Api::V2::BaseController + hide_taxonomy_options + + def index + render :json => { :location_id => params[:location_id], :organization_id => params[:organization_id] }, :status => :ok + end +end + +# Controller that calls hide_taxonomy_options WITH a subsequent resource_description. +# This simulates plugin controllers (e.g. foreman_ansible) where resource_description +# with api_base_url follows hide_taxonomy_options. +class Api::V2::HideTaxonomyWithDescriptionController < Api::V2::BaseController + hide_taxonomy_options + + resource_description do + api_version 'v2' + api_base_url '/test_plugin/api' + end + + def index + render :json => { :location_id => params[:location_id], :organization_id => params[:organization_id] }, :status => :ok + end +end + +class Api::V2::HideTaxonomySimpleControllerTest < ActionController::TestCase + tests Api::V2::HideTaxonomySimpleController + + test 'should drop taxonomy params from request' do + get :index, params: { :location_id => 1, :organization_id => 2 }, session: set_session_user + assert_response :success + body = JSON.parse(response.body) + assert_nil body['location_id'] + assert_nil body['organization_id'] + end + + test 'should succeed without taxonomy params' do + get :index, session: set_session_user + assert_response :success + end +end + +class Api::V2::HideTaxonomyWithDescriptionControllerTest < ActionController::TestCase + tests Api::V2::HideTaxonomyWithDescriptionController + + test 'should drop taxonomy params even when resource_description follows hide_taxonomy_options' do + get :index, params: { :location_id => 1, :organization_id => 2 }, session: set_session_user + assert_response :success + body = JSON.parse(response.body) + assert_nil body['location_id'] + assert_nil body['organization_id'] + end + + test 'should succeed without taxonomy params' do + get :index, session: set_session_user + assert_response :success + end +end diff --git a/test/unit/foreman/access_permissions_test.rb b/test/unit/foreman/access_permissions_test.rb index aed5b957b6..ee8839afd7 100644 --- a/test/unit/foreman/access_permissions_test.rb +++ b/test/unit/foreman/access_permissions_test.rb @@ -37,6 +37,7 @@ class AccessPermissionsTest < ActiveSupport::TestCase "testable/index", "api/testable/index", "api/testable/raise_error", "api/testable/required_nested_values", "api/testable/optional_nested_values", "api/testable/nested_values", "api/v2/testable/index", "api/v2/testable/create", "api/v2/testable/new", "fake/index", "api/v2/fake/index", + "api/v2/hide_taxonomy_simple/index", "api/v2/hide_taxonomy_with_description/index", # test stubs "testable_resources/index",