FIX: Don't ever grant badges when they're disabled

This commit is contained in:
Robin Ward 2016-08-19 15:16:37 -04:00
parent b4b96bf62b
commit 4061725a95
3 changed files with 16 additions and 1 deletions

View File

@ -12,6 +12,7 @@ class BadgeGranter
def grant
return if @granted_by and !Guardian.new(@granted_by).can_grant_badges?(@user)
return unless @badge.enabled?
find_by = { badge_id: @badge.id, user_id: @user.id }

View File

@ -604,11 +604,18 @@ describe CookedPostProcessor do
before { Oneboxer.stubs(:onebox) }
it "awards a badge for using an emoji" do
it "awards a badge for using an onebox" do
cpp.post_process_oneboxes
cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(true)
end
it "doesn't award the badge when the badge is disabled" do
Badge.where(id: Badge::FirstOnebox).update_all(enabled: false)
cpp.post_process_oneboxes
cpp.grant_badges
expect(post.user.user_badges.where(badge_id: Badge::FirstOnebox).exists?).to eq(false)
end
end
context "reply_by_email" do

View File

@ -106,7 +106,14 @@ describe BadgeGranter do
expect(user_badge.granted_at).to eq(time)
expect(Notification.where(user_id: user.id).count).to eq(0)
end
it "doesn't grant disabled badges" do
badge = Fabricate(:badge, badge_type_id: BadgeType::Bronze, enabled: false)
time = 1.year.ago
user_badge = BadgeGranter.grant(badge, user, created_at: time)
expect(user_badge).to eq(nil)
end
it 'grants multiple badges' do