sutty/test/controllers/api/v1/posts_controller_test.rb

149 lines
4 KiB
Ruby
Raw Normal View History

2020-02-18 16:45:08 +00:00
# frozen_string_literal: true
require 'test_helper'
module Api
module V1
class PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@rol = create :rol
@site = @rol.site
@usuarie = @rol.usuarie
@site.update_attribute :colaboracion_anonima, true
end
teardown do
@site.destroy
end
test 'no se pueden enviar sin cookie' do
2020-09-29 21:22:28 +00:00
post v1_site_posts_url(@site.hostname, layout: :post), params: {
2020-02-18 16:45:08 +00:00
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
posts = @site.posts.size
@site = Site.find(@site.id)
assert_equal posts, @site.posts.size
2020-09-29 21:22:28 +00:00
assert_response :precondition_required
assert_equal 'expired_or_invalid_cookie', response.body
2020-02-18 16:45:08 +00:00
end
test 'no se pueden enviar a sitios que no existen' do
site = SecureRandom.hex
get v1_site_invitades_cookie_url(site_id: site)
2020-09-29 21:22:28 +00:00
assert_not cookies[site]
get v1_site_invitades_cookie_url(@site.hostname)
assert cookies[@site.name]
post v1_site_posts_url(site_id: site, layout: :post),
2020-02-18 16:45:08 +00:00
headers: { cookies: cookies },
params: {
2020-09-29 21:22:28 +00:00
consent: true,
title: SecureRandom.hex,
description: SecureRandom.hex
2020-02-18 16:45:08 +00:00
}
2020-09-29 21:22:28 +00:00
assert_response :precondition_required
# XXX: Como la cookie es lo primero que se verifica, si el sitio
# no existe tampoco se va a encontrar la cookie correcta.
assert_equal 'expired_or_invalid_cookie', response.body
2020-02-18 16:45:08 +00:00
end
test 'antes hay que pedir una cookie' do
assert_equal 2, @site.posts.size
2020-09-29 21:22:28 +00:00
get v1_site_invitades_cookie_url(@site.hostname)
2020-02-18 16:45:08 +00:00
2020-09-29 21:22:28 +00:00
post v1_site_posts_url(@site.hostname, layout: :post),
2020-02-18 16:45:08 +00:00
headers: {
cookies: cookies,
2020-09-29 21:22:28 +00:00
origin: @site.url
2020-02-18 16:45:08 +00:00
},
params: {
2020-09-29 21:22:28 +00:00
consent: true,
title: SecureRandom.hex,
description: SecureRandom.hex
2020-02-18 16:45:08 +00:00
}
2020-09-29 21:22:28 +00:00
@site.reload
2020-02-18 16:45:08 +00:00
assert_equal 3, @site.posts.size
assert_response :redirect
end
test 'no se pueden enviar algunos valores' do
uuid = SecureRandom.uuid
date = Date.today + 2.days
slug = SecureRandom.hex
2020-09-29 21:22:28 +00:00
desc = SecureRandom.hex
2020-02-18 16:45:08 +00:00
title = SecureRandom.hex
order = (rand * 100).to_i
2020-09-29 21:22:28 +00:00
get v1_site_invitades_cookie_url(@site.hostname)
2020-02-18 16:45:08 +00:00
2020-09-29 21:22:28 +00:00
post v1_site_posts_url(@site.hostname, layout: :post),
2020-02-18 16:45:08 +00:00
headers: {
cookies: cookies,
2020-09-29 21:22:28 +00:00
origin: @site.url
2020-02-18 16:45:08 +00:00
},
params: {
2020-09-29 21:22:28 +00:00
consent: true,
title: title,
description: desc,
uuid: uuid,
date: date,
slug: slug,
order: order
2020-02-18 16:45:08 +00:00
}
2020-09-29 21:22:28 +00:00
@site.reload
2020-02-18 16:45:08 +00:00
p = @site.posts.find_by title: title
2020-09-29 21:22:28 +00:00
assert p
assert_equal desc, p.description.value
2020-02-18 16:45:08 +00:00
assert_not_equal uuid, p.uuid.value
assert_not_equal slug, p.slug.value
assert_not_equal order, p.order.value
assert_not_equal date, p.date.value
end
test 'las cookies tienen un vencimiento interno' do
assert_equal 2, @site.posts.size
2020-09-29 21:22:28 +00:00
get v1_site_invitades_cookie_url(@site.hostname)
2020-02-18 16:45:08 +00:00
2020-09-29 21:22:28 +00:00
expired = (ENV.fetch('COOKIE_DURATION', '30').to_i + 1).minutes
Timecop.freeze(Time.now + expired) do
post v1_site_posts_url(@site.hostname, layout: :post),
2020-02-18 16:45:08 +00:00
headers: {
cookies: cookies,
2020-09-29 21:22:28 +00:00
origin: @site.url
2020-02-18 16:45:08 +00:00
},
params: {
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
end
2020-09-29 21:22:28 +00:00
@site.reload
assert_response :precondition_required
2020-02-18 16:45:08 +00:00
assert_equal 2, @site.posts.size
2020-09-29 21:22:28 +00:00
assert_equal 'expired_or_invalid_cookie', response.body
2020-02-18 16:45:08 +00:00
end
end
end
end