2014-03-05 20:52:20 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BadgeGranter do
|
|
|
|
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
describe 'grant' do
|
|
|
|
|
|
|
|
it 'grants a badge' do
|
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
|
|
|
user_badge.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets granted_at' do
|
|
|
|
time = Time.zone.now
|
|
|
|
Timecop.freeze time
|
|
|
|
|
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
|
|
|
user_badge.granted_at.should eq(time)
|
|
|
|
|
|
|
|
Timecop.return
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets granted_by if the option is present' do
|
|
|
|
admin = Fabricate(:admin)
|
2014-03-20 03:30:12 +08:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
2014-03-05 20:52:20 +08:00
|
|
|
user_badge = BadgeGranter.grant(badge, user, granted_by: admin)
|
|
|
|
user_badge.granted_by.should eq(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults granted_by to the system user' do
|
2014-03-20 03:30:12 +08:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
2014-03-05 20:52:20 +08:00
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
|
|
|
user_badge.granted_by_id.should eq(Discourse.system_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow a regular user to grant badges' do
|
|
|
|
user_badge = BadgeGranter.grant(badge, user, granted_by: Fabricate(:user))
|
|
|
|
user_badge.should_not be_present
|
|
|
|
end
|
|
|
|
|
2014-04-17 03:59:45 +08:00
|
|
|
it 'increments grant_count on the badge and creates a notification' do
|
2014-03-05 20:52:20 +08:00
|
|
|
BadgeGranter.grant(badge, user)
|
|
|
|
badge.reload.grant_count.should eq(1)
|
2014-05-06 21:41:59 +08:00
|
|
|
user.notifications.find_by(notification_type: Notification.types[:granted_badge]).data_hash["badge_id"].should == badge.id
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'revoke' do
|
|
|
|
|
2014-03-20 03:30:12 +08:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2014-03-05 20:52:20 +08:00
|
|
|
let!(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
2014-04-18 11:10:53 +08:00
|
|
|
it 'revokes the badge and does necessary cleanup' do
|
|
|
|
user.title = badge.name; user.save!
|
2014-03-05 20:52:20 +08:00
|
|
|
badge.reload.grant_count.should eq(1)
|
2014-03-20 03:30:12 +08:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
|
|
|
|
BadgeGranter.revoke(user_badge, revoked_by: admin)
|
2014-05-06 21:41:59 +08:00
|
|
|
UserBadge.find_by(user: user, badge: badge).should_not be_present
|
2014-03-05 20:52:20 +08:00
|
|
|
badge.reload.grant_count.should eq(0)
|
2014-04-17 03:59:45 +08:00
|
|
|
user.notifications.where(notification_type: Notification.types[:granted_badge]).should be_empty
|
2014-04-18 11:10:53 +08:00
|
|
|
user.reload.title.should == nil
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|