From b547b8415dccfa7dc803c8809bf9d4ae2ce13e43 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Mon, 18 Jan 2021 15:12:38 -0500 Subject: [PATCH] FIX: Check for user presence before granting badge (#11745) --- app/services/badge_granter.rb | 1 + spec/services/badge_granter_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 09c6f1dc790..f3bd9bb0f57 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -45,6 +45,7 @@ class BadgeGranter def grant return if @granted_by && !Guardian.new(@granted_by).can_grant_badges?(@user) return unless @badge.present? && @badge.enabled? + return if @user.blank? 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 1ca7b4d7970..05f82abcc89 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -280,6 +280,11 @@ describe BadgeGranter do expect(user.notifications.find_by(notification_type: Notification.types[:granted_badge]).data_hash["badge_id"]).to eq(badge.id) end + it 'does not fail when user is missing' do + BadgeGranter.grant(badge, nil) + expect(badge.reload.grant_count).to eq(0) + end + end describe 'revoke' do