mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-24 17:36:21 +00:00
151 lines
4.2 KiB
Ruby
151 lines
4.2 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'test_helper'
|
||
|
|
||
|
class MetadataFileTest < ActiveSupport::TestCase
|
||
|
setup do
|
||
|
@site = build(:site)
|
||
|
@document = build(:document, site: @site.jekyll)
|
||
|
@name = SecureRandom.hex
|
||
|
@layout = build(:layout, site: @site)
|
||
|
@metadata = MetadataFile.new(site: @site, document: @document, name: @name, type: 'file', layout: @layout)
|
||
|
end
|
||
|
|
||
|
def random_value
|
||
|
{
|
||
|
'path' => "public/#{SecureRandom.base36(28)}/#{SecureRandom.hex}.pdf",
|
||
|
'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 = MetadataFile.new(site: @site, document: @document, name: @name, type: 'file', 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 = MetadataFile.new(site: @site, document: @document, name: @name, type: 'file', 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 = MetadataFile.new(site: @site, document: @document, name: @name, type: 'file', 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?
|
||
|
ensure
|
||
|
@site.destroy
|
||
|
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?
|
||
|
ensure
|
||
|
@site.destroy
|
||
|
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
|
||
|
ensure
|
||
|
@site.destroy
|
||
|
end
|
||
|
end
|