diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 08bb60faa..0aa0834a7 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -47,6 +47,7 @@ @import "sdg/**/*"; @import "sdg_management/*"; @import "sdg_management/**/*"; +@import "subscriptions"; @import "widgets/**/*"; @import "custom"; diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 4c38b27d7..d3c5ca090 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -74,7 +74,8 @@ main { &.sdg-goals-index, &.sdg-goal-show, &.topic-edit, - &.topic-new { + &.topic-new, + &.subscriptions-edit { @include grid-column-gutter; } } diff --git a/app/assets/stylesheets/subscriptions.scss b/app/assets/stylesheets/subscriptions.scss new file mode 100644 index 000000000..6affddaaf --- /dev/null +++ b/app/assets/stylesheets/subscriptions.scss @@ -0,0 +1,5 @@ +.subscriptions-edit { + form { + max-width: $global-width * 7 / 12; + } +} diff --git a/app/components/subscriptions/edit_component.html.erb b/app/components/subscriptions/edit_component.html.erb new file mode 100644 index 000000000..09e65926b --- /dev/null +++ b/app/components/subscriptions/edit_component.html.erb @@ -0,0 +1,13 @@ +
+ <%= form_for user, url: subscriptions_path(token: user.subscriptions_token) do |f| %> +

<%= t("account.show.notifications") %>

+ +
<%= f.check_box :email_on_comment %>
+
<%= f.check_box :email_on_comment_reply %>
+
<%= f.check_box :newsletter %>
+
<%= f.check_box :email_digest %>
+
<%= f.check_box :email_on_direct_message %>
+ + <%= f.submit t("account.show.save_changes_submit"), class: "button margin-top" %> + <% end %> +
diff --git a/app/components/subscriptions/edit_component.rb b/app/components/subscriptions/edit_component.rb new file mode 100644 index 000000000..bd5771102 --- /dev/null +++ b/app/components/subscriptions/edit_component.rb @@ -0,0 +1,7 @@ +class Subscriptions::EditComponent < ApplicationComponent + attr_reader :user + + def initialize(user) + @user = user + end +end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb new file mode 100644 index 000000000..580bd0e34 --- /dev/null +++ b/app/controllers/subscriptions_controller.rb @@ -0,0 +1,38 @@ +class SubscriptionsController < ApplicationController + before_action :set_user, :set_user_locale + skip_authorization_check + + def edit + end + + def update + @user.update!(subscriptions_params) + redirect_to edit_subscriptions_path(token: @user.subscriptions_token), + notice: t("flash.actions.save_changes.notice") + end + + private + + def set_user + @user = if params[:token].present? + User.find_by!(subscriptions_token: params[:token]) + else + current_user || raise(CanCan::AccessDenied) + end + end + + def subscriptions_params + params.require(:user).permit(allowed_params) + end + + def allowed_params + [:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter] + end + + def set_user_locale + if params[:locale].blank? + I18n.locale = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym } + session[:locale] = I18n.locale + end + end +end diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 9fbb8ec88..b0c0d3541 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -9,6 +9,7 @@ class Mailer < ApplicationMailer @comment = comment @commentable = comment.commentable @email_to = @commentable.author.email + manage_subscriptions_token(@commentable.author) with_user(@commentable.author) do subject = t("mailers.comment.subject", commentable: t("activerecord.models.#{@commentable.class.name.underscore}", count: 1).downcase) @@ -19,6 +20,7 @@ class Mailer < ApplicationMailer def reply(reply) @email = ReplyEmail.new(reply) @email_to = @email.to + manage_subscriptions_token(@email.recipient) with_user(@email.recipient) do mail(to: @email_to, subject: @email.subject) if @email.can_be_sent? @@ -41,6 +43,7 @@ class Mailer < ApplicationMailer @direct_message = direct_message @receiver = @direct_message.receiver @email_to = @receiver.email + manage_subscriptions_token(@receiver) with_user(@receiver) do mail(to: @email_to, subject: t("mailers.direct_message_for_receiver.subject")) @@ -60,6 +63,7 @@ class Mailer < ApplicationMailer def proposal_notification_digest(user, notifications) @notifications = notifications @email_to = user.email + manage_subscriptions_token(user) with_user(user) do mail(to: @email_to, subject: t("mailers.proposal_notification_digest.title", org_name: Setting["org_name"])) @@ -116,6 +120,7 @@ class Mailer < ApplicationMailer def newsletter(newsletter, recipient_email) @newsletter = newsletter @email_to = recipient_email + manage_subscriptions_token(User.find_by(email: @email_to)) mail(to: @email_to, from: @newsletter.from, subject: @newsletter.subject) end @@ -150,4 +155,9 @@ class Mailer < ApplicationMailer mail.perform_deliveries = false end end + + def manage_subscriptions_token(user) + user.add_subscriptions_token + @token = user.subscriptions_token + end end diff --git a/app/models/user.rb b/app/models/user.rb index 0c6e03bfe..0dde87ca7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -415,6 +415,10 @@ class User < ApplicationRecord devise_mailer.send(notification, self, *args).deliver_later end + def add_subscriptions_token + update!(subscriptions_token: SecureRandom.base58(32)) if subscriptions_token.blank? + end + private def clean_document_number diff --git a/app/views/mailer/comment.html.erb b/app/views/mailer/comment.html.erb index c4e7d1354..d831b60bc 100644 --- a/app/views/mailer/comment.html.erb +++ b/app/views/mailer/comment.html.erb @@ -18,6 +18,13 @@

- <%= t("mailers.config.manage_email_subscriptions") %> <%= link_to t("account.show.title"), account_url, style: "color: #2895F1; text-decoration:none;" %> + <%= sanitize(t("mailers.config.unsubscribe_text", + notifications: link_to( + t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;" + ), + notification: User.human_attribute_name(:email_on_comment) + )) %>

diff --git a/app/views/mailer/direct_message_for_receiver.html.erb b/app/views/mailer/direct_message_for_receiver.html.erb index b563ce621..ae9637d3e 100644 --- a/app/views/mailer/direct_message_for_receiver.html.erb +++ b/app/views/mailer/direct_message_for_receiver.html.erb @@ -26,9 +26,10 @@

- <%= sanitize(t("mailers.direct_message_for_receiver.unsubscribe", - account: link_to(t("mailers.direct_message_for_receiver.unsubscribe_account"), - account_url, style: "color: #2895F1; text-decoration: none;"))) %> + <%= sanitize(t("mailers.direct_message_for_receiver.unsubscribe_text", + notifications: link_to(t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;"))) %>

diff --git a/app/views/mailer/newsletter.html.erb b/app/views/mailer/newsletter.html.erb index 3b45f6105..e28be0eea 100644 --- a/app/views/mailer/newsletter.html.erb +++ b/app/views/mailer/newsletter.html.erb @@ -2,4 +2,15 @@

<%= auto_link_already_sanitized_html wysiwyg(@newsletter.body) %>

+ +

+ <%= sanitize(t("mailers.config.unsubscribe_text", + notifications: link_to( + t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;" + ), + notification: User.human_attribute_name(:newsletter) + )) %> +

diff --git a/app/views/mailer/proposal_notification_digest.html.erb b/app/views/mailer/proposal_notification_digest.html.erb index 576b592cd..7b3d0f58b 100644 --- a/app/views/mailer/proposal_notification_digest.html.erb +++ b/app/views/mailer/proposal_notification_digest.html.erb @@ -70,9 +70,10 @@

- <%= sanitize(t("mailers.proposal_notification_digest.unsubscribe", - account: link_to(t("mailers.proposal_notification_digest.unsubscribe_account"), - account_url, style: "color: #2895F1; text-decoration: none;"))) %> + <%= sanitize(t("mailers.proposal_notification_digest.unsubscribe_text", + notifications: link_to(t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;"))) %>

diff --git a/app/views/mailer/reply.html.erb b/app/views/mailer/reply.html.erb index 38419aacf..ac1b7ee55 100644 --- a/app/views/mailer/reply.html.erb +++ b/app/views/mailer/reply.html.erb @@ -18,6 +18,13 @@

- <%= t("mailers.config.manage_email_subscriptions") %> <%= link_to t("account.show.title"), account_url, style: "color: #2895F1; text-decoration:none;" %> + <%= sanitize(t("mailers.config.unsubscribe_text", + notifications: link_to( + t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;" + ), + notification: User.human_attribute_name(:email_on_comment_reply) + )) %>

diff --git a/app/views/subscriptions/edit.html.erb b/app/views/subscriptions/edit.html.erb new file mode 100644 index 000000000..cc3b8a685 --- /dev/null +++ b/app/views/subscriptions/edit.html.erb @@ -0,0 +1 @@ +<%= render Subscriptions::EditComponent.new(@user) %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index ce7e4f199..973455a05 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -272,7 +272,7 @@ en: password: "Password" current_password: "Current password" email_digest: "Receive a summary of proposal notifications" - email_on_comment: "Notify me by email when someone comments on my proposals or debates" + email_on_comment: "Notify me by email when someone comments on my contents" email_on_comment_reply: "Notify me by email when someone replies to my comments" email_on_direct_message: "Receive emails about direct messages" newsletter: "Receive by email website relevant information" diff --git a/config/locales/en/mailers.yml b/config/locales/en/mailers.yml index 552aa9831..f70669073 100644 --- a/config/locales/en/mailers.yml +++ b/config/locales/en/mailers.yml @@ -8,7 +8,8 @@ en: subject: Someone has commented on your %{commentable} title: New comment config: - manage_email_subscriptions: To stop receiving these emails change your settings in + notifications_link: Notifications + unsubscribe_text: 'To unsubscribe from these emails, visit %{notifications} and uncheck "%{notification}".' email_verification: click_here_to_verify: this link instructions_2: This email will verify your account with %{document_type} %{document_number}. If these don't belong to you, please don't click on the previous link and ignore this email. @@ -26,14 +27,12 @@ en: title: "Proposal notifications in %{org_name}" share: Share proposal comment: Comment proposal - unsubscribe: "If you don't want to receive any proposal notifications, visit %{account} and uncheck 'Receive a summary of proposal notifications'." - unsubscribe_account: My account + unsubscribe_text: "If you don't want to receive any proposal notifications, visit %{notifications} and uncheck 'Receive a summary of proposal notifications'." unfollow: "Visit this proposal and unfollow it to stop receiving notifications." direct_message_for_receiver: subject: "You have received a new private message" reply: Reply to %{sender} - unsubscribe: "If you don't want to receive direct messages, visit %{account} and uncheck 'Receive emails about direct messages'." - unsubscribe_account: My account + unsubscribe_text: "If you don't want to receive direct messages, visit %{notifications} and uncheck 'Receive emails about direct messages'." direct_message_for_sender: subject: "You have sent a new private message" title: "You have sent a new private message to %{receiver} with the content:" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 74d03742a..82c682edf 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -272,7 +272,7 @@ es: password: "Contraseña" current_password: "Contraseña actual" email_digest: "Recibir resumen de notificaciones sobre propuestas" - email_on_comment: "Recibir un email cuando alguien comenta en mis propuestas o debates" + email_on_comment: "Recibir un email cuando alguien comenta en mis contenidos" email_on_comment_reply: "Recibir un email cuando alguien contesta a mis comentarios" email_on_direct_message: "Recibir emails con mensajes privados" newsletter: "Recibir emails con información interesante sobre la web" diff --git a/config/locales/es/mailers.yml b/config/locales/es/mailers.yml index ec4974a4d..0cc21c5b5 100644 --- a/config/locales/es/mailers.yml +++ b/config/locales/es/mailers.yml @@ -8,7 +8,8 @@ es: subject: Alguien ha comentado en tu %{commentable} title: Nuevo comentario config: - manage_email_subscriptions: Puedes dejar de recibir estos emails cambiando tu configuración en + notifications_link: Notificaciones + unsubscribe_text: 'Para darse de baja de estos emails, puedes entrar en %{notifications} y desmarcar la opción "%{notification}".' email_verification: click_here_to_verify: en este enlace instructions_2: Este email es para verificar tu cuenta con %{document_type} %{document_number}. Si esos no son tus datos, por favor no pulses el enlace anterior e ignora este email. @@ -26,14 +27,12 @@ es: title: "Notificaciones de propuestas en %{org_name}" share: Compartir propuesta comment: Comentar propuesta - unsubscribe: "Si no quieres recibir notificaciones de propuestas, puedes entrar en %{account} y desmarcar la opción 'Recibir resumen de notificaciones sobre propuestas'." - unsubscribe_account: Mi cuenta + unsubscribe_text: "Si no quieres recibir notificaciones de propuestas, puedes entrar en %{notifications} y desmarcar la opción 'Recibir resumen de notificaciones sobre propuestas'." unfollow: "Si no quieres recibir más notificaciones, visita esta propuesta y deja de seguirla." direct_message_for_receiver: subject: "Has recibido un nuevo mensaje privado" reply: Responder a %{sender} - unsubscribe: "Si no quieres recibir mensajes privados, puedes entrar en %{account} y desmarcar la opción 'Recibir emails con mensajes privados'." - unsubscribe_account: Mi cuenta + unsubscribe_text: "Si no quieres recibir mensajes privados, puedes entrar en %{notifications} y desmarcar la opción 'Recibir emails con mensajes privados'." direct_message_for_sender: subject: "Has enviado un nuevo mensaje privado" title: "Has enviado un nuevo mensaje privado a %{receiver} con el siguiente contenido:" diff --git a/config/routes/account.rb b/config/routes/account.rb index 2b377bc36..e4baa306e 100644 --- a/config/routes/account.rb +++ b/config/routes/account.rb @@ -1,3 +1,5 @@ resource :account, controller: "account", only: [:show, :update, :delete] do get :erase, on: :collection end + +resource :subscriptions, only: [:edit, :update] diff --git a/db/migrate/20201218082830_add_subscriptions_token_to_users.rb b/db/migrate/20201218082830_add_subscriptions_token_to_users.rb new file mode 100644 index 000000000..5d447d019 --- /dev/null +++ b/db/migrate/20201218082830_add_subscriptions_token_to_users.rb @@ -0,0 +1,5 @@ +class AddSubscriptionsTokenToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :subscriptions_token, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 9758897cc..050cb482f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1625,6 +1625,7 @@ ActiveRecord::Schema.define(version: 2021_11_03_112944) do t.boolean "public_interests", default: false t.boolean "recommended_debates", default: true t.boolean "recommended_proposals", default: true + t.string "subscriptions_token" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["date_of_birth"], name: "index_users_on_date_of_birth" t.index ["email"], name: "index_users_on_email", unique: true diff --git a/spec/components/subscriptions/edit_component_spec.rb b/spec/components/subscriptions/edit_component_spec.rb new file mode 100644 index 000000000..4e8fa6323 --- /dev/null +++ b/spec/components/subscriptions/edit_component_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" + +describe Subscriptions::EditComponent do + let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) } + let(:component) { Subscriptions::EditComponent.new(user) } + + it "renders checkboxes to change the subscriptions preferences" do + render_inline component + + expect(page).to have_content "Notifications" + expect(page).to have_field "Notify me by email when someone comments on my contents", type: :checkbox + expect(page).to have_field "Notify me by email when someone replies to my comments", type: :checkbox + expect(page).to have_field "Receive by email website relevant information", type: :checkbox + expect(page).to have_field "Receive a summary of proposal notifications", type: :checkbox + expect(page).to have_field "Receive emails about direct messages", type: :checkbox + expect(page).to have_button "Save changes" + end +end diff --git a/spec/controllers/subscriptions_controller_spec.rb b/spec/controllers/subscriptions_controller_spec.rb new file mode 100644 index 000000000..3c1d39414 --- /dev/null +++ b/spec/controllers/subscriptions_controller_spec.rb @@ -0,0 +1,23 @@ +require "rails_helper" + +describe SubscriptionsController do + describe "GET edit" do + it "returns a 404 code with a wrong token" do + expect { get :edit, params: { token: "non_existent" } }.to raise_error ActiveRecord::RecordNotFound + end + + it "doesn't allow access to anonymous users without a token" do + get :edit, params: { token: "" } + + expect(response).to redirect_to "/" + expect(flash[:alert]).to eq "You do not have permission to access this page." + end + + it "shows the 'not allowed' message in the current locale" do + get :edit, params: { token: "", locale: :es } + + expect(response).to redirect_to "/" + expect(flash[:alert]).to eq "No tienes permiso para acceder a esta página." + end + end +end diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index d69d9cc01..21231ca4d 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -29,4 +29,26 @@ describe Mailer do expect(email.subject).to include("commented on your proposal") end end + + describe "#manage_subscriptions_token" do + let(:user) { create(:user) } + let(:proposal) { create(:proposal, author: user) } + let(:comment) { create(:comment, commentable: proposal) } + + it "generates a subscriptions token when the receiver doesn't have one" do + user.update!(subscriptions_token: nil) + + Mailer.comment(comment).deliver_now + + expect(user.reload.subscriptions_token).to be_present + end + + it "uses the existing subscriptions token when the receivesr already has one" do + user.update!(subscriptions_token: "subscriptions_token_value") + + Mailer.comment(comment).deliver_now + + expect(user.subscriptions_token).to eq "subscriptions_token_value" + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9c326f846..72acf4f50 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -823,4 +823,22 @@ describe User do expect(other_user_legislation_proposal.reload).to be_hidden end end + + describe "#add_subscriptions_token" do + let(:user) { build(:user, subscriptions_token: nil) } + + it "generates a subscriptions token when the user doesn't have one" do + user.add_subscriptions_token + + expect(user.subscriptions_token).to be_present + end + + it "keeps the existing subscriptions token when the user already has one" do + user.update!(subscriptions_token: "already_set") + + user.add_subscriptions_token + + expect(user.subscriptions_token).to eq "already_set" + end + end end diff --git a/spec/system/admin/system_emails_spec.rb b/spec/system/admin/system_emails_spec.rb index e464998f2..b0717c98a 100644 --- a/spec/system/admin/system_emails_spec.rb +++ b/spec/system/admin/system_emails_spec.rb @@ -92,6 +92,7 @@ describe "System Emails" do href: proposal_url(proposal_b, anchor: "tab-notifications", host: app_host)) expect(page).to have_content("Proposal A Notification Body") expect(page).to have_content("Proposal B Notification Body") + expect(page).to have_link "Notifications" end scenario "#budget_investment_created" do @@ -155,6 +156,9 @@ describe "System Emails" do expect(page).to have_content comment.body expect(page).to have_link "Let's do...", href: debate_url(debate, host: app_host) + expect(page).to have_link("Notifications", + href: edit_subscriptions_url(token: user.subscriptions_token, + host: app_host)) end scenario "#reply" do @@ -172,6 +176,9 @@ describe "System Emails" do expect(page).to have_content reply.body expect(page).to have_link "Let's do...", href: comment_url(reply, host: app_host) + expect(page).to have_link("Notifications", + href: edit_subscriptions_url(token: user.subscriptions_token, + host: app_host)) end scenario "#direct_message_for_receiver" do @@ -182,6 +189,9 @@ describe "System Emails" do expect(page).to have_content "This is a sample of message's content." expect(page).to have_link "Reply to #{admin.user.name}", href: user_url(admin.user, host: app_host) + expect(page).to have_link("Notifications", + href: edit_subscriptions_url(token: admin.user.subscriptions_token, + host: app_host)) end scenario "#direct_message_for_sender" do diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index 9f9cb9398..57db5c376 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -50,8 +50,9 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your citizen proposal") expect(email).to deliver_to(proposal.author) expect(email).to have_body_text(proposal_path(proposal)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: proposal.author.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone comments on my contents"') end scenario "Do not send email about own proposal comments" do @@ -77,8 +78,9 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your debate") expect(email).to deliver_to(debate.author) expect(email).to have_body_text(debate_path(debate)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: debate.author.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone comments on my contents"') end scenario "Do not send email about own debate comments" do @@ -104,8 +106,9 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your investment") expect(email).to deliver_to(investment.author) expect(email).to have_body_text(budget_investment_path(investment, budget_id: investment.budget_id)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: investment.author.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone comments on my contents"') end scenario "Do not send email about own budget investments comments" do @@ -132,8 +135,9 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your topic") expect(email).to deliver_to(topic.author) expect(email).to have_body_text(community_topic_path(topic, community_id: topic.community_id)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: topic.author.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone comments on my contents"') end scenario "Do not send email about own topic comments" do @@ -159,8 +163,9 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your poll") expect(email).to deliver_to(poll.author) expect(email).to have_body_text(poll_path(poll)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: poll.author.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone comments on my contents"') end scenario "Do not send email about own poll comments" do @@ -188,8 +193,9 @@ describe "Emails" do expect(email).to deliver_to(user) expect(email).not_to have_body_text(debate_path(debate)) expect(email).to have_body_text(comment_path(Comment.last)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') end scenario "Do not send email about own replies to own comments" do @@ -232,7 +238,8 @@ describe "Emails" do expect(email).to have_body_text(direct_message.title) expect(email).to have_body_text(direct_message.body) expect(email).to have_body_text(direct_message.sender.name) - expect(email).to have_body_text(/#{user_path(direct_message.sender_id)}/) + expect(email).to have_body_text(user_path(direct_message.sender_id)) + expect(email).to have_body_text(edit_subscriptions_path(token: receiver.subscriptions_token)) end scenario "Sender email" do @@ -279,20 +286,20 @@ describe "Emails" do expect(email).to have_body_text(notification1.notifiable.body) expect(email).to have_body_text(proposal1.author.name) - expect(email).to have_body_text(/#{proposal_path(proposal1, anchor: "tab-notifications")}/) - expect(email).to have_body_text(/#{proposal_path(proposal1, anchor: "comments")}/) - expect(email).to have_body_text(/#{proposal_path(proposal1, anchor: "social-share")}/) + expect(email).to have_body_text(proposal_path(proposal1, anchor: "tab-notifications")) + expect(email).to have_body_text(proposal_path(proposal1, anchor: "comments")) + expect(email).to have_body_text(proposal_path(proposal1, anchor: "social-share")) expect(email).to have_body_text(proposal2.title) expect(email).to have_body_text(notification2.notifiable.title) expect(email).to have_body_text(notification2.notifiable.body) - expect(email).to have_body_text(/#{proposal_path(proposal2, anchor: "tab-notifications")}/) - expect(email).to have_body_text(/#{proposal_path(proposal2, anchor: "comments")}/) - expect(email).to have_body_text(/#{proposal_path(proposal2, anchor: "social-share")}/) + expect(email).to have_body_text(proposal_path(proposal2, anchor: "tab-notifications")) + expect(email).to have_body_text(proposal_path(proposal2, anchor: "comments")) + expect(email).to have_body_text(proposal_path(proposal2, anchor: "social-share")) expect(email).to have_body_text(proposal2.author.name) expect(email).not_to have_body_text(proposal3.title) - expect(email).to have_body_text(/#{account_path}/) + expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) expect(email).to have_body_text("Visit this proposal and unfollow it to stop receiving notifications.") notification1.reload @@ -336,7 +343,7 @@ describe "Emails" do email = open_last_email expect(email).to have_subject("Invitation to CONSUL") - expect(email).to have_body_text(/#{new_user_registration_path}/) + expect(email).to have_body_text(new_user_registration_path) end end @@ -458,8 +465,9 @@ describe "Emails" do expect(email).to deliver_to(user1) expect(email).not_to have_body_text(poll_path(poll)) expect(email).to have_body_text(comment_path(Comment.last)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: user1.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') end end @@ -489,6 +497,10 @@ describe "Emails" do expect(email).to have_subject("This is a different subject") expect(email).to deliver_from("no-reply@consul.dev") expect(email.body.encoded).to include("This is a different body") + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text( + edit_subscriptions_path(token: user_with_newsletter_in_segment_2.subscriptions_token)) + expect(email).to have_body_text('and uncheck "Receive by email website relevant information"') end end diff --git a/spec/system/subscriptions_spec.rb b/spec/system/subscriptions_spec.rb new file mode 100644 index 000000000..8cc6b7244 --- /dev/null +++ b/spec/system/subscriptions_spec.rb @@ -0,0 +1,51 @@ +require "rails_helper" + +describe "Subscriptions" do + let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) } + + context "Edit page" do + scenario "Render content in the user's preferred locale" do + user.update!(locale: "es") + visit edit_subscriptions_path(token: user.subscriptions_token) + + expect(page).to have_content "Notificaciones" + expect(page).to have_field "Recibir un email cuando alguien comenta en mis contenidos", type: :checkbox + expect(page).to have_field "Recibir un email cuando alguien contesta a mis comentarios", type: :checkbox + expect(page).to have_field "Recibir emails con información interesante sobre la web", type: :checkbox + expect(page).to have_field "Recibir resumen de notificaciones sobre propuestas", type: :checkbox + expect(page).to have_field "Recibir emails con mensajes privados", type: :checkbox + expect(page).to have_button "Guardar cambios" + end + + scenario "Use the locale in the parameters when accessing anonymously" do + visit edit_subscriptions_path(token: user.subscriptions_token, locale: :es) + + expect(page).to have_content "Notificaciones" + end + end + + context "Update" do + scenario "Allow updating the status notification" do + user.update!(email_on_comment: false, + email_on_comment_reply: true, + newsletter: true, + email_digest: false, + email_on_direct_message: true) + visit edit_subscriptions_path(token: user.subscriptions_token) + + check "Notify me by email when someone comments on my contents" + uncheck "Notify me by email when someone replies to my comments" + uncheck "Receive by email website relevant information" + check "Receive a summary of proposal notifications" + uncheck "Receive emails about direct messages" + click_button "Save changes" + + expect(page).to have_content "Changes saved" + expect(page).to have_field "Notify me by email when someone comments on my contents", checked: true + expect(page).to have_field "Notify me by email when someone replies to my comments", checked: false + expect(page).to have_field "Receive by email website relevant information", checked: false + expect(page).to have_field "Receive a summary of proposal notifications", checked: true + expect(page).to have_field "Receive emails about direct messages", checked: false + end + end +end