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) comment = create(:comment, commentable: proposal) email = I18n.with_locale :en do Mailer.comment(comment) end expect(email.subject).to include("comentado") end it "reads the from address at runtime" do Setting["mailer_from_name"] = "New organization" Setting["mailer_from_address"] = "new@consul.dev" email = Mailer.comment(create(:comment)) expect(email).to deliver_from "New organization " end it "sends emails for comments on legislation proposals" do email = Mailer.comment(create(:legislation_proposal_comment)) 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 it "uses the default locale setting" do Setting["locales.default"] = "es" Mailer.user_invite("invited@consul.dev").deliver_now expect(ActionMailer::Base.deliveries.last.body.to_s).to match "