FIX: Check for user presence before granting badge (#11745)

This commit is contained in:
Penar Musaraj 2021-01-18 15:12:38 -05:00 committed by GitHub
parent 670b438ca5
commit b547b8415d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -45,6 +45,7 @@ class BadgeGranter
def grant def grant
return if @granted_by && !Guardian.new(@granted_by).can_grant_badges?(@user) return if @granted_by && !Guardian.new(@granted_by).can_grant_badges?(@user)
return unless @badge.present? && @badge.enabled? 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 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 } find_by = { badge_id: @badge.id, user_id: @user.id }

View File

@ -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) expect(user.notifications.find_by(notification_type: Notification.types[:granted_badge]).data_hash["badge_id"]).to eq(badge.id)
end end
it 'does not fail when user is missing' do
BadgeGranter.grant(badge, nil)
expect(badge.reload.grant_count).to eq(0)
end
end end
describe 'revoke' do describe 'revoke' do