mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-24 07:56:21 +00:00
test: testear subida de imagenes #14433
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
3649dc6e4e
commit
48b703cf06
2 changed files with 185 additions and 1 deletions
|
@ -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
|
||||
|
|
180
test/models/metadata_image_test.rb
Normal file
180
test/models/metadata_image_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue