2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-06-27 12:26:03 -04:00
|
|
|
|
2014-09-27 19:38:00 -04:00
|
|
|
describe Jobs::Tl3Promotions do
|
2014-06-27 12:26:03 -04:00
|
|
|
|
2018-02-05 17:54:07 -05:00
|
|
|
def create_qualifying_stats(user)
|
|
|
|
user.create_user_stat if user.user_stat.nil?
|
2019-04-29 03:32:25 -04:00
|
|
|
user.user_stat.update!(
|
2018-02-05 17:54:07 -05:00
|
|
|
days_visited: 1000,
|
|
|
|
topics_entered: 1000,
|
|
|
|
posts_read_count: 1000,
|
|
|
|
likes_given: 1000,
|
|
|
|
likes_received: 1000
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
subject(:run_job) { described_class.new.execute({}) }
|
|
|
|
|
|
|
|
it "promotes tl2 user who qualifies for tl3" do
|
2018-08-24 18:41:03 -04:00
|
|
|
tl2_user = Fabricate(:user, trust_level: TrustLevel[2])
|
|
|
|
create_qualifying_stats(tl2_user)
|
2014-09-05 01:20:39 -04:00
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(true)
|
|
|
|
Promotion.any_instance.expects(:change_trust_level!).with(TrustLevel[3], anything).once
|
2014-06-27 12:26:03 -04:00
|
|
|
run_job
|
|
|
|
end
|
|
|
|
|
2018-08-24 18:41:03 -04:00
|
|
|
it "promotes a qualifying tl2 user who has a group_granted_trust_level" do
|
|
|
|
group = Fabricate(:group, grant_trust_level: 1)
|
|
|
|
group_locked_user = Fabricate(:user, trust_level: TrustLevel[2])
|
|
|
|
group.add(group_locked_user)
|
|
|
|
|
|
|
|
create_qualifying_stats(group_locked_user)
|
2018-08-09 20:42:23 -04:00
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(true)
|
|
|
|
Promotion.any_instance.expects(:change_trust_level!).with(TrustLevel[3], anything).once
|
|
|
|
run_job
|
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
it "doesn't promote tl1 and tl0 users who have met tl3 requirements" do
|
2018-08-24 18:41:03 -04:00
|
|
|
tl1_user = Fabricate(:user, trust_level: TrustLevel[1])
|
|
|
|
tl0_user = Fabricate(:user, trust_level: TrustLevel[0])
|
|
|
|
create_qualifying_stats(tl1_user)
|
|
|
|
create_qualifying_stats(tl0_user)
|
2014-09-05 01:20:39 -04:00
|
|
|
TrustLevel3Requirements.any_instance.expects(:requirements_met?).never
|
2014-06-27 12:26:03 -04:00
|
|
|
Promotion.any_instance.expects(:change_trust_level!).never
|
|
|
|
run_job
|
|
|
|
end
|
|
|
|
|
2014-07-08 17:39:36 -04:00
|
|
|
context "tl3 user who doesn't qualify for tl3 anymore" do
|
|
|
|
def create_leader_user
|
2014-09-05 01:20:39 -04:00
|
|
|
user = Fabricate(:user, trust_level: TrustLevel[2])
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(true)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Promotion.new(user).review_tl2).to eq(true)
|
2014-07-08 17:39:36 -04:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2014-09-05 02:55:25 -04:00
|
|
|
SiteSetting.tl3_promotion_min_duration = 3
|
2014-07-08 17:39:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "demotes if was promoted more than X days ago" do
|
|
|
|
user = nil
|
2017-07-24 09:17:42 -04:00
|
|
|
|
|
|
|
freeze_time 4.days.ago do
|
2014-07-08 17:39:36 -04:00
|
|
|
user = create_leader_user
|
|
|
|
end
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(true)
|
2014-07-08 17:39:36 -04:00
|
|
|
run_job
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[2])
|
2014-07-08 17:39:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't demote if user was promoted recently" do
|
|
|
|
user = nil
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time 1.day.ago do
|
2014-07-08 17:39:36 -04:00
|
|
|
user = create_leader_user
|
|
|
|
end
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(true)
|
2014-07-08 17:39:36 -04:00
|
|
|
run_job
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
2014-07-08 17:39:36 -04:00
|
|
|
end
|
2014-07-28 02:42:38 -04:00
|
|
|
|
|
|
|
it "doesn't demote if user hasn't lost requirements (low water mark)" do
|
|
|
|
user = nil
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time(4.days.ago) do
|
2014-07-28 02:42:38 -04:00
|
|
|
user = create_leader_user
|
|
|
|
end
|
|
|
|
|
2014-09-05 01:20:39 -04:00
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(false)
|
2014-07-28 02:42:38 -04:00
|
|
|
run_job
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
2018-08-24 18:41:03 -04:00
|
|
|
it "demotes a user with a group_granted_trust_level of 2" do
|
|
|
|
group = Fabricate(:group, grant_trust_level: 2)
|
2018-08-09 20:42:23 -04:00
|
|
|
user = nil
|
|
|
|
freeze_time(4.days.ago) do
|
2018-08-24 18:41:03 -04:00
|
|
|
user = Fabricate(:user, trust_level: TrustLevel[3])
|
|
|
|
group.add(user)
|
2018-08-09 20:42:23 -04:00
|
|
|
end
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(true)
|
|
|
|
run_job
|
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[2])
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-08-24 18:41:03 -04:00
|
|
|
it "doesn't demote user if their group_granted_trust_level is 3" do
|
|
|
|
group = Fabricate(:group, grant_trust_level: 3)
|
2018-08-09 20:42:23 -04:00
|
|
|
user = nil
|
|
|
|
freeze_time(4.days.ago) do
|
2018-08-24 18:41:03 -04:00
|
|
|
user = Fabricate(:user, trust_level: TrustLevel[3])
|
|
|
|
group.add(user)
|
2018-08-09 20:42:23 -04:00
|
|
|
end
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(true)
|
|
|
|
run_job
|
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
|
|
|
end
|
2018-10-11 15:11:40 -04:00
|
|
|
|
|
|
|
it "doesn't demote with very high tl3_promotion_min_duration value" do
|
|
|
|
SiteSetting.stubs(:tl3_promotion_min_duration).returns(2000000000)
|
|
|
|
user = nil
|
|
|
|
freeze_time(500.days.ago) do
|
|
|
|
user = create_leader_user
|
|
|
|
end
|
|
|
|
expect(user).to be_on_tl3_grace_period
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_lost?).returns(true)
|
|
|
|
run_job
|
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
|
|
|
end
|
2020-04-16 12:28:16 -04:00
|
|
|
|
|
|
|
it "doesn't demote if default trust level for all users is 3" do
|
|
|
|
SiteSetting.default_trust_level = 3
|
|
|
|
user = Fabricate(:user, trust_level: TrustLevel[3], created_at: 1.year.ago)
|
|
|
|
expect(user).to_not be_on_tl3_grace_period
|
|
|
|
TrustLevel3Requirements.any_instance.stubs(:requirements_met?).returns(false)
|
|
|
|
run_job
|
|
|
|
expect(user.reload.trust_level).to eq(TrustLevel[3])
|
|
|
|
end
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
end
|