diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a2548ff1ac..437b4549e9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,6 +13,8 @@ class ApplicationController < ActionController::Base after_action :verify_authorized, except: :index, unless: :devise_controller? # after_action :verify_policy_scoped, only: :index + KNOWN_ERRORS = [Pundit::NotAuthorizedError, Organizational::UnknownOrganization] + rescue_from StandardError, with: :log_and_reraise rescue_from Pundit::NotAuthorizedError, with: :not_authorized rescue_from Organizational::UnknownOrganization, with: :not_authorized @@ -136,6 +138,13 @@ def not_authorized redirect_to(root_url) end + def log_and_reraise(error) + unless KNOWN_ERRORS.include?(error.class) + Bugsnag.notify(error) + end + raise + end + def check_unconfirmed_email_notice(user) notice = "#{user.role} was successfully updated." if user.saved_changes.include?("unconfirmed_email") diff --git a/spec/lib/tasks/volunteer_birthday_reminder_spec.rb b/spec/lib/tasks/volunteer_birthday_reminder_spec.rb index 9e7113c83a..d14c8c8282 100644 --- a/spec/lib/tasks/volunteer_birthday_reminder_spec.rb +++ b/spec/lib/tasks/volunteer_birthday_reminder_spec.rb @@ -4,12 +4,17 @@ RSpec.describe "lib/tasks/volunteer_birthday_reminder.rake" do let(:rake_task) { Rake::Task["volunteer_birthday_reminder"].invoke } + let(:now) { Date.new(2022, 10, 15) } + let(:this_month) { now.month } + let(:this_month_15th) { Date.new(now.year, now.month, 15) } + let(:next_month) { Date.new(1988, this_month + 1, 1) } + let(:not_next_month) { Date.new(1998, this_month - 1, 1) } before do Rake::Task.clear Casa::Application.load_tasks - travel_to Date.new(2022, 10, 15) + travel_to now end after do @@ -18,7 +23,7 @@ context "there is a volunteer with a birthday next month" do let!(:volunteer) do - create(:volunteer, :with_assigned_supervisor, date_of_birth: Date.new(1988, 11, 30)) + create(:volunteer, :with_assigned_supervisor, date_of_birth: next_month) end it "creates a notification" do @@ -29,7 +34,7 @@ context "there are many volunteers with birthdays next month" do volunteer_count = 10 let!(:volunteer) do - create_list(:volunteer, volunteer_count, :with_assigned_supervisor, date_of_birth: Date.new(1988, 11, 30)) + create_list(:volunteer, volunteer_count, :with_assigned_supervisor, date_of_birth: next_month) end it "creates many notifications" do @@ -39,7 +44,7 @@ context "there is an unsupervised volunteer with a birthday next month" do let!(:volunteer) do - create(:volunteer, date_of_birth: Date.new(1988, 11, 30)) + create(:volunteer, date_of_birth: next_month) end it "does not create a notification" do @@ -59,7 +64,7 @@ context "there is a volunteer with a birthday that is not next month" do let!(:volunteer) do - create(:volunteer, :with_assigned_supervisor, date_of_birth: Date.new(1998, 7, 16)) + create(:volunteer, :with_assigned_supervisor, date_of_birth: not_next_month) end it "does not create a notification" do @@ -68,10 +73,10 @@ end context "when today is the 15th" do - before { travel_to(Date.new(2022, 10, 15)) } + before { travel_to(this_month_15th) } let!(:volunteer) do - create(:volunteer, :with_assigned_supervisor, date_of_birth: Date.new(1988, 11, 30)) + create(:volunteer, :with_assigned_supervisor, date_of_birth: next_month) end it "runs the rake task" do @@ -80,7 +85,7 @@ end context "when today is not the 15th" do - before { travel_to(Date.new(2022, 10, 1)) } + before { travel_to(this_month_15th + 2.days) } it "skips the rake task" do expect { rake_task }.to change { Notification.count }.by(0)