diff --git a/app/models/metadata_email.rb b/app/models/metadata_email.rb index bbe95561..63b70888 100644 --- a/app/models/metadata_email.rb +++ b/app/models/metadata_email.rb @@ -1,5 +1,22 @@ # frozen_string_literal: true # Un campo de correo -# TODO: Validar que tenga un formato correo -class MetadataEmail < MetadataString; end +class MetadataEmail < MetadataString + # Aplica distintas validaciones a la cuenta de correo + # + # @return [Boolean] + def validate + super + + errors << email_address.error.capitalize unless email_address.valid? + + errors.empty? + end + + private + + # @return [EmailAddress::Address] + def email_address + @email_address ||= EmailAddress::Address.new(value, {}, I18n.locale.to_s) + end +end diff --git a/test/models/metadata_email_test.rb b/test/models/metadata_email_test.rb new file mode 100644 index 00000000..eeab6872 --- /dev/null +++ b/test/models/metadata_email_test.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require 'test_helper' + +class MetadataEmailTest < ActiveSupport::TestCase + setup do + @site = build(:site) + @document = build(:document, site: @site.jekyll) + @name = SecureRandom.hex + @layout = build(:layout, site: @site) + @metadata = MetadataEmail.new(site: @site, document: @document, name: @name, type: 'email', layout: @layout) + end + + def random_value + "#{SecureRandom.hex}@sutty.nl" + 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_value + + 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_value + @metadata.value = random_value + + 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_value + + @layout = build(:layout, site: @site, metadata: { @name => { 'default' => { I18n.locale.to_s => default } } }) + @metadata = MetadataEmail.new(site: @site, document: @document, name: @name, type: 'email', layout: @layout) + + assert_equal default, @metadata.default_value + end + + test 'no puede estar vacía si es obligatoria' do + @layout = build(:layout, site: @site, metadata: { @name => { 'default' => { 'required' => true } } }) + @metadata = MetadataEmail.new(site: @site, document: @document, name: @name, type: 'email', layout: @layout, + required: true) + + assert @metadata.required + assert @metadata.empty? + assert_not @metadata.valid? + 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 = MetadataEmail.new(site: @site, document: @document, name: @name, type: 'email', layout: @layout) + + assert @metadata.private? + end + + test 'se les puede asignar un valor' do + @metadata.value = value = random_value + + assert_equal value, @metadata.value + end + + test 'el valor asignado no puede tener espacios adelante ni atrás' do + @metadata.value = value = " #{random_value}\n\n" + + assert @metadata.save + assert_equal value.strip, @metadata.value + end + + test 'no pueden tener html' do + content = random_value + + @metadata.value = value = "#{content}" + + assert @metadata.save + assert_not_equal value, @metadata.value + assert_equal content, @metadata.value + end + + test 'no puede ser un correo inexistente' do + @metadata.value = "#{SecureRandom.hex}@gmilll.com" + + assert_not @metadata.valid? + end +end