5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 01:16:21 +00:00
panel/test/controllers/posts_controller_test.rb

169 lines
4.9 KiB
Ruby
Raw Normal View History

2019-08-13 23:33:57 +00:00
# frozen_string_literal: true
require 'test_helper'
class PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@rol = create :rol
@site = @rol.site
@usuarie = @rol.usuarie
@post = @site.posts.build(title: SecureRandom.hex,
description: SecureRandom.hex)
2019-08-13 23:33:57 +00:00
@post.save
@authorization = {
Authorization: ActionController::HttpAuthentication::Basic
.encode_credentials(@usuarie.email, @usuarie.password)
}
end
teardown do
@site.destroy
end
test 'se pueden ver todos' do
2019-08-13 23:33:57 +00:00
get site_posts_url(@site), headers: @authorization
assert_match @site.name, response.body
assert_match @post.title.value, response.body
end
test 'se pueden crear nuevos' do
title = SecureRandom.hex
post site_posts_url(@site), headers: @authorization,
params: {
post: {
title: title,
description: title,
2019-08-13 23:33:57 +00:00
date: 2.days.ago.strftime('%F')
}
}
# TODO: implementar reload?
site = Site.find(@site.id)
new_post = site.posts.find_by(title: title)
2019-08-13 23:33:57 +00:00
assert_equal 302, response.status
# XXX: No usamos follow_redirect! porque pierde la autenticación
get site_posts_url(@site), headers: @authorization
assert_match new_post.title.value, response.body
assert_equal title, new_post.title.value
assert_equal I18n.t('post_service.created', title: new_post.title.value),
@site.repository.rugged.head.target.message
end
test 'se pueden ver' do
2020-09-29 21:22:28 +00:00
get site_post_url(@site, @post.lang.value, @post.id), headers: @authorization
assert_equal 200, response.status
assert_match @post.title.value, response.body
end
2019-08-16 23:12:22 +00:00
test 'se pueden actualizar' do
title = SecureRandom.hex
2020-09-29 21:22:28 +00:00
patch site_post_url(@site, @post.lang.value, @post.id),
headers: @authorization,
params: {
post: {
title: title,
description: title
}
}
2019-08-16 23:12:22 +00:00
assert_equal 302, response.status
get site_posts_url(@site), headers: @authorization
assert_match title, response.body
site = Site.find @site.id
assert site.posts.map { |p| p.title.value }.include?(title)
end
test 'se pueden eliminar' do
params = ActionController::Parameters.new(post: { author: ['hola'] })
assert PostService.new(site: @site,
usuarie: @usuarie,
post: @post,
params: params).update
2020-09-29 21:22:28 +00:00
delete site_post_url(@site, @post.lang.value, @post.id), headers: @authorization
2019-08-16 23:12:22 +00:00
get site_posts_url(@site), headers: @authorization
site = Site.find @site.id
assert_not site.posts.include?(@post)
assert @post.destroyed?
assert_equal I18n.t('post_service.destroyed', title: @post.title.value),
@site.repository.rugged.head.target.message
end
2019-08-22 19:13:21 +00:00
test 'se pueden subir imágenes' do
2020-09-29 21:22:28 +00:00
patch site_post_url(@site, @post.lang.value, @post.id),
2019-08-22 19:13:21 +00:00
headers: @authorization,
params: {
post: {
image: {
2021-01-25 19:47:19 +00:00
path: fixture_file_upload('logo.png', 'image/png'),
2019-08-22 19:13:21 +00:00
description: 'hola'
}
}
}
assert_equal 302, response.status
@site = Site.find(@site.id)
# TODO: Implementar reload
@post = @site.posts.find(@post.id)
Dir.chdir(@site.path) do
assert File.exist?(@post.image.value['path'])
end
assert_equal 'hola', @post.image.value['description']
end
2019-08-23 18:24:41 +00:00
test 'no se pueden subir archivos cualquiera' do
2020-09-29 21:22:28 +00:00
patch site_post_url(@site, @post.lang.value, @post.id),
2019-08-23 18:24:41 +00:00
headers: @authorization,
params: {
post: {
image: {
2021-01-25 19:47:19 +00:00
path: fixture_file_upload('_logo.png', 'image/png'),
2019-08-23 18:24:41 +00:00
description: 'hola'
}
}
}
assert_equal 200, response.status
assert_match I18n.t('metadata.image.not_an_image'), response.body
end
2019-10-18 20:35:09 +00:00
test 'se pueden reordenar' do
2021-02-17 21:48:01 +00:00
lang = { lang: @site.locales.sample }
(rand * 10).round.times do
@site.posts(**lang).create title: SecureRandom.hex, description: SecureRandom.hex
end
posts = @site.posts(**lang)
2020-01-02 23:29:04 +00:00
reorder = Hash[posts.map { |p| p.uuid.value }.shuffle.each_with_index.to_a]
2019-10-18 20:35:09 +00:00
post site_posts_reorder_url(@site),
headers: @authorization,
2021-02-17 21:48:01 +00:00
params: { post: { lang: lang[:lang], reorder: reorder } }
2019-10-18 20:35:09 +00:00
@site = Site.find @site.id
assert_equal reorder,
2021-02-17 21:48:01 +00:00
Hash[@site.posts(**lang).map do |p|
2020-01-02 23:29:04 +00:00
[p.uuid.value, p.order.value]
2019-11-06 22:35:48 +00:00
end]
2021-02-17 21:48:01 +00:00
assert_equal I18n.t('post_service.reorder'),
@site.repository.rugged.head.target.message
2019-10-18 20:35:09 +00:00
end
2019-08-13 23:33:57 +00:00
end