diff --git a/app/mailers/report_mailer.rb b/app/mailers/report_mailer.rb index 584b460d783..aacd9d463e9 100644 --- a/app/mailers/report_mailer.rb +++ b/app/mailers/report_mailer.rb @@ -16,4 +16,9 @@ def system_demand_report(email:, data:, name:) attachments['system_demand.csv'] = data mail(to: email, subject: t('report_mailer.system_demand_report.subject')) end + + def warn_error(email:, error:, env: Rails.env) + @error = error + mail(to: email, subject: "[#{env}] identity-idp error: #{error.class.name}") + end end diff --git a/app/services/service_provider_seeder.rb b/app/services/service_provider_seeder.rb index fee5f47d928..cfe816c2732 100644 --- a/app/services/service_provider_seeder.rb +++ b/app/services/service_provider_seeder.rb @@ -80,6 +80,12 @@ def check_for_missing_sps extra_sp_error = ExtraServiceProviderError.new( "Extra service providers found in DB: #{extra_sps.join(', ')}", ) - NewRelic::Agent.notice_error(extra_sp_error) + + if IdentityConfig.store.team_ursula_email.present? + ReportMailer.warn_error( + email: IdentityConfig.store.team_ursula_email, + error: extra_sp_error, + ).deliver_now_or_later + end end end diff --git a/app/views/report_mailer/warn_error.text.erb b/app/views/report_mailer/warn_error.text.erb new file mode 100644 index 00000000000..d114ae14356 --- /dev/null +++ b/app/views/report_mailer/warn_error.text.erb @@ -0,0 +1,5 @@ +**Error**: +<%= @error.class.name %> + +**Message**: +<%= @error.message %> diff --git a/config/application.yml.default b/config/application.yml.default index c6ccdf79486..4a7f718f502 100644 --- a/config/application.yml.default +++ b/config/application.yml.default @@ -324,6 +324,7 @@ sp_handoff_bounce_max_seconds: 2 show_user_attribute_deprecation_warnings: false otp_min_attempts_remaining_warning_count: 3 system_demand_report_email: 'foo@bar.com' +team_ursula_email: '' test_ssn_allowed_list: '' totp_code_interval: 30 unauthorized_scope_enabled: false diff --git a/lib/identity_config.rb b/lib/identity_config.rb index e7bf41a529e..363ae5f8764 100644 --- a/lib/identity_config.rb +++ b/lib/identity_config.rb @@ -431,6 +431,7 @@ def self.build_store(config_map) config.add(:sp_handoff_bounce_max_seconds, type: :integer) config.add(:state_tracking_enabled, type: :boolean) config.add(:system_demand_report_email, type: :string) + config.add(:team_ursula_email, type: :string) config.add(:telephony_adapter, type: :string) config.add(:test_ssn_allowed_list, type: :comma_separated_string_list) config.add(:totp_code_interval, type: :integer) diff --git a/spec/mailers/previews/report_mailer_preview.rb b/spec/mailers/previews/report_mailer_preview.rb new file mode 100644 index 00000000000..f3c71460244 --- /dev/null +++ b/spec/mailers/previews/report_mailer_preview.rb @@ -0,0 +1,10 @@ +class ReportMailerPreview < ActionMailer::Preview + def warn_error + ReportMailer.warn_error( + email: 'test@example.com', + error: ServiceProviderSeeder::ExtraServiceProviderError.new( + 'Extra service providers found in DB: a, b, c', + ), + ) + end +end diff --git a/spec/mailers/previews/report_mailer_preview_spec.rb b/spec/mailers/previews/report_mailer_preview_spec.rb new file mode 100644 index 00000000000..fb1fc38697f --- /dev/null +++ b/spec/mailers/previews/report_mailer_preview_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' +require_relative './report_mailer_preview' + +RSpec.describe ReportMailerPreview do + subject(:mailer_preview) { ReportMailerPreview.new } + + describe '#warn_error' do + it 'generates a warn_error email' do + expect { mailer_preview.warn_error }.to_not raise_error + end + end +end diff --git a/spec/mailers/report_mailer_spec.rb b/spec/mailers/report_mailer_spec.rb index d712bf3737f..3d16c583795 100644 --- a/spec/mailers/report_mailer_spec.rb +++ b/spec/mailers/report_mailer_spec.rb @@ -4,7 +4,7 @@ let(:user) { build(:user) } let(:email_address) { user.email_addresses.first } - describe 'deleted_user_accounts_report' do + describe '#deleted_user_accounts_report' do let(:mail) do ReportMailer.deleted_user_accounts_report( email: email_address.email, @@ -30,4 +30,26 @@ expect(mail.html_part.body).to have_content('issuer2') end end + + describe '#warn_error' do + let(:error) { RuntimeError.new('this is my test message') } + let(:env) { ActiveSupport::StringInquirer.new('prod') } + + let(:mail) do + ReportMailer.warn_error( + email: 'test@example.com', + error: error, + env: env, + ) + end + + it 'puts the rails env and error in a plaintext email', aggregate_failures: true do + expect(mail.html_part).to be_nil + + expect(mail.subject).to include('prod') + expect(mail.subject).to include('RuntimeError') + + expect(mail.text_part.body).to include('this is my test') + end + end end diff --git a/spec/services/service_provider_seeder_spec.rb b/spec/services/service_provider_seeder_spec.rb index ab1e7852f16..3151b056fd9 100644 --- a/spec/services/service_provider_seeder_spec.rb +++ b/spec/services/service_provider_seeder_spec.rb @@ -83,6 +83,10 @@ let(:prod_issuer) { 'urn:gov:login:test-providers:fake-prod-sp' } let(:unrestricted_issuer) { 'urn:gov:login:test-providers:fake-unrestricted-sp' } + before do + allow(IdentityConfig.store).to receive(:team_ursula_email).and_return('team@example.com') + end + context 'when %{env} is present in the config file' do let(:deploy_env) { 'dev' } @@ -106,12 +110,10 @@ expect(ServiceProvider.find_by(issuer: unrestricted_issuer)).not_to be_present end - it 'sends New Relic an error if the DB has an SP not in the config' do - allow(NewRelic::Agent).to receive(:notice_error) + it 'sends an email an error if the DB has an SP not in the config' do create(:service_provider, issuer: 'missing_issuer') - run - expect(NewRelic::Agent).to have_received(:notice_error) + expect { run }.to change { ActionMailer::Base.deliveries.count }.by(1) end end @@ -128,11 +130,9 @@ end it 'sends New Relic an error if the DB has an SP not in the config' do - allow(NewRelic::Agent).to receive(:notice_error) create(:service_provider, issuer: 'missing_issuer') - run - expect(NewRelic::Agent).to have_received(:notice_error) + expect { run }.to change { ActionMailer::Base.deliveries.count }.by(1) end end