Merge pull request #3236 from sigginet/patch-1

FIX: Added two user badge triggers
This commit is contained in:
Robin Ward 2015-07-07 11:25:11 -04:00
commit 2c14acac72
2 changed files with 22 additions and 1 deletions

View File

@ -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

View File

@ -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