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

feat: validar colores #14433
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
f 2023-10-06 11:47:31 -03:00
parent 76fd32bd92
commit 5f77fbcdef
No known key found for this signature in database
2 changed files with 111 additions and 1 deletions

View file

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

View file

@ -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 = "<html>#{content}</html>"
assert @metadata.save
assert_not_equal value, @metadata.value
assert_equal content, @metadata.value
end
end