From 24b507623f36e908c142ebe4fefc641c4d57d3bd Mon Sep 17 00:00:00 2001 From: f Date: Fri, 6 Oct 2023 16:59:59 -0300 Subject: [PATCH] feat: validar que los valores ingresados sean los predefinidos #14433 --- app/models/metadata_predefined_array.rb | 14 +++++++++++++ test/models/metadata_predefined_array_test.rb | 20 ++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/models/metadata_predefined_array.rb b/app/models/metadata_predefined_array.rb index 6ac30636..dfcb2037 100644 --- a/app/models/metadata_predefined_array.rb +++ b/app/models/metadata_predefined_array.rb @@ -13,4 +13,18 @@ class MetadataPredefinedArray < MetadataArray [v[I18n.locale.to_s], k] end end + + private + + # Solo permite guardar los valores predefinidos + # + # @param :value [Array] + # @return [Array] + def sanitize(value) + value = super(value) + + value.select do |v| + values.values.include? v + end + end end diff --git a/test/models/metadata_predefined_array_test.rb b/test/models/metadata_predefined_array_test.rb index 8dd99e0b..75b75609 100644 --- a/test/models/metadata_predefined_array_test.rb +++ b/test/models/metadata_predefined_array_test.rb @@ -18,9 +18,9 @@ class MetadataPredefinedArrayTest < ActiveSupport::TestCase end def random_values - random_value.to_h do |k| + (1..100).to_a.sample.times.to_h do [ - k, + SecureRandom.hex, I18n.available_locales.map(&:to_s).to_h do |l| [l, SecureRandom.hex] end @@ -29,9 +29,7 @@ class MetadataPredefinedArrayTest < ActiveSupport::TestCase end def random_value - (1..100).to_a.sample.times.map do - SecureRandom.hex - end + @metadata.values.values.sample((2..(@metadata.values.values.size)).to_a.sample) end test 'se guarda en el encabezado' do @@ -104,14 +102,14 @@ class MetadataPredefinedArrayTest < ActiveSupport::TestCase end test 'el valor asignado no puede tener espacios adelante ni atrĂ¡s' do - @metadata.value = value = [" #{SecureRandom.hex}\n\n"] + @metadata.value = value = [" #{random_value.sample}\n\n"] assert @metadata.save assert_equal value.map(&:strip), @metadata.value end test 'no pueden tener html' do - content = SecureRandom.hex + content = random_value.sample @metadata.value = value = ["#{content}"] @@ -132,4 +130,12 @@ class MetadataPredefinedArrayTest < ActiveSupport::TestCase assert @metadata.value.empty? assert @metadata.empty? end + + test 'solo se permiten los valores predefinidos' do + valid_values = random_value + @metadata.value = valid_values + ((1..100).to_a.sample.times.map { SecureRandom.hex }) + + assert @metadata.save + assert_equal valid_values, @metadata.value + end end