validar que estemos subiendo imágenes

This commit is contained in:
f 2019-08-23 15:24:41 -03:00
parent b4aebb9fe2
commit 3ff64f365c
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
5 changed files with 32 additions and 0 deletions

View file

@ -72,6 +72,7 @@ RUN apk add --no-cache yarn
RUN apk add --no-cache libgit2
# Instalar foreman para poder correr los servicios
RUN gem install --no-document --no-user-install foreman
RUN apk add --no-cache file
# Agregar el grupo del servidor web
RUN addgroup -g 82 -S www-data

View file

@ -16,6 +16,7 @@ class MetadataImage < MetadataTemplate
super
errors << I18n.t('metadata.image.path_required') if path_missing?
errors << I18n.t('metadata.image.not_an_image') unless image?
errors.compact!
errors.empty?
@ -31,6 +32,19 @@ class MetadataImage < MetadataTemplate
value['path'].is_a?(String)
end
# Determina si es una imagen antes de subirla
def image?
if value['path'].is_a? ActionDispatch::Http::UploadedFile
`file --mime-type "#{value['path'].tempfile.path}"`
.split(' ')
.last
.chomp
.starts_with? 'image/'
else
true
end
end
# Determina si la ruta es opcional pero deja pasar si la ruta se
# especifica
def path_optional?

View file

@ -117,4 +117,20 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_equal 'hola', @post.image.value['description']
end
test 'no se pueden subir archivos cualquiera' do
patch site_post_url(@site, @post.id),
headers: @authorization,
params: {
post: {
image: {
path: fixture_file_upload('files/_logo.png', 'image/png'),
description: 'hola'
}
}
}
assert_equal 200, response.status
assert_match I18n.t('metadata.image.not_an_image'), response.body
end
end

1
test/fixtures/files/_logo.png vendored Normal file
View file

@ -0,0 +1 @@
pwned

BIN
test/fixtures/files/logo.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB