diff --git a/app/mailers/dashboard/mailer.rb b/app/mailers/dashboard/mailer.rb index 8eff9207a..6bd14f110 100644 --- a/app/mailers/dashboard/mailer.rb +++ b/app/mailers/dashboard/mailer.rb @@ -1,5 +1,6 @@ class Dashboard::Mailer < ApplicationMailer layout "mailer" + after_action :check_deliverability def forward(proposal) @proposal = proposal @@ -38,4 +39,9 @@ class Dashboard::Mailer < ApplicationMailer def get_new_actions(new_actions_ids) Dashboard::Action.where(id: new_actions_ids) end + + def check_deliverability + mail.perform_deliveries = false unless Setting["dashboard.emails"] + end + end diff --git a/db/dev_seeds/settings.rb b/db/dev_seeds/settings.rb index 60073ae55..22605d21c 100644 --- a/db/dev_seeds/settings.rb +++ b/db/dev_seeds/settings.rb @@ -82,4 +82,6 @@ section "Creating Settings" do Setting.create(key: "proposals.poll_link", value: nil) Setting.create(key: "proposals.email_short_title", value: nil) Setting.create(key: "proposals.email_description", value: nil) + + Setting.create(key: "dashboard.emails", value: nil) end diff --git a/db/seeds.rb b/db/seeds.rb index 8269ed3e0..4e212606d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -139,5 +139,8 @@ Setting["proposals.email_description"] = nil Setting["proposals.poster_short_title"] = nil Setting["proposals.poster_description"] = nil +# Dashboard +Setting["dashboard.emails"] = nil + # Default custom pages load Rails.root.join("db", "pages.rb") diff --git a/spec/mailers/dashboard/mailer_spec.rb b/spec/mailers/dashboard/mailer_spec.rb index 6afbfb291..7cbd4904d 100644 --- a/spec/mailers/dashboard/mailer_spec.rb +++ b/spec/mailers/dashboard/mailer_spec.rb @@ -9,6 +9,44 @@ describe Dashboard::Mailer do day_offset: 0, published_proposal: true) } + before do + Setting["dashboard.emails"] = true + end + + after do + Setting["dashboard.emails"] = nil + end + + describe "#forward" do + let!(:proposal) { create(:proposal) } + + before do + ActionMailer::Base.deliveries.clear + end + + it "Disables email delivery using setting" do + Setting["dashboard.emails"] = nil + + Dashboard::Mailer.forward(proposal).deliver_now + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + + it "sends forward email" do + Dashboard::Mailer.forward(proposal).deliver_now + + email = open_last_email + + expect(email).to deliver_from("CONSUL ") + expect(email).to deliver_to(proposal.author) + expect(email).to have_subject(proposal.title) + expect(email).to have_body_text("Support this proposal") + expect(email).to have_body_text("Share in") + expect(email).to have_body_text(proposal_path(proposal)) + end + + end + describe "#new_actions_notification rake task" do before do @@ -25,6 +63,16 @@ describe Dashboard::Mailer do describe "#new_actions_notification_rake_created" do let!(:proposal) { create(:proposal, :draft) } + it "Disables email delivery using setting" do + Setting["dashboard.emails"] = nil + + action.update(published_proposal: false) + resource.update(published_proposal: false) + run_rake_task + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + it "sends emails when detect new actions for draft proposal" do action.update(published_proposal: false) resource.update(published_proposal: false) @@ -60,6 +108,15 @@ describe Dashboard::Mailer do describe "#new_actions_notification_rake_published" do let!(:proposal) { create(:proposal) } + it "Disables email delivery using setting" do + Setting["dashboard.emails"] = nil + ActionMailer::Base.deliveries.clear + + run_rake_task + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + it "sends emails when detect new actions for proposal" do run_rake_task @@ -104,7 +161,17 @@ describe Dashboard::Mailer do let!(:proposal) { build(:proposal, :draft) } - it "sends emails when detect new actions when create a proposal" do + it "Disables email delivery using setting" do + Setting["dashboard.emails"] = nil + + action.update(published_proposal: false) + resource.update(published_proposal: false) + proposal.save + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + + it "sends emails if new actions detected when creating a proposal" do action.update(published_proposal: false) resource.update(published_proposal: false) proposal.save @@ -162,6 +229,15 @@ describe Dashboard::Mailer do let!(:proposed_action) { create(:dashboard_action, :proposed_action, :active, day_offset: 0, published_proposal: true) } + it "Disables email delivery using setting" do + Setting["dashboard.emails"] = nil + + proposal.save + proposal.publish + + expect(ActionMailer::Base.deliveries.count).to eq(0) + end + it "sends emails when detect new actions when publish a proposal" do proposal.save proposal.publish @@ -202,4 +278,5 @@ describe Dashboard::Mailer do expect(email).to have_body_text("Go ahead, discover them!") end end + end