diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 127eca9d54c..09c6f1dc790 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -44,7 +44,7 @@ class BadgeGranter def grant return if @granted_by && !Guardian.new(@granted_by).can_grant_badges?(@user) - return unless @badge.enabled? + return unless @badge.present? && @badge.enabled? return if @badge.badge_grouping_id == BadgeGrouping::GettingStarted && @badge.id != Badge::NewUserOfTheMonth && @user.user_option.skip_new_user_tips find_by = { badge_id: @badge.id, user_id: @user.id } diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index 310a9932611..1ca7b4d7970 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -208,6 +208,12 @@ describe BadgeGranter do expect(Notification.where(user_id: user.id).count).to eq(0) end + it "handles deleted badge" do + freeze_time + user_badge = BadgeGranter.grant(nil, user, created_at: 1.year.ago) + expect(user_badge).to eq(nil) + end + it "doesn't grant disabled badges" do freeze_time badge = Fabricate(:badge, badge_type_id: BadgeType::Bronze, enabled: false)