Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/controllers/api/v2/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/routes/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions test/controllers/api/v2/hide_taxonomy_options_test.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/unit/foreman/access_permissions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading