137 lines
3.4 KiB
Ruby
137 lines
3.4 KiB
Ruby
|
# 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
|