diff --git a/test/models/metadata_file_test.rb b/test/models/metadata_file_test.rb index 8d8b6806..09c2e74c 100644 --- a/test/models/metadata_file_test.rb +++ b/test/models/metadata_file_test.rb @@ -11,6 +11,10 @@ class MetadataFileTest < ActiveSupport::TestCase @metadata = MetadataFile.new(site: @site, document: @document, name: @name, type: 'file', layout: @layout) end + teardown do + @site&.destroy + end + def random_value { 'path' => "public/#{SecureRandom.base36(28)}/#{SecureRandom.hex}.pdf", @@ -103,8 +107,6 @@ class MetadataFileTest < ActiveSupport::TestCase assert_instance_of String, @metadata.value['path'] assert_instance_of ActiveStorage::Attachment, @metadata.static_file assert_not @metadata.empty? - ensure - @site.destroy end test 'se puede volver a encontrar un archivo subido' do @@ -124,8 +126,6 @@ class MetadataFileTest < ActiveSupport::TestCase assert_instance_of String, @metadata.value['path'] assert_instance_of ActiveStorage::Attachment, @metadata.static_file assert_not @metadata.empty? - ensure - @site.destroy end test 'si el archivo esta en el repositorio lo podemos asociar al sitio' do @@ -144,7 +144,24 @@ class MetadataFileTest < ActiveSupport::TestCase @metadata.save assert_instance_of ActiveStorage::Attachment, @metadata.static_file - ensure - @site.destroy + end + + test 'no se pueden subir archivos si el sitio no existe' do + assert_not @metadata.valid? + assert @metadata.errors.include?(I18n.t('metadata.file.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.file.path_required')) end end