From 4061725a95f5013d0701e2c5ed6dc79f78e14dae Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 19 Aug 2016 15:16:37 -0400 Subject: [PATCH] FIX: Don't ever grant badges when they're disabled --- app/services/badge_granter.rb | 1 + spec/components/cooked_post_processor_spec.rb | 9 ++++++++- spec/services/badge_granter_spec.rb | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 56186e696b3..da74a099746 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -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 } diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index b430b0a9737..cdf5431bf79 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -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 diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index 6111be99a1c..a7ce5e40df0 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -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