2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
describe BadgeGranter do
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-10-07 19:26:18 -04:00
|
|
|
describe 'revoke_titles' do
|
|
|
|
it 'can correctly revoke titles' do
|
|
|
|
badge = Fabricate(:badge, allow_title: true)
|
|
|
|
user = Fabricate(:user, title: badge.name)
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
user.user_profile.update_column(:badge_granted_title, true)
|
|
|
|
|
2014-10-09 07:27:34 -04:00
|
|
|
BadgeGranter.grant(badge, user)
|
2014-10-07 19:26:18 -04:00
|
|
|
BadgeGranter.revoke_ungranted_titles!
|
|
|
|
|
|
|
|
user.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.title).to eq(badge.name)
|
2014-10-07 19:26:18 -04:00
|
|
|
|
|
|
|
badge.update_column(:allow_title, false)
|
|
|
|
BadgeGranter.revoke_ungranted_titles!
|
|
|
|
|
|
|
|
user.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.title).to eq('')
|
2014-10-07 19:26:18 -04:00
|
|
|
|
|
|
|
user.title = "CEO"
|
|
|
|
user.save
|
|
|
|
|
|
|
|
BadgeGranter.revoke_ungranted_titles!
|
|
|
|
|
|
|
|
user.reload
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.title).to eq("CEO")
|
2014-10-07 19:26:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-12 22:25:56 -04:00
|
|
|
describe 'preview' do
|
|
|
|
it 'can correctly preview' do
|
|
|
|
Fabricate(:user, email: 'sam@gmail.com')
|
2017-04-26 14:47:36 -04:00
|
|
|
result = BadgeGranter.preview('select u.id user_id, null post_id, u.created_at granted_at from users u
|
|
|
|
join user_emails ue on ue.user_id = u.id AND ue.primary
|
|
|
|
where ue.email like \'%gmail.com\'', explain: true)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(result[:grant_count]).to eq(1)
|
2015-05-28 02:06:22 -04:00
|
|
|
expect(result[:query_plan]).to be_present
|
2014-08-12 22:25:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.like(user2, post)
|
2014-07-03 03:29:44 -04:00
|
|
|
|
|
|
|
UserBadge.destroy_all
|
|
|
|
BadgeGranter.backfill(Badge.find(Badge::Welcome))
|
2014-07-08 22:17:39 -04:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::FirstLike))
|
2014-07-03 03:29:44 -04:00
|
|
|
|
2014-07-08 00:26:53 -04:00
|
|
|
b = UserBadge.find_by(user_id: post.user_id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(b.post_id).to eq(post.id)
|
2014-07-08 00:26:53 -04:00
|
|
|
b.badge_id = Badge::Welcome
|
|
|
|
|
|
|
|
b = UserBadge.find_by(user_id: user2.id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(b.post_id).to eq(post.id)
|
2014-07-08 22:17:39 -04:00
|
|
|
b.badge_id = Badge::FirstLike
|
2014-07-03 03:29:44 -04:00
|
|
|
end
|
2014-05-04 14:15:38 -04:00
|
|
|
|
2014-07-01 08:00:31 -04:00
|
|
|
it 'should grant missing badges' do
|
2019-01-04 09:17:54 -05:00
|
|
|
nice_topic = Badge.find(Badge::NiceTopic)
|
2016-02-01 02:26:45 -05:00
|
|
|
good_topic = Badge.find(Badge::GoodTopic)
|
|
|
|
|
2014-07-01 08:00:31 -04:00
|
|
|
post = Fabricate(:post, like_count: 30)
|
2019-01-04 09:17:54 -05:00
|
|
|
|
2014-07-05 04:32:06 -04:00
|
|
|
2.times {
|
2019-01-04 09:17:54 -05:00
|
|
|
BadgeGranter.backfill(nice_topic, post_ids: [post.id])
|
2016-02-01 02:26:45 -05:00
|
|
|
BadgeGranter.backfill(good_topic)
|
2014-07-05 04:32:06 -04:00
|
|
|
}
|
2014-07-01 08:00:31 -04:00
|
|
|
|
|
|
|
# TODO add welcome
|
2019-01-04 09:17:54 -05:00
|
|
|
expect(post.user.user_badges.pluck(:badge_id)).to contain_exactly(nice_topic.id, good_topic.id)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(post.user.notifications.count).to eq(2)
|
2014-07-07 03:55:25 -04:00
|
|
|
|
2019-01-04 09:17:54 -05:00
|
|
|
data = post.user.notifications.last.data_hash
|
2016-02-01 02:26:45 -05:00
|
|
|
expect(data["badge_id"]).to eq(good_topic.id)
|
|
|
|
expect(data["badge_slug"]).to eq(good_topic.slug)
|
|
|
|
expect(data["username"]).to eq(post.user.username)
|
|
|
|
|
2019-01-04 09:17:54 -05:00
|
|
|
expect(nice_topic.grant_count).to eq(1)
|
|
|
|
expect(good_topic.grant_count).to eq(1)
|
2014-07-01 08:00:31 -04:00
|
|
|
end
|
2016-10-11 18:15:36 -04:00
|
|
|
|
|
|
|
it 'should grant badges in the user locale' do
|
|
|
|
SiteSetting.allow_user_locale = true
|
|
|
|
|
|
|
|
nice_topic = Badge.find(Badge::NiceTopic)
|
|
|
|
name_english = nice_topic.name
|
|
|
|
|
|
|
|
user = Fabricate(:user, locale: 'fr')
|
|
|
|
post = Fabricate(:post, like_count: 10, user: user)
|
|
|
|
|
|
|
|
BadgeGranter.backfill(nice_topic)
|
|
|
|
|
|
|
|
notification_badge_name = JSON.parse(post.user.notifications.first.data)['badge_name']
|
|
|
|
|
|
|
|
expect(notification_badge_name).not_to eq(name_english)
|
|
|
|
end
|
2014-07-01 08:00:31 -04:00
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
describe 'grant' do
|
|
|
|
|
2016-04-12 08:08:38 -04:00
|
|
|
it 'allows overriding of granted_at does not notify old bronze' do
|
2018-06-05 03:29:17 -04:00
|
|
|
badge = Badge.create!(name: 'a badge', badge_type_id: BadgeType::Bronze)
|
|
|
|
|
2016-04-12 08:08:38 -04:00
|
|
|
time = 1.year.ago
|
|
|
|
|
|
|
|
user_badge = BadgeGranter.grant(badge, user, created_at: time)
|
|
|
|
|
|
|
|
expect(user_badge.granted_at).to eq(time)
|
|
|
|
expect(Notification.where(user_id: user.id).count).to eq(0)
|
2016-08-19 15:16:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't grant disabled badges" do
|
|
|
|
badge = Fabricate(:badge, badge_type_id: BadgeType::Bronze, enabled: false)
|
|
|
|
time = 1.year.ago
|
2016-04-12 08:08:38 -04:00
|
|
|
|
2016-08-19 15:16:37 -04:00
|
|
|
user_badge = BadgeGranter.grant(badge, user, created_at: time)
|
|
|
|
expect(user_badge).to eq(nil)
|
2016-04-12 08:08:38 -04:00
|
|
|
end
|
|
|
|
|
2014-08-10 19:21:06 -04:00
|
|
|
it 'grants multiple badges' do
|
|
|
|
badge = Fabricate(:badge, multiple_grant: true)
|
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
2014-03-05 07:52:20 -05:00
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user_badge).to be_present
|
2014-08-10 19:21:06 -04:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id).count).to eq(2)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets granted_at' do
|
2017-07-24 09:17:42 -04:00
|
|
|
time = 1.day.ago
|
|
|
|
freeze_time time
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(user_badge.granted_at).to be_within(1.second).of(time)
|
2014-03-05 07:52:20 -05:00
|
|
|
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)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user_badge.granted_by).to eq(admin)
|
2014-03-05 07:52:20 -05:00
|
|
|
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)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user_badge.granted_by_id).to eq(Discourse.system_user.id)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow a regular user to grant badges' do
|
|
|
|
user_badge = BadgeGranter.grant(badge, user, granted_by: Fabricate(:user))
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user_badge).not_to be_present
|
2014-03-05 07:52:20 -05:00
|
|
|
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)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(badge.reload.grant_count).to eq(1)
|
|
|
|
expect(user.notifications.find_by(notification_type: Notification.types[:granted_badge]).data_hash["badge_id"]).to eq(badge.id)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'revoke' do
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(: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!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(badge.reload.grant_count).to 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)
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user: user, badge: badge)).not_to be_present
|
|
|
|
expect(badge.reload.grant_count).to eq(0)
|
|
|
|
expect(user.notifications.where(notification_type: Notification.types[:granted_badge])).to be_empty
|
|
|
|
expect(user.reload.title).to eq(nil)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-05-15 13:43:04 -04:00
|
|
|
context "update_badges" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:liker) { Fabricate(:user) }
|
2014-05-04 14:15:38 -04:00
|
|
|
|
2014-07-22 21:42:24 -04:00
|
|
|
before do
|
|
|
|
BadgeGranter.clear_queue!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants autobiographer" do
|
2017-07-27 21:20:09 -04:00
|
|
|
user.user_profile.bio_raw = "THIS IS MY bio it a long bio I like my bio"
|
2014-07-22 21:42:24 -04:00
|
|
|
user.uploaded_avatar_id = 10
|
|
|
|
user.user_profile.save
|
|
|
|
user.save
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge::Autobiographer).count).to eq(1)
|
2014-07-22 21:42:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "grants read guidlines" do
|
|
|
|
user.user_stat.read_faq = Time.now
|
|
|
|
user.user_stat.save
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge::ReadGuidelines).count).to eq(1)
|
2014-07-22 21:42:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "grants first link" do
|
|
|
|
post = create_post
|
|
|
|
post2 = create_post(raw: "#{Discourse.base_url}/t/slug/#{post.topic_id}")
|
|
|
|
|
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: post2.user.id, badge_id: Badge::FirstLink).count).to eq(1)
|
2014-07-22 21:42:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "grants first edit" do
|
2015-11-24 14:28:42 -05:00
|
|
|
SiteSetting.editing_grace_period = 0
|
2014-07-22 21:42:24 -04:00
|
|
|
post = create_post
|
|
|
|
user = post.user
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge::Editor).count).to eq(0)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
PostRevisor.new(post).revise!(user, raw: "This is my new test 1235 123")
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.process_queue!
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge::Editor).count).to eq(1)
|
2014-07-22 21:42:24 -04:00
|
|
|
end
|
|
|
|
|
2014-05-15 13:43:04 -04:00
|
|
|
it "grants and revokes trust level badges" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.change_trust_level!(TrustLevel[4])
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge.trust_level_badge_ids).count).to eq(4)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
user.change_trust_level!(TrustLevel[1])
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.backfill(Badge.find(1))
|
|
|
|
BadgeGranter.backfill(Badge.find(2))
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: 1).first).not_to eq(nil)
|
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: 2).first).to eq(nil)
|
2014-05-04 14:15:38 -04:00
|
|
|
end
|
2014-06-17 02:29:49 -04:00
|
|
|
|
|
|
|
it "grants system like badges" do
|
|
|
|
post = create_post(user: user)
|
|
|
|
# Welcome badge
|
2019-01-03 12:03:01 -05:00
|
|
|
action = PostActionCreator.like(liker, post).post_action
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user_id: user.id, badge_id: 5)).not_to eq(nil)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2014-09-10 22:36:37 -04:00
|
|
|
post = create_post(topic: post.topic, user: user)
|
2019-01-03 12:03:01 -05:00
|
|
|
action = PostActionCreator.like(liker, post).post_action
|
2014-09-10 22:36:37 -04:00
|
|
|
|
2014-06-17 02:29:49 -04:00
|
|
|
# Nice post badge
|
2019-04-29 03:32:25 -04:00
|
|
|
post.update like_count: 10
|
2014-07-22 21:42:24 -04:00
|
|
|
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user_id: user.id, badge_id: Badge::NicePost)).not_to eq(nil)
|
|
|
|
expect(UserBadge.where(user_id: user.id, badge_id: Badge::NicePost).count).to eq(1)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2014-06-17 02:29:49 -04:00
|
|
|
# Good post badge
|
2019-04-29 03:32:25 -04:00
|
|
|
post.update like_count: 25
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user_id: user.id, badge_id: Badge::GoodPost)).not_to eq(nil)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2014-06-17 02:29:49 -04:00
|
|
|
# Great post badge
|
2019-04-29 03:32:25 -04:00
|
|
|
post.update like_count: 50
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: action)
|
|
|
|
BadgeGranter.process_queue!
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user_id: user.id, badge_id: Badge::GreatPost)).not_to eq(nil)
|
2014-07-22 21:42:24 -04:00
|
|
|
|
2014-06-17 02:29:49 -04:00
|
|
|
# Revoke badges on unlike
|
2019-04-29 03:32:25 -04:00
|
|
|
post.update like_count: 49
|
2014-07-22 21:42:24 -04:00
|
|
|
BadgeGranter.backfill(Badge.find(Badge::GreatPost))
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(UserBadge.find_by(user_id: user.id, badge_id: Badge::GreatPost)).to eq(nil)
|
2014-06-17 02:29:49 -04:00
|
|
|
end
|
2014-05-04 14:15:38 -04:00
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|