WeeklyOverviewWorker: add feature flip on worker

The main objective is to avoid batch mail sent in staging
This commit is contained in:
Simon Lehericey 2017-05-19 16:21:11 +02:00
parent 45209619a4
commit b48549d765
3 changed files with 33 additions and 15 deletions

View file

@ -2,9 +2,12 @@ class WeeklyOverviewWorker
include Sidekiq::Worker
def perform(*args)
Gestionnaire.all
.map { |gestionnaire| [gestionnaire, gestionnaire.last_week_overview] }
.reject { |_, overview| overview.nil? }
.each { |gestionnaire, overview| GestionnaireMailer.last_week_overview(gestionnaire, overview).deliver_now }
# Feature flipped to avoid mails in staging due to unprocessed dossier
if Features.weekly_overview
Gestionnaire.all
.map { |gestionnaire| [gestionnaire, gestionnaire.last_week_overview] }
.reject { |_, overview| overview.nil? }
.each { |gestionnaire, overview| GestionnaireMailer.last_week_overview(gestionnaire, overview).deliver_now }
end
end
end

View file

@ -1 +1,2 @@
remote_storage: false
weekly_overview: false

View file

@ -6,25 +6,39 @@ RSpec.describe WeeklyOverviewWorker, type: :worker do
let(:overview) { double('overview') }
let(:mailer_double) { double('mailer', deliver_now: true) }
context 'with one gestionnaire with one overview' do
before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(overview)
allow(GestionnaireMailer).to receive(:last_week_overview).and_return(mailer_double)
WeeklyOverviewWorker.new.perform
context 'if the feature is enabled' do
before { allow(Features).to receive(:weekly_overview).and_return(true) }
context 'with one gestionnaire with one overview' do
before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(overview)
allow(GestionnaireMailer).to receive(:last_week_overview).and_return(mailer_double)
WeeklyOverviewWorker.new.perform
end
it { expect(GestionnaireMailer).to have_received(:last_week_overview).with(gestionnaire, overview) }
it { expect(mailer_double).to have_received(:deliver_now) }
end
it { expect(GestionnaireMailer).to have_received(:last_week_overview).with(gestionnaire, overview) }
it { expect(mailer_double).to have_received(:deliver_now) }
context 'with one gestionnaire with no overviews' do
before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(nil)
allow(GestionnaireMailer).to receive(:last_week_overview)
WeeklyOverviewWorker.new.perform
end
it { expect(GestionnaireMailer).not_to have_received(:last_week_overview) }
end
end
context 'with one gestionnaire with no overviews' do
context 'if the feature is disabled' do
before { allow(Features).to receive(:weekly_overview).and_return(false) }
before :each do
expect_any_instance_of(Gestionnaire).to receive(:last_week_overview).and_return(nil)
allow(GestionnaireMailer).to receive(:last_week_overview)
allow(Gestionnaire).to receive(:all)
WeeklyOverviewWorker.new.perform
end
it { expect(GestionnaireMailer).not_to have_received(:last_week_overview) }
it { expect(Gestionnaire).not_to receive(:all) }
end
end
end