5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 02:56:22 +00:00
panel/test/controllers/api/v1/posts_controller_test.rb

137 lines
3.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
post v1_site_posts_url(@site), params: {
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
posts = @site.posts.size
@site = Site.find(@site.id)
assert_equal posts, @site.posts.size
assert_response :no_content
end
test 'no se pueden enviar a sitios que no existen' do
site = SecureRandom.hex
get v1_site_invitades_cookie_url(site_id: site)
post v1_site_posts_url(site_id: site),
headers: { cookies: cookies },
params: {
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
assert_response :no_content
end
test 'antes hay que pedir una cookie' do
assert_equal 2, @site.posts.size
get v1_site_invitades_cookie_url(@site)
post v1_site_posts_url(@site),
headers: {
cookies: cookies,
origin: "https://#{@site.name}"
},
params: {
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
# XXX: No tenemos reload
@site = Site.find @site.id
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
title = SecureRandom.hex
order = (rand * 100).to_i
get v1_site_invitades_cookie_url(@site)
post v1_site_posts_url(@site),
headers: {
cookies: cookies,
origin: "https://#{@site.name}"
},
params: {
post: {
title: title,
description: SecureRandom.hex,
uuid: uuid,
date: date,
slug: slug,
order: order
}
}
# XXX: No tenemos reload
@site = Site.find @site.id
p = @site.posts.find_by title: title
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
get v1_site_invitades_cookie_url(@site)
Timecop.freeze(Time.now + 31.minutes) do
post v1_site_posts_url(@site),
headers: {
cookies: cookies,
origin: "https://#{@site.name}"
},
params: {
post: {
title: SecureRandom.hex,
description: SecureRandom.hex
}
}
end
@site = Site.find @site.id
assert_response :no_content
assert_equal 2, @site.posts.size
end
end
end
end