From 48b703cf06a3e02096998a4a80c8180738c32e45 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 9 Oct 2023 14:25:32 -0300 Subject: [PATCH] test: testear subida de imagenes #14433 --- app/models/metadata_image.rb | 6 +- test/models/metadata_image_test.rb | 180 +++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 test/models/metadata_image_test.rb diff --git a/app/models/metadata_image.rb b/app/models/metadata_image.rb index fc61a4d7..14e939b0 100644 --- a/app/models/metadata_image.rb +++ b/app/models/metadata_image.rb @@ -11,7 +11,11 @@ class MetadataImage < MetadataFile errors.empty? end - # Determina si es una imagen + private + + # Determina si es una imagen para web + # + # @return [Boolean] def image? static_file&.blob&.send(:web_image?) end diff --git a/test/models/metadata_image_test.rb b/test/models/metadata_image_test.rb new file mode 100644 index 00000000..6fa146d8 --- /dev/null +++ b/test/models/metadata_image_test.rb @@ -0,0 +1,180 @@ +# frozen_string_literal: true + +require 'test_helper' + +class MetadataImageTest < ActiveSupport::TestCase + setup do + @site = build(:site) + @document = build(:document, site: @site.jekyll) + @name = SecureRandom.hex + @layout = build(:layout, site: @site) + @metadata = MetadataImage.new(site: @site, document: @document, name: @name, type: 'image', layout: @layout) + end + + teardown do + # @site&.destroy + end + + def random_value + { + 'path' => "public/#{SecureRandom.base36(28)}/#{SecureRandom.hex}.png", + 'description' => SecureRandom.hex + } + end + + test 'se guarda en el encabezado' do + assert @metadata.front_matter? + end + + test 'por defecto es una ruta vacía' do + assert_equal ({ 'path' => nil, 'description' => '' }), @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 = MetadataImage.new(site: @site, document: @document, name: @name, type: 'image', 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 = MetadataImage.new(site: @site, document: @document, name: @name, type: 'image', layout: @layout, + required: true) + + assert @metadata.required + assert @metadata.empty? + assert_not @metadata.valid? + end + + test 'no se pueden indexar' do + assert_not @metadata.indexable? + end + + test 'son públicos por defecto' do + assert_not @metadata.private? + end + + test 'no se pueden hacer privados' do + @layout = build(:layout, site: @site, metadata: { @name => { 'private' => true } }) + @metadata = MetadataImage.new(site: @site, document: @document, name: @name, type: 'image', layout: @layout) + + assert_not @metadata.private? + end + + test 'se les puede asignar un valor' do + @metadata.value = value = random_value + + assert_equal value, @metadata.value + end + + test 'se lo considera vacío si no se asigna un archivo' do + assert @metadata.empty? + end + + test 'se puede subir un archivo' do + value = random_value + value['path'] = + ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new, filename: value['path'].split('/').last) + + # XXX: por alguna razón no se guarda esto en cascada + @site.design.save + @site.design_id = @site.design.id + @site.save + + @metadata.value = value + + assert @metadata.valid? + assert @metadata.save + assert_instance_of String, @metadata.value['path'] + assert_instance_of ActiveStorage::Attachment, @metadata.static_file + assert_not @metadata.empty? + end + + test 'se puede volver a encontrar un archivo subido' do + value = random_value + value['path'] = + ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new, filename: value['path'].split('/').last) + + @site.design.save + @site.design_id = @site.design.id + @site.save + + @metadata.value = value + @metadata.save + + @metadata.instance_variable_set :@static_file, nil + + assert_instance_of String, @metadata.value['path'] + assert_instance_of ActiveStorage::Attachment, @metadata.static_file + assert_not @metadata.empty? + end + + test 'si el archivo esta en el repositorio lo podemos asociar al sitio' do + value = random_value + + @site.design.save + @site.design_id = @site.design.id + @site.save + + path = File.join(@site.path, value['path']) + FileUtils.mkdir_p File.dirname(path) + + File.write(path, 'test') + + @metadata.value = value + @metadata.save + + assert_instance_of ActiveStorage::Attachment, @metadata.static_file + end + + test 'no se pueden subir archivos si el sitio no existe' do + assert_not @metadata.valid? + assert @metadata.errors.include?(I18n.t('metadata.image.site_invalid')) + end + + test 'si es obligatorio hay que asignarle un archivo' do + @site.design.save + @site.design_id = @site.design.id + @site.save + + @metadata.required = true + @metadata.value = random_value + @metadata.value.delete 'path' + + assert @metadata.empty? + assert_not @metadata.valid? + assert @metadata.errors.include?(I18n.t('metadata.image.path_required')) + end + + test 'solo se pueden subir imagenes' do + @site.design.save + @site.design_id = @site.design.id + @site.save + + @metadata.required = true + @metadata.value = random_value + @metadata.value['path'].sub! '.pdf', '.png' + + assert_not @metadata.valid? + assert @metadata.errors.include?(I18n.t('metadata.image.not_an_image')) + end +end