2014-03-05 20:52:20 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe BadgeGranter do
|
|
|
|
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2014-08-13 10:25:56 +08:00
|
|
|
describe 'preview' do
|
|
|
|
it 'can correctly preview' do
|
|
|
|
Fabricate(:user, email: 'sam@gmail.com')
|
|
|
|
result = BadgeGranter.preview('select id user_id, null post_id, created_at granted_at from users where email like \'%gmail.com\'')
|
|
|
|
result[:grant_count].should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-03 15:29:44 +08: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))
|
2014-07-09 10:17:39 +08:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::FirstLike))
|
2014-07-03 15:29:44 +08:00
|
|
|
|
2014-07-08 12:26:53 +08:00
|
|
|
b = UserBadge.find_by(user_id: post.user_id)
|
2014-07-03 15:29:44 +08:00
|
|
|
b.post_id.should == post.id
|
2014-07-08 12:26:53 +08:00
|
|
|
b.badge_id = Badge::Welcome
|
|
|
|
|
|
|
|
b = UserBadge.find_by(user_id: user2.id)
|
|
|
|
b.post_id.should == post.id
|
2014-07-09 10:17:39 +08:00
|
|
|
b.badge_id = Badge::FirstLike
|
2014-07-03 15:29:44 +08:00
|
|
|
end
|
2014-05-05 02:15:38 +08:00
|
|
|
|
2014-07-01 20:00:31 +08:00
|
|
|
it 'should grant missing badges' do
|
|
|
|
post = Fabricate(:post, like_count: 30)
|
2014-07-05 16:32:06 +08:00
|
|
|
2.times {
|
2014-08-08 08:02:26 +08:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::NicePost), post_ids: [post.id])
|
2014-07-05 16:32:06 +08:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::GoodPost))
|
|
|
|
}
|
2014-07-01 20:00:31 +08:00
|
|
|
|
|
|
|
# TODO add welcome
|
|
|
|
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]
|
|
|
|
|
2014-07-07 15:55:25 +08:00
|
|
|
post.user.notifications.count.should == 2
|
|
|
|
|
2014-07-01 20:00:31 +08:00
|
|
|
Badge.find(Badge::NicePost).grant_count.should == 1
|
|
|
|
Badge.find(Badge::GoodPost).grant_count.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-05 20:52:20 +08:00
|
|
|
describe 'grant' do
|
|
|
|
|
2014-08-11 07:21:06 +08:00
|
|
|
it 'grants multiple badges' do
|
|
|
|
badge = Fabricate(:badge, multiple_grant: true)
|
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
2014-03-05 20:52:20 +08:00
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
|
|
|
user_badge.should be_present
|
2014-08-11 07:21:06 +08:00
|
|
|
|
|
|
|
UserBadge.where(user_id: user.id).count.should == 2
|
2014-03-05 20:52:20 +08:00
|
|
|
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
|
|
|
|
|
2014-05-16 01:43:04 +08:00
|
|
|
context "update_badges" do
|
2014-05-05 02:15:38 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2014-06-17 14:29:49 +08:00
|
|
|
let(:liker) { Fabricate(:user) }
|
2014-05-05 02:15:38 +08:00
|
|
|
|
2014-07-23 09:42:24 +08:00
|
|
|
before do
|
|
|
|
BadgeGranter.clear_queue!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants autobiographer" do
|
|
|
|
user.user_profile.bio_raw = "THIS IS MY bio it a long bio I like my bio"
|
|
|
|
user.uploaded_avatar_id = 10
|
|
|
|
user.user_profile.save
|
|
|
|
user.save
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge::Autobiographer).count.should eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants read guidlines" do
|
|
|
|
user.user_stat.read_faq = Time.now
|
|
|
|
user.user_stat.save
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge::ReadGuidelines).count.should eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants first link" do
|
|
|
|
post = create_post
|
|
|
|
post2 = create_post(raw: "#{Discourse.base_url}/t/slug/#{post.topic_id}")
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
UserBadge.where(user_id: post2.user.id, badge_id: Badge::FirstLink).count.should eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants first edit" do
|
|
|
|
SiteSetting.ninja_edit_window = 0
|
|
|
|
post = create_post
|
|
|
|
user = post.user
|
|
|
|
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge::Editor).count.should eq(0)
|
|
|
|
|
|
|
|
PostRevisor.new(post).revise!(user, "This is my new test 1235 123")
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
|
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge::Editor).count.should eq(1)
|
|
|
|
end
|
|
|
|
|
2014-05-16 01:43:04 +08:00
|
|
|
it "grants and revokes trust level badges" do
|
2014-09-05 13:20:39 +08:00
|
|
|
user.change_trust_level!(TrustLevel[4])
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.process_queue!
|
2014-05-05 02:15:38 +08:00
|
|
|
UserBadge.where(user_id: user.id, badge_id: Badge.trust_level_badge_ids).count.should eq(4)
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
user.change_trust_level!(TrustLevel[1])
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.backfill(Badge.find(1))
|
|
|
|
BadgeGranter.backfill(Badge.find(2))
|
2014-05-05 02:15:38 +08: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 14:29:49 +08:00
|
|
|
|
|
|
|
it "grants system like badges" do
|
|
|
|
post = create_post(user: user)
|
|
|
|
# Welcome badge
|
2014-07-23 09:42:24 +08:00
|
|
|
action = PostAction.act(liker, post, PostActionType.types[:like])
|
|
|
|
BadgeGranter.process_queue!
|
2014-06-17 14:29:49 +08:00
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 5).should_not be_nil
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2014-06-17 14:29:49 +08:00
|
|
|
# Nice post badge
|
|
|
|
post.update_attributes like_count: 10
|
2014-07-23 09:42:24 +08:00
|
|
|
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
|
2014-06-17 14:29:49 +08:00
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 6).should_not be_nil
|
2014-06-28 03:02:09 +08:00
|
|
|
UserBadge.where(user_id: user.id, badge_id: 6).count.should == 1
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2014-06-17 14:29:49 +08:00
|
|
|
# Good post badge
|
|
|
|
post.update_attributes like_count: 25
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
2014-06-17 14:29:49 +08:00
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 7).should_not be_nil
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2014-06-17 14:29:49 +08:00
|
|
|
# Great post badge
|
2014-07-03 15:29:44 +08:00
|
|
|
post.update_attributes like_count: 50
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
2014-06-17 14:29:49 +08:00
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: 8).should_not be_nil
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2014-06-17 14:29:49 +08:00
|
|
|
# Revoke badges on unlike
|
2014-07-03 15:29:44 +08:00
|
|
|
post.update_attributes like_count: 49
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::GreatPost))
|
|
|
|
UserBadge.find_by(user_id: user.id, badge_id: Badge::GreatPost).should be_nil
|
2014-06-17 14:29:49 +08:00
|
|
|
end
|
2014-05-05 02:15:38 +08:00
|
|
|
end
|
|
|
|
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|