mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-24 14:26:22 +00:00
181 lines
5.1 KiB
Ruby
181 lines
5.1 KiB
Ruby
|
# 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
|