From 148ac6dcb4b19f8d587ed124d514bf831a381cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 14 Mar 2025 00:01:05 +0100 Subject: [PATCH] Move comment_on tests to mailer specs These tests don't use the browser to send emails since commit e21588ec1. However, note that this commit actually actually decreased our test coverage somehow; since then, we're no longer testing whether we send an email to the author after clicking the "Publish comment" button. We might need to add a test for this in the `spec/system/comments_spec.rb` file... but that's a story for another time. Note we need to stub the `deliver_later` method; otherwise, `open_last_email` would raise an exception since no email would have been delivered. We're moving them now because these tests use the `open_last_email` method, and we're looking for places where using this method might result in a flaky test when used inside a system test. --- spec/mailers/mailer_spec.rb | 157 ++++++++++++++++++++++++ spec/support/common_actions/comments.rb | 7 -- spec/system/emails_spec.rb | 141 --------------------- 3 files changed, 157 insertions(+), 148 deletions(-) diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index 25d5887d8..14020183a 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -2,6 +2,10 @@ require "rails_helper" describe Mailer do describe "#comment" do + before do + allow_any_instance_of(ActionMailer::MessageDelivery).to receive(:deliver_later), &:deliver_now + end + it "sends emails in the user's locale" do user = create(:user, locale: "es") proposal = create(:proposal, author: user) @@ -28,6 +32,159 @@ describe Mailer do expect(email.subject).to include("commented on your proposal") end + + context "Proposal comments" do + let(:user) { create(:user, email_on_comment: true) } + let(:proposal) { create(:proposal, author: user) } + + it "Send email on proposal comment" do + comment_on(proposal) + + email = open_last_email + 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 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 + + it "Do not send email about own proposal comments" do + comment_on(proposal, user) + expect { open_last_email }.to raise_error "No email has been sent!" + end + + it "Do not send email about proposal comment unless set in preferences" do + user.update!(email_on_comment: false) + comment_on(proposal) + expect { open_last_email }.to raise_error "No email has been sent!" + end + end + + context "Debate comments" do + let(:user) { create(:user, email_on_comment: true) } + let(:debate) { create(:debate, author: user) } + + scenario "Send email on debate comment" do + comment_on(debate) + + email = open_last_email + 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 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 + comment_on(debate, user) + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about debate comment unless set in preferences" do + user.update!(email_on_comment: false) + comment_on(debate) + expect { open_last_email }.to raise_error "No email has been sent!" + end + end + + context "Budget investments comments" do + let(:user) { create(:user, email_on_comment: true) } + let(:investment) { create(:budget_investment, author: user, budget: create(:budget)) } + + scenario "Send email on budget investment comment" do + comment_on(investment) + + email = open_last_email + 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 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 + comment_on(investment, user) + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about budget investment comment unless set in preferences" do + user.update!(email_on_comment: false) + comment_on(investment) + expect { open_last_email }.to raise_error "No email has been sent!" + end + end + + context "Topic comments" do + let(:user) { create(:user, email_on_comment: true) } + let(:proposal) { create(:proposal) } + let(:topic) { create(:topic, author: user, community: proposal.community) } + + scenario "Send email on topic comment" do + comment_on(topic) + + email = open_last_email + 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 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 + comment_on(topic, user) + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about topic comment unless set in preferences" do + user.update!(email_on_comment: false) + comment_on(topic) + expect { open_last_email }.to raise_error "No email has been sent!" + end + end + + context "Poll comments" do + let(:user) { create(:user, email_on_comment: true) } + let(:poll) { create(:poll, author: user) } + + scenario "Send email on poll comment" do + comment_on(poll) + + email = open_last_email + 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 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 + comment_on(poll, user) + expect { open_last_email }.to raise_error "No email has been sent!" + end + + scenario "Do not send email about poll question comment unless set in preferences" do + user.update!(email_on_comment: false) + comment_on(poll) + expect { open_last_email }.to raise_error "No email has been sent!" + end + end + + def comment_on(commentable, user = nil) + user ||= create(:user) + + comment = create(:comment, commentable: commentable, user: user) + CommentNotifier.new(comment: comment).process + end end describe "#user_invite" do diff --git a/spec/support/common_actions/comments.rb b/spec/support/common_actions/comments.rb index 1f600470b..0a0f55d86 100644 --- a/spec/support/common_actions/comments.rb +++ b/spec/support/common_actions/comments.rb @@ -1,11 +1,4 @@ module Comments - def comment_on(commentable, user = nil) - user ||= create(:user) - - comment = create(:comment, commentable: commentable, user: user) - CommentNotifier.new(comment: comment).process - end - def reply_to(comment, replier: create(:user)) login_as(replier) diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index 44b999252..f79cf50d6 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -39,147 +39,6 @@ describe "Emails" do expect(email).to have_body_text(edit_user_password_path) end - context "Proposal comments" do - let(:user) { create(:user, email_on_comment: true) } - let(:proposal) { create(:proposal, author: user) } - - scenario "Send email on proposal comment" do - comment_on(proposal) - - email = open_last_email - 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 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 - comment_on(proposal, user) - expect { open_last_email }.to raise_error("No email has been sent!") - end - - scenario "Do not send email about proposal comment unless set in preferences" do - user.update!(email_on_comment: false) - comment_on(proposal) - expect { open_last_email }.to raise_error("No email has been sent!") - end - end - - context "Debate comments" do - let(:user) { create(:user, email_on_comment: true) } - let(:debate) { create(:debate, author: user) } - - scenario "Send email on debate comment" do - comment_on(debate) - - email = open_last_email - 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 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 - comment_on(debate, user) - expect { open_last_email }.to raise_error("No email has been sent!") - end - - scenario "Do not send email about debate comment unless set in preferences" do - user.update!(email_on_comment: false) - comment_on(debate) - expect { open_last_email }.to raise_error("No email has been sent!") - end - end - - context "Budget investments comments" do - let(:user) { create(:user, email_on_comment: true) } - let(:investment) { create(:budget_investment, author: user, budget: create(:budget)) } - - scenario "Send email on budget investment comment" do - comment_on(investment) - - email = open_last_email - 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 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 - comment_on(investment, user) - expect { open_last_email }.to raise_error("No email has been sent!") - end - - scenario "Do not send email about budget investment comment unless set in preferences" do - user.update!(email_on_comment: false) - comment_on(investment) - expect { open_last_email }.to raise_error("No email has been sent!") - end - end - - context "Topic comments" do - let(:user) { create(:user, email_on_comment: true) } - let(:proposal) { create(:proposal) } - let(:topic) { create(:topic, author: user, community: proposal.community) } - - scenario "Send email on topic comment" do - comment_on(topic) - - email = open_last_email - 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 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 - comment_on(topic, user) - expect { open_last_email }.to raise_error("No email has been sent!") - end - - scenario "Do not send email about topic comment unless set in preferences" do - user.update!(email_on_comment: false) - comment_on(topic) - expect { open_last_email }.to raise_error("No email has been sent!") - end - end - - context "Poll comments" do - let(:user) { create(:user, email_on_comment: true) } - let(:poll) { create(:poll, author: user) } - - scenario "Send email on poll comment" do - comment_on(poll) - - email = open_last_email - 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 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 - comment_on(poll, user) - expect { open_last_email }.to raise_error("No email has been sent!") - end - - scenario "Do not send email about poll question comment unless set in preferences" do - user.update!(email_on_comment: false) - comment_on(poll) - expect { open_last_email }.to raise_error("No email has been sent!") - end - end - context "Comment replies" do let(:user) { create(:user, email_on_comment_reply: true) } let(:debate) { create(:debate) }