diff --git a/app/controllers/api/automate_workspaces_controller.rb b/app/controllers/api/automate_workspaces_controller.rb index 482ef78faa..a8cb9ac0b6 100644 --- a/app/controllers/api/automate_workspaces_controller.rb +++ b/app/controllers/api/automate_workspaces_controller.rb @@ -24,7 +24,7 @@ def decrypt(obj, data) 'value' => obj.decrypt(data['object'], data['attribute'])} end - def normalize_attr(attr, value) + def normalize_attr(attr, value, type = nil) return "password::********" if value.kind_of?(String) && value.start_with?("password::") super end diff --git a/app/controllers/api/base_controller/normalizer.rb b/app/controllers/api/base_controller/normalizer.rb index c4ffcc45ef..5913ab548a 100644 --- a/app/controllers/api/base_controller/normalizer.rb +++ b/app/controllers/api/base_controller/normalizer.rb @@ -25,17 +25,17 @@ def normalize_hash(type, obj, opts = {}) attrs.each do |k| next if Api.encrypted_attribute?(k) next if is_ar ? !obj.respond_to?(k) : !obj.key?(k) - result[k] = normalize_attr(k, is_ar ? obj.try(k) : obj[k]) + result[k] = normalize_attr(k, is_ar ? obj.try(k) : obj[k], type) end result end private - def normalize_attr(attr, value) + def normalize_attr(attr, value, type = nil) return if value.nil? if value.kind_of?(Array) || value.kind_of?(ActiveRecord::Relation) - normalize_array(value) + normalize_array(value, type) elsif value.respond_to?(:attributes) || value.respond_to?(:keys) normalize_hash(attr, value) elsif attr == "id" || attr.to_s.ends_with?("_id") @@ -128,7 +128,7 @@ def normalize_select_attributes(obj, opts) def normalize_array(obj, type = nil) type ||= @req.subject - obj.collect { |item| normalize_attr(get_reftype(type, type, item), item) } + obj.collect { |item| normalize_attr(get_reftype(type, type, item), item, type) } end def create_resource_attributes_hash(attributes, resource) diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index 6fa8a3c00e..2f32809f3d 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -12,7 +12,7 @@ def create_resource(_type, _id, data = {}) def validate_vms_resource(type, id, data = {}) transformation_mapping = resource_search(id, type, collection_class(type)) - transformation_mapping.validate_vms(data["import"]) || {} + normalize_hash(:vms, transformation_mapping.validate_vms(data["import"]) || {}) rescue StandardError => err raise BadRequestError, "Could not validate vms - #{err}" end diff --git a/spec/requests/transformation_mappings_spec.rb b/spec/requests/transformation_mappings_spec.rb index 2200837aa9..83f7e06258 100644 --- a/spec/requests/transformation_mappings_spec.rb +++ b/spec/requests/transformation_mappings_spec.rb @@ -154,14 +154,16 @@ "action" => "validate_vms", "import" => [ {"name" => vm.name, "uid" => vm.uid_ems}, - {"name" => "bad name", "uid" => "bad uid"} + {"name" => "bad_name", "uid" => "99999"} ] } post(api_transformation_mapping_url(nil, transformation_mapping), :params => request) expected = { - "valid_vms" => [a_hash_including("name" => "foo")], - "invalid_vms" => [a_hash_including("name" => "bad name")], + "valid_vms" => [a_hash_including("href" => a_string_including(api_vm_url(nil, vm)), + "name" => vm.name)], + "invalid_vms" => [a_hash_including("href" => a_string_including(api_vm_url(nil, "99999")), + "name" => "bad_name")], "conflict_vms" => [] } expect(response).to have_http_status(:ok)