Project

General

Profile

« Previous | Next » 

Revision 46338cd7

Added by Daniel Lobato Garcia over 10 years ago

fixes #3515 - API handles not found objects with 404

View differences:

app/controllers/api/base_controller.rb
end
rescue_from StandardError, :with => lambda { |error|
Rails.logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
render_error 'standard_error', :status => 500, :locals => { :exception => error }
}
rescue_from Apipie::ParamError, :with => lambda { |error|
Rails.logger.info "#{error.message} (#{error.class})"
logger.info "#{error.message} (#{error.class})"
render_error 'param_error', :status => :bad_request, :locals => { :exception => error }
}
rescue_from ActiveRecord::RecordNotFound, :with => lambda { |error|
logger.info "#{error.message} (#{error.class})"
not_found(:message => "#{error.message}", :class => "#{error.class}")
}
def get_resource
instance_variable_get :"@#{resource_name}" or raise 'no resource loaded'
end
......
protected
def not_found
render_error 'not_found', :status => :not_found and return false
def not_found(options = nil)
not_found_message = {}
case options
when String
not_found_message.merge! :message => options
when Hash
not_found_message.merge! options
else
render_error 'not_found', :status => :not_found and return false
end
render :json => not_found_message, :status => :not_found and return false
end
def process_resource_error(options = { })
......
def not_found_if_nested_id_exists
allowed_nested_id.each do |obj_id|
if params[obj_id].present?
msg = "#{obj_id.humanize} not found by id '#{params[obj_id]}'"
render :json => {:message => msg}, :status => :not_found and return false
not_found "#{obj_id.humanize} not found by id '#{params[obj_id]}'"
end
end
end
app/controllers/api/v2/host_classes_controller.rb
else
@host ||= Host::Managed.find_by_name(params[:host_id])
return @host_id = @host.id if @host
render_error 'not_found', :status => :not_found and return false
not_found
end
end
end
end
end
end
app/controllers/api/v2/override_values_controller.rb
def return_if_override_mismatch
if (@override_values && @override_value && !@override_values.find_by_id(@override_value.id)) || (@override_values && !@override_value) || !@override_values
msg = "Override value not found by id '#{params[:id]}'"
render :json => {:message => msg}, :status => :not_found and return false
not_found "Override value not found by id '#{params[:id]}'"
end
end
......
end
end
end
end
app/controllers/api/v2/parameters_controller.rb
@parameter = @parameters.find_by_id(params[:id].to_i) if params[:id].to_i > 0
@parameter ||= @parameters.find_by_name(params[:id])
return @parameter if @parameter
render_error 'not_found', :status => :not_found and return false
not_found
end
end
app/controllers/api/v2/template_combinations_controller.rb
def find_parent_config_template
@config_template = ConfigTemplate.find(params[:config_template_id])
unless @config_template
render_error 'not_found', :status => :not_found and return false
end
not_found unless @config_template
@config_template
end
end
app/controllers/concerns/api/import_puppetclasses_common_controller.rb
@smart_proxy = SmartProxy.find_by_id(id.to_i) if id.to_i > 0
@smart_proxy ||= SmartProxy.find_by_name(id)
unless @smart_proxy && SmartProxy.puppet_proxies.pluck("smart_proxies.id").include?(@smart_proxy.id)
msg = 'We did not find a foreman proxy that can provide the information, ensure that this proxy has the puppet feature turned on.'
render :json => {:message => msg}, :status => :not_found and return false
not_found 'We did not find a foreman proxy that can provide the information, ensure that this proxy has the puppet feature turned on.'
end
@smart_proxy
end
app/controllers/concerns/api/v2/lookup_keys_common_controller.rb
else
params.keys.include?('smart_class_parameter_id') ? params['smart_variable_id'] : params['id']
end
msg = "#{obj} not found by id '#{id}'"
render :json => {:message => msg}, :status => :not_found and return false
not_found "#{obj} not found by id '#{id}'"
end
end
test/functional/api/v1/reports_controller_test.rb
test "should give error if no last report for given host" do
get :last, {:host_id => hosts(:two).to_param }
assert_response 500
assert_response :not_found
end
end
test/functional/api/v1/subnets_controller_test.rb
assert_response :success
end
test "does not create subnet with non-existent domain" do
post :create, { :subnet => valid_attrs.merge( :domain_ids => [1, 2] ) }
assert_response :not_found
end
test "should update subnet" do
put :update, { :id => subnets(:one).to_param, :subnet => { } }
assert_response :success
test/functional/api/v2/reports_controller_test.rb
test "should give error if no last report for given host" do
get :last, {:host_id => hosts(:two).to_param }
assert_response 500
assert_response :not_found
end
end
test/functional/api/v2/subnets_controller_test.rb
valid_attrs = { :name => 'QA2', :network => '10.35.2.27', :mask => '255.255.255.0' }
def test_index
test "index content is a JSON array" do
get :index
subnets = ActiveSupport::JSON.decode(@response.body)
assert subnets['results'].is_a?(Array)
assert_response :success
assert !subnets.empty?
end
test "should show individual record" do
......
assert_response :success
end
test "does not create subnet with non-existent domain" do
post :create, { :subnet => valid_attrs.merge( :domain_ids => [1, 2] ) }
assert_response :not_found
end
test "should update subnet" do
put :update, { :id => subnets(:one).to_param, :subnet => { } }
assert_response :success
......
assert_response :unprocessable_entity
end
def test_destroy_json
test "delete destroys subnet not in use" do
subnet = Subnet.first
subnet.hosts.clear
subnet.interfaces.clear

Also available in: Unified diff