Project

General

Profile

« Previous | Next » 

Revision 15d91324

Added by Joseph Magen over 10 years ago

fixes #3646 - API v2 change default root name for api json single object response to no root node but make it configurable

View differences:

config/initializers/rabl_init.rb
alias_method_chain :collection, :defaults
# extending this helper defined in module Rabl::Helpers allows users to
# overwrite the object root name in show rabl views. Three options:
# 1) class name of object (ex. domain) (default behavior of rabl)
# 2) no root - pass ?params[:object_name]=false in URL
# 3) custom - pass ?params[:object_name]=custom_name in URL
# overwrite the object root name in show rabl views. Two options:
# 1) no root - default
# 2) custom - pass ?params[:root_name]=custom_name in URL
def data_name(data_token)
return nil if params['object_name'].to_s == 'false'
return params['object_name'] if params['object_name'].present?
# custom object root
return params['root_name'] if params['root_name'].present? && !['false', false].include?(params['root_name'])
# no object root for v2
return nil if api_version.to_i > 1 || ['false', false].include?(params['root_name'])
# otherwise return super since v1 has object root (config.include_child_root = true)
super
end
test/functional/api/v2/config_templates_controller_test.rb
assert_response :success
template = ActiveSupport::JSON.decode(@response.body)
assert !template.empty?
assert_equal template["config_template"]["name"], config_templates(:pxekickstart).name
assert_equal template["name"], config_templates(:pxekickstart).name
end
test "should not create invalid" do
......
valid_attrs = { :template => "This is a test template", :template_kind_id => template_kinds(:gpxe).id, :name => "RandomName" }
post :create, { :config_template => valid_attrs }
template = ActiveSupport::JSON.decode(@response.body)
assert template["config_template"]["name"] == "RandomName"
assert template["name"] == "RandomName"
assert_response 200
end
test/functional/api/v2/hosts_controller_test.rb
facts = fact_json['facts']
post :facts, {:name => hostname, :facts => facts}, set_session_user
assert_response :unprocessable_entity
assert_equal 'A stub failure', JSON.parse(response.body)['host']['errors']['foo'].first
assert_equal 'A stub failure', JSON.parse(response.body)['errors']['foo'].first
end
context 'BMC proxy operations' do
test/functional/api/v2/locations_controller_test.rb
show_response = ActiveSupport::JSON.decode(@response.body)
assert !show_response.empty?
#assert *_ids are included in response. Test just for domain_ids
assert show_response["location"].any? {|k,v| k == "domain_ids" }
assert show_response.any? {|k,v| k == "domain_ids" }
end
test "should not create invalid location" do
......
assert response.kind_of?(Hash)
klass_name = obj.class.name.downcase
assert "location", klass_name
assert response[klass_name].kind_of?(Hash)
assert_equal obj.id, response[klass_name]["id"]
assert response.kind_of?(Hash)
assert_equal obj.id, response["id"]
end
test "object name on show can be specified" do
obj = taxonomies(:location1)
get :show, {:id => obj.id, :object_name => 'row'}
get :show, {:id => obj.id, :root_name => 'row'}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert response['row'].kind_of?(Hash)
......
test "no object name on show" do
obj = taxonomies(:location1)
get :show, {:id => obj.id, :object_name => 'false'}
get :show, {:id => obj.id, :root_name => 'false'}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert_equal obj.id, response["id"]
test/functional/api/v2/override_values_controller_test.rb
get :show, {:smart_variable_id => lookup_keys(:two).to_param, :id => lookup_values(:four).to_param }
assert_response :success
results = ActiveSupport::JSON.decode(@response.body)
assert !results['override_value'].empty?
assert_equal "hostgroup=Common", results['override_value']['match']
assert !results.empty?
assert_equal "hostgroup=Common", results['match']
end
test "should show specific override values for specific smart class parameter" do
get :show, {:smart_class_parameter_id => lookup_keys(:complex).to_param, :id => lookup_values(:hostgroupcommon).to_param }
results = ActiveSupport::JSON.decode(@response.body)
assert !results['override_value'].empty?
assert_equal "hostgroup=Common", results['override_value']['match']
assert !results.empty?
assert_equal "hostgroup=Common", results['match']
assert_response :success
end
test/functional/api/v2/parameters_controller_test.rb
get :show, {:operatingsystem_id => operatingsystems(:redhat).to_param,:id => param.name }
assert_response :success
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal param.id, show_response['parameter']['id']
assert_equal param.id, show_response['id']
end
test "should create host parameter" do
test/functional/api/v2/template_combinations_controller_test.rb
assert_response :success
template_combination = ActiveSupport::JSON.decode(@response.body)
assert !template_combination.empty?
assert_equal template_combination["template_combination"]["config_template_id"], template_combinations(:two).config_template_id
assert_equal template_combination["config_template_id"], template_combinations(:two).config_template_id
end
test "should create valid" do
......
:config_template_id => config_templates(:mystring2).id }
end
template_combination = ActiveSupport::JSON.decode(@response.body)
assert template_combination["template_combination"]["environment_id"] == environments(:production).id
assert template_combination["template_combination"]["hostgroup_id"] == hostgroups(:unusual).id
assert template_combination["template_combination"]["config_template_id"] == config_templates(:mystring2).id
assert template_combination["environment_id"] == environments(:production).id
assert template_combination["hostgroup_id"] == hostgroups(:unusual).id
assert template_combination["config_template_id"] == config_templates(:mystring2).id
assert_response 200
end

Also available in: Unified diff