2020-03-23 20:46:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ContactControllerTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
@rol = create :rol
|
|
|
|
@site = @rol.site
|
|
|
|
@usuarie = @rol.usuarie
|
2020-09-29 21:22:28 +00:00
|
|
|
|
|
|
|
@site.update contact: true, design: Design.find_by_gem('editorial-autogestiva-jekyll-theme')
|
|
|
|
@site.config.write
|
|
|
|
@site.reload
|
2021-04-15 14:34:42 +00:00
|
|
|
@host = { host: "api.#{Site.domain}" }
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
@site&.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'el sitio tiene que existir' do
|
|
|
|
@site.destroy
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
get v1_site_contact_cookie_url(@site.hostname, **@host)
|
2020-09-29 21:22:28 +00:00
|
|
|
|
|
|
|
assert_not cookies[@site.name]
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
post v1_site_contact_url(site_id: @site.hostname, form: :contacto, **@host),
|
2020-03-23 20:46:19 +00:00
|
|
|
params: {
|
|
|
|
name: SecureRandom.hex,
|
|
|
|
pronouns: SecureRandom.hex,
|
|
|
|
contact: SecureRandom.hex,
|
|
|
|
from: "#{SecureRandom.hex}@sutty.nl",
|
|
|
|
body: SecureRandom.hex,
|
2020-09-29 21:22:28 +00:00
|
|
|
consent: true
|
2020-03-23 20:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:22:28 +00:00
|
|
|
assert_response :precondition_required
|
|
|
|
assert_equal 'expired_or_invalid_cookie', response.body
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'hay que enviar desde el sitio principal' do
|
2021-04-15 14:34:42 +00:00
|
|
|
get v1_site_contact_cookie_url(@site.hostname, **@host)
|
|
|
|
post v1_site_contact_url(site_id: @site.hostname, form: :contacto, **@host),
|
2020-03-23 20:46:19 +00:00
|
|
|
params: {
|
|
|
|
name: SecureRandom.hex,
|
|
|
|
pronouns: SecureRandom.hex,
|
|
|
|
contact: SecureRandom.hex,
|
|
|
|
from: "#{SecureRandom.hex}@sutty.nl",
|
|
|
|
body: SecureRandom.hex,
|
2020-09-29 21:22:28 +00:00
|
|
|
consent: true
|
2020-03-23 20:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 21:22:28 +00:00
|
|
|
assert_response :precondition_required
|
|
|
|
assert_equal 'site_is_not_origin', response.body
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'hay que dar consentimiento' do
|
2021-04-15 14:34:42 +00:00
|
|
|
get v1_site_contact_cookie_url(@site.hostname, **@host)
|
|
|
|
post v1_site_contact_url(site_id: @site.hostname, form: :contacto, **@host),
|
2020-03-23 20:46:19 +00:00
|
|
|
headers: {
|
2020-09-29 21:22:28 +00:00
|
|
|
origin: @site.url
|
2020-03-23 20:46:19 +00:00
|
|
|
},
|
|
|
|
params: {
|
|
|
|
name: SecureRandom.hex,
|
|
|
|
pronouns: SecureRandom.hex,
|
|
|
|
contact: SecureRandom.hex,
|
|
|
|
from: "#{SecureRandom.hex}@sutty.nl",
|
|
|
|
body: SecureRandom.hex
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:22:28 +00:00
|
|
|
assert_response :precondition_required
|
|
|
|
assert_equal 'no_consent', response.body
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'enviar un mensaje genera correos' do
|
2020-03-25 18:36:06 +00:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
redirect = "#{@site.url}?thanks"
|
2020-03-25 18:36:06 +00:00
|
|
|
|
2020-03-23 20:46:19 +00:00
|
|
|
10.times do
|
|
|
|
create :rol, site: @site
|
|
|
|
end
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
get v1_site_contact_cookie_url(@site.hostname, **@host)
|
|
|
|
post v1_site_contact_url(site_id: @site.hostname, form: :contacto, **@host),
|
2020-03-23 20:46:19 +00:00
|
|
|
headers: {
|
|
|
|
Origin: @site.url
|
|
|
|
},
|
|
|
|
params: {
|
|
|
|
name: SecureRandom.hex,
|
|
|
|
pronouns: SecureRandom.hex,
|
|
|
|
contact: SecureRandom.hex,
|
|
|
|
from: "#{SecureRandom.hex}@sutty.nl",
|
|
|
|
body: SecureRandom.hex,
|
2020-09-29 21:22:28 +00:00
|
|
|
consent: true,
|
2020-03-25 18:36:06 +00:00
|
|
|
redirect: redirect
|
2020-03-23 20:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:36:06 +00:00
|
|
|
assert_equal redirect, response.headers['Location']
|
2020-03-23 20:46:19 +00:00
|
|
|
assert_equal 2, ActionMailer::Base.deliveries.size
|
|
|
|
end
|
2020-03-25 18:49:36 +00:00
|
|
|
|
|
|
|
test 'se puede enviar mensajes a dominios propios' do
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
|
|
|
|
@site.update name: 'example.org.'
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
redirect = "#{@site.url}?thanks"
|
2020-03-25 18:49:36 +00:00
|
|
|
|
|
|
|
10.times do
|
|
|
|
create :rol, site: @site
|
|
|
|
end
|
|
|
|
|
2021-04-15 14:34:42 +00:00
|
|
|
get v1_site_contact_cookie_url(@site.hostname, **@host)
|
|
|
|
post v1_site_contact_url(site_id: @site.hostname, form: :contacto, **@host),
|
2020-09-29 21:22:28 +00:00
|
|
|
headers: { origin: @site.url },
|
2020-03-25 18:49:36 +00:00
|
|
|
params: {
|
|
|
|
name: SecureRandom.hex,
|
|
|
|
pronouns: SecureRandom.hex,
|
|
|
|
contact: SecureRandom.hex,
|
|
|
|
from: "#{SecureRandom.hex}@sutty.nl",
|
|
|
|
body: SecureRandom.hex,
|
2020-09-29 21:22:28 +00:00
|
|
|
consent: true,
|
2020-03-25 18:49:36 +00:00
|
|
|
redirect: redirect
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal redirect, response.headers['Location']
|
|
|
|
assert_equal 2, ActionMailer::Base.deliveries.size
|
|
|
|
end
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|