From 73068d5fa3ba39736d0591c8e92c25dc49545a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigur=C3=B0ur=20Gu=C3=B0brandsson?= Date: Thu, 26 Feb 2015 00:55:17 +0000 Subject: [PATCH] ADD: Spec tests for User Badge triggers NOTE: The DiscourseEvent trigger mechanism is VERY weird. If there are ANY triggers triggered in the chain, you can't only list the one you're looking for, you have to list all triggers in the order they will come. Example: line 98-100 :user_created and :user_verified are triggers that are introduced in PR #3237 so if this PR is accepted but not PR #3237 then lines 98-99 need to be removed. --- .../user_badges_controller_spec.rb | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/spec/controllers/user_badges_controller_spec.rb b/spec/controllers/user_badges_controller_spec.rb index 1bfb3ad31e6..84225b39c64 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) @@ -63,23 +63,13 @@ describe UserBadgesController do it 'grants badges from staff' do admin = Fabricate(:admin) - post = create_post - log_in_user admin - StaffActionLogger.any_instance.expects(:log_badge_grant).once - - xhr :post, :create, badge_id: badge.id, - username: user.username, - reason: Discourse.base_url + post.url - + xhr :post, :create, badge_id: badge.id, username: user.username expect(response.status).to eq(200) - user_badge = UserBadge.find_by(user: user, badge: badge) - expect(user_badge).to be_present expect(user_badge.granted_by).to eq(admin) - expect(user_badge.post_id).to eq(post.id) end it 'does not grant badges from regular api calls' do @@ -97,6 +87,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 +117,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