2014-03-05 07:52:20 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BadgeGranter do
|
|
|
|
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2014-07-03 03:29:44 -04:00
|
|
|
describe 'backfill' do
|
|
|
|
|
|
|
|
it 'has no broken badge queries' do
|
|
|
|
Badge.all.each do |b|
|
|
|
|
BadgeGranter.backfill(b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can backfill the welcome badge' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
PostAction.act(user2, post, PostActionType.types[:like])
|
|
|
|
|
|
|
|
UserBadge.destroy_all
|
|
|
|
BadgeGranter.backfill(Badge.find(Badge::Welcome))
|
|
|
|
|
|
|
|
b = UserBadge.first
|
|
|
|
b.user_id.should == post.user_id
|
|
|
|
b.post_id.should == post.id
|
|
|
|
end
|
2014-05-04 14:15:38 -04:00
|
|
|
|
2014-07-01 08:00:31 -04:00
|
|
|
it 'should grant missing badges' do
|
|
|
|
post = Fabricate(:post, like_count: 30)
|
2014-07-05 04:32:06 -04:00
|
|
|
2.times {
|
|
|
|
BadgeGranter.backfill(Badge.find(Badge::NicePost))
|
|
|
|
BadgeGranter.backfill(Badge.find(Badge::GoodPost))
|
|
|
|
}
|
2014-07-01 08:00:31 -04:00
|
|
|
|
|
|
|
# TODO add welcome
|
|
|
|
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]
|
|
|
|
|
|
|
|
Badge.find(Badge::NicePost).grant_count.should == 1
|
|
|
|
Badge.find(Badge::GoodPost).grant_count.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-03 03:29:44 -04:00
|
|
|
describe 'autobiographer' do
|
|
|
|
it 'grants autobiographer correctly' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_profile.bio_raw = "I filled my bio"
|
|
|
|
user.user_profile.save!
|
|
|
|
|
|
|
|
Badge.find(Badge::Autobiographer).grant_count.should == 0
|
|
|
|
|
|
|
|
user.uploaded_avatar_id = 100
|
|
|
|
user.save
|
|
|
|
|
|
|
|
Badge.find(Badge::Autobiographer).grant_count.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
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-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
2014-03-05 07:52:20 -05: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-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
2014-03-05 07:52:20 -05: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-16 15:59:45 -04:00
|
|
|
it 'increments grant_count on the badge and creates a notification' do
|
2014-03-05 07:52:20 -05:00
|
|
|
BadgeGranter.grant(badge, user)
|
|
|
|
badge.reload.grant_count.should eq(1)
|
2014-05-06 09:41:59 -04:00
|
|
|
user.notifications.find_by(notification_type: Notification.types[:granted_badge]).data_hash["badge_id"].should == badge.id
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'revoke' do
|
|
|
|
|
2014-03-19 15:30:12 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
2014-03-05 07:52:20 -05:00
|
|
|
let!(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
2014-04-17 23:10:53 -04:00
|
|
|
it 'revokes the badge and does necessary cleanup' do
|
|
|
|
user.title = badge.name; user.save!
|
2014-03-05 07:52:20 -05:00
|
|
|
badge.reload.grant_count.should eq(1)
|
2014-03-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
|
|
|
|
BadgeGranter.revoke(user_badge, revoked_by: admin)
|
2014-05-06 09:41:59 -04:00
|
|
|
UserBadge.find_by(user: user, badge: badge).should_not be_present
|
2014-03-05 07:52:20 -05:00
|
|
|
badge.reload.grant_count.should eq(0)
|
2014-04-16 15:59:45 -04:00
|
|
|
user.notifications.where(notification_type: Notification.types[:granted_badge]).should be_empty
|
2014-04-17 23:10:53 -04:00
|
|
|
user.reload.title.should == nil
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-05-15 13:43:04 -04:00
|
|
|
context "update_badges" do
|
2014-05-04 14:15:38 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-06-17 02:29:49 -04:00
|
|
|
let(:liker) { Fabricate(:user) }
|
2014-05-04 14:15:38 -04:00
|
|
|
|
2014-05-15 13:43:04 -04:00
|
|
|
it "grants and revokes trust level badges" do
|
2014-05-04 14:15:38 -04:00
|
|
|
user.change_trust_level!(:elder)
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge.trust_level_badge_ids).count.should eq(4)
|
2014-06-16 20:46:30 -04:00
|
|
|
user.change_trust_level!(:basic)
|
2014-05-04 14:15:38 -04:00
|
|
|
UserBadge.where(user_id: user.id, badge_id: 1).first.should_not be_nil
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: 2).first.should be_nil
|
|
|
|
end
|
2014-06-17 02:29:49 -04:00
|
|
|
|
|
|
|
it "grants system like badges" do
|
|
|
|
post = create_post(user: user)
|
|
|
|
# Welcome badge
|
|
|
|
PostAction.act(liker, post, PostActionType.types[:like])
|
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 5).should_not be_nil
|
|
|
|
# Nice post badge
|
|
|
|
post.update_attributes like_count: 10
|
|
|
|
BadgeGranter.update_badges(action: :post_like, post_id: post.id)
|
2014-06-27 15:02:09 -04:00
|
|
|
BadgeGranter.update_badges(action: :post_like, post_id: post.id)
|
2014-06-17 02:29:49 -04:00
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 6).should_not be_nil
|
2014-06-27 15:02:09 -04:00
|
|
|
UserBadge.where(user_id: user.id, badge_id: 6).count.should == 1
|
2014-06-17 02:29:49 -04:00
|
|
|
# Good post badge
|
|
|
|
post.update_attributes like_count: 25
|
|
|
|
BadgeGranter.update_badges(action: :post_like, post_id: post.id)
|
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 7).should_not be_nil
|
|
|
|
# Great post badge
|
2014-07-03 03:29:44 -04:00
|
|
|
post.update_attributes like_count: 50
|
2014-06-17 02:29:49 -04:00
|
|
|
BadgeGranter.update_badges(action: :post_like, post_id: post.id)
|
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 8).should_not be_nil
|
|
|
|
# Revoke badges on unlike
|
2014-07-03 03:29:44 -04:00
|
|
|
post.update_attributes like_count: 49
|
2014-06-17 02:29:49 -04:00
|
|
|
BadgeGranter.update_badges(action: :post_like, post_id: post.id)
|
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 8).should be_nil
|
|
|
|
end
|
2014-05-04 14:15:38 -04:00
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|