5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-24 09:36:21 +00:00

feat: validar que los valores ingresados sean los predefinidos #14433

This commit is contained in:
f 2023-10-06 16:59:59 -03:00
parent 49810beb1a
commit 24b507623f
No known key found for this signature in database
2 changed files with 27 additions and 7 deletions

View file

@ -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

View file

@ -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 = ["<html>#{content}</html>"]
@ -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