diff --git a/app/models/user_badge.rb b/app/models/user_badge.rb index 5c6894d4d9a..f78ed824bb0 100644 --- a/app/models/user_badge.rb +++ b/app/models/user_badge.rb @@ -12,10 +12,12 @@ class UserBadge < ActiveRecord::Base after_create do Badge.increment_counter 'grant_count', self.badge_id + DiscourseEvent.trigger(:user_badge_granted, self.badge_id, self.user_id) end after_destroy do Badge.decrement_counter 'grant_count', self.badge_id + DiscourseEvent.trigger(:user_badge_removed, self.badge_id, self.user_id) end end diff --git a/spec/controllers/user_badges_controller_spec.rb b/spec/controllers/user_badges_controller_spec.rb index 1bfb3ad31e6..ac650b20fb6 100644 --- a/spec/controllers/user_badges_controller_spec.rb +++ b/spec/controllers/user_badges_controller_spec.rb @@ -5,7 +5,7 @@ describe UserBadgesController do let(:badge) { Fabricate(:badge) } context 'index' do - it 'doest not leak private info' do + it 'does not leak private info' do badge = Fabricate(:badge, target_posts: true, show_posts: false) p = create_post UserBadge.create(badge: badge, user: user, post_id: p.id, granted_by_id: -1, granted_at: Time.now) @@ -97,6 +97,19 @@ describe UserBadgesController do expect(user_badge).to be_present expect(user_badge.granted_by).to eq(Discourse.system_user) end + + it 'will trigger :user_badge_granted' do + log_in :admin + + # Make sure our extensibility points are triggered + # Stupid DiscourseEvent.clear doesn't work properly .. requires you to list ALL triggers in the chain! + # If there are future triggers added in the user creation chain, they need to be added anywhere you create a user and monitor ANY trigger. + # Perhaps DiscourseEvent needs a little fix so it doesn't break everything once you add a trigger in the chain. + DiscourseEvent.expects(:trigger).with(:user_created, anything).once + DiscourseEvent.expects(:trigger).with(:user_verified, anything).once + DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once + xhr :post, :create, badge_id: badge.id, username: user.username + end end context 'destroy' do @@ -114,5 +127,11 @@ describe UserBadgesController do expect(response.status).to eq(200) expect(UserBadge.find_by(id: user_badge.id)).to eq(nil) end + + it 'will trigger :user_badge_removed' do + log_in :admin + DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once + xhr :delete, :destroy, id: user_badge.id + end end end