From 5f77fbcdefbd28fa584020c9fda8d26ccedeefe4 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 6 Oct 2023 11:47:31 -0300 Subject: [PATCH] feat: validar colores #14433 --- app/models/metadata_color.rb | 24 +++++++- test/models/metadata_color_test.rb | 88 ++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/models/metadata_color_test.rb diff --git a/app/models/metadata_color.rb b/app/models/metadata_color.rb index e6c28155..a4785c4c 100644 --- a/app/models/metadata_color.rb +++ b/app/models/metadata_color.rb @@ -1,4 +1,26 @@ # frozen_string_literal: true +module MetadataColorConstants + COLOR_RE = /\A#[a-f0-9]{6}\z/.freeze +end + # Un campo de color -class MetadataColor < MetadataString; end +class MetadataColor < MetadataString + include MetadataColorConstants + + private + + # Solo permitir colores + # + # @param [String] + # @return [String] + def sanitize(color) + color = super(color) + + if COLOR_RE =~ color + color + else + '' + end + end +end diff --git a/test/models/metadata_color_test.rb b/test/models/metadata_color_test.rb new file mode 100644 index 00000000..e2447a50 --- /dev/null +++ b/test/models/metadata_color_test.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +require 'test_helper' + +class MetadataColorTest < ActiveSupport::TestCase + setup do + @site = build(:site) + @document = build(:document, site: @site.jekyll) + @name = SecureRandom.hex + @layout = build(:layout, site: @site) + @metadata = MetadataColor.new(site: @site, document: @document, name: @name, type: 'color', layout: @layout) + end + + def random_color + "##{SecureRandom.hex(3)}" + end + + test 'se guarda en el encabezado' do + assert @metadata.front_matter? + end + + test 'por defecto es una string vacía' do + assert_equal '', @metadata.default_value + end + + test 'puede traer el valor desde el documento' do + @document.data[@name] = random_color + + assert_equal @document[@name], @metadata.document_value + assert @metadata.save + assert_equal @document[@name], @metadata.value + end + + test 'al cambiar el valor podemos obtenerlo' do + @metadata.value = value_was = random_color + @metadata.value = random_color + + assert_equal value_was, @metadata.value_was + assert_not_equal value_was, @metadata.value + end + + test 'pueden tener un valor por defecto desde el layout' do + default = random_color + + @layout = build(:layout, site: @site, metadata: { @name => { 'default' => { I18n.locale.to_s => default } } }) + @metadata = MetadataColor.new(site: @site, document: @document, name: @name, type: 'color', layout: @layout) + + assert_equal default, @metadata.default_value + end + + test 'se pueden indexar' do + assert @metadata.indexable? + end + + test 'son públicos por defecto' do + assert_not @metadata.private? + end + + test 'se pueden hacer privados' do + @layout = build(:layout, site: @site, metadata: { @name => { 'private' => true } }) + @metadata = MetadataColor.new(site: @site, document: @document, name: @name, type: 'color', layout: @layout) + + assert @metadata.private? + end + + test 'se les puede asignar un valor' do + @metadata.value = value = random_color + + assert_equal value, @metadata.value + end + + test 'el valor asignado no puede tener espacios adelante ni atrás' do + @metadata.value = value = " #{random_color}\n\n" + + assert @metadata.save + assert_equal value.strip, @metadata.value + end + + test 'no pueden tener html' do + content = random_color + + @metadata.value = value = "#{content}" + + assert @metadata.save + assert_not_equal value, @metadata.value + assert_equal content, @metadata.value + end +end