Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #27897 - Parameter type is not required #7054

Merged
merged 3 commits into from Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v2/hostgroups_controller.rb
Expand Up @@ -56,7 +56,7 @@ def show
param :group_parameters_attributes, Array, :required => false, :desc => N_("Array of parameters") do
param :name, String, :desc => N_("Name of the parameter"), :required => true
param :value, String, :desc => N_("Parameter value"), :required => true
param :parameter_type, Parameter::KEY_TYPES, :desc => N_("Type of value"), :required => true
param :parameter_type, Parameter::KEY_TYPES, :desc => N_("Type of value")
param :hidden_value, :bool
end
Hostgroup.registered_smart_proxies.each do |name, options|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v2/hosts_controller.rb
Expand Up @@ -100,7 +100,7 @@ def show
param :host_parameters_attributes, Array, :desc => N_("Host's parameters (array or indexed hash)") do
param :name, String, :desc => N_("Name of the parameter"), :required => true
param :value, String, :desc => N_("Parameter value"), :required => true
param :parameter_type, Parameter::KEY_TYPES, :desc => N_("Type of value"), :required => true
param :parameter_type, Parameter::KEY_TYPES, :desc => N_("Type of value")
param :hidden_value, :bool
end
param :build, :bool
Expand Down
6 changes: 5 additions & 1 deletion app/models/parameter.rb
Expand Up @@ -39,7 +39,7 @@ class Parameter < ApplicationRecord
default_scope -> { order("parameters.name") }

before_create :set_priority
before_save :set_searchable_value
before_save :set_searchable_value, :set_default_key_type

PRIORITY = { :common_parameter => 0,
:organization_parameter => 10,
Expand Down Expand Up @@ -77,6 +77,10 @@ def hash_for_include_source(source, source_name = nil)

private

def set_default_key_type
self.key_type ||= Parameter::KEY_TYPES.first
end

def set_searchable_value
self.searchable_value = Parameter.format_value_before_type_cast(value, key_type)
end
Expand Down
15 changes: 15 additions & 0 deletions test/controllers/api/v2/hostgroups_controller_test.rb
Expand Up @@ -236,6 +236,21 @@ def valid_attrs_with_root(extra_attrs = {})
assert_equal 42, show_response['parameters'].first['value'].to_i
assert_equal 'integer', show_response['parameters'].first['parameter_type']
end

test "should create a group parameter with default parameter type" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This this test also assert that it was created with the string type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test just asserts that creating doesn't fail if no parameter_type was provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following test does check if there is no parameter type was provided then it defaults to "string"

assert_equal 'string', show_response['parameters'].first['parameter_type']

hostgroup_params = [{ :name => "foo", :value => 42 }]
post :create, params: { hostgroup: valid_attrs.merge(parameters: hostgroup_params) }
assert_response :success
end

test "should show a group parameter with default parameter type" do
hostgroup = FactoryBot.create(:hostgroup)
hostgroup.group_parameters.create!(:name => "foo", :value => 42)
get :show, params: { :id => hostgroup.id }
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal 42, show_response['parameters'].first['value']
assert_equal 'string', show_response['parameters'].first['parameter_type']
end
end

context 'hidden parameters' do
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/api/v2/hosts_controller_test.rb
Expand Up @@ -994,6 +994,22 @@ def setup
assert_equal 42, show_response['parameters'].first['value'].to_i
assert_equal 'integer', show_response['parameters'].first['parameter_type']
end

test "should create an host parameter with default parameter type" do
host = FactoryBot.build(:host)
host_params = [{ :name => "foo", :value => 42 }]
post :create, params: { host: host.attributes.merge(parameters: host_params) }
assert_response :success
end

test "should show an host parameter with default parameter type" do
host = FactoryBot.create(:host)
host.host_parameters.create!(:name => "foo", :value => 42)
get :show, params: { :id => host.id }
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal 42, show_response['parameters'].first['value'].to_i
assert_equal 'string', show_response['parameters'].first['parameter_type']
end
end

context 'hidden parameters' do
Expand Down