2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe TrustLevel3Requirements do
|
2014-09-05 13:20:39 +08:00
|
|
|
subject(:tl3_requirements) { described_class.new(user) }
|
2014-06-28 00:26:03 +08:00
|
|
|
|
2024-01-25 14:28:26 +08:00
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:moderator)
|
2020-04-18 00:23:40 +08:00
|
|
|
fab!(:topic1) { Fabricate(:topic) }
|
|
|
|
fab!(:topic2) { Fabricate(:topic) }
|
|
|
|
fab!(:topic3) { Fabricate(:topic) }
|
|
|
|
fab!(:topic4) { Fabricate(:topic) }
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
before { described_class.clear_cache }
|
|
|
|
|
2020-04-18 00:23:40 +08:00
|
|
|
def make_view(topic, at, user_id)
|
|
|
|
TopicViewItem.add(topic.id, "11.22.33.44", user_id, at, _skip_redis = true)
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def like_at(created_by, post, created_at)
|
|
|
|
PostActionCreator.new(
|
|
|
|
created_by,
|
|
|
|
post,
|
|
|
|
PostActionType.types[:like],
|
|
|
|
created_at: created_at,
|
|
|
|
).perform
|
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
describe "requirements" do
|
2018-04-25 01:29:15 +08:00
|
|
|
describe "penalty_counts" do
|
2019-07-19 17:46:10 +08:00
|
|
|
it "returns if the user has been silenced in last 6 months" do
|
2018-04-25 01:29:15 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
UserSilencer.new(user, moderator).silence
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
|
|
|
UserSilencer.new(user, moderator).unsilence
|
2019-12-21 07:22:53 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores system user unsilences" do
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
UserSilencer.new(user, moderator).silence
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
|
|
|
UserSilencer.new(user, Discourse.system_user).unsilence
|
2019-07-19 17:46:10 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
2018-04-25 01:29:15 +08:00
|
|
|
end
|
|
|
|
|
2019-07-19 17:46:10 +08:00
|
|
|
it "returns if the user has been suspended in last 6 months" do
|
2018-04-25 13:38:37 +08:00
|
|
|
user.save!
|
2018-04-25 01:29:15 +08:00
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
|
2018-04-25 13:38:37 +08:00
|
|
|
UserHistory.create!(
|
|
|
|
target_user_id: user.id,
|
2019-12-21 07:22:53 +08:00
|
|
|
acting_user_id: moderator.id,
|
|
|
|
action: UserHistory.actions[:suspend_user],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
target_user_id: user.id,
|
|
|
|
acting_user_id: moderator.id,
|
|
|
|
action: UserHistory.actions[:unsuspend_user],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores system user un-suspend" do
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
target_user_id: user.id,
|
|
|
|
acting_user_id: Discourse.system_user.id,
|
2018-04-25 13:38:37 +08:00
|
|
|
action: UserHistory.actions[:suspend_user],
|
|
|
|
)
|
2018-04-25 01:29:15 +08:00
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
|
|
|
|
2018-04-25 13:38:37 +08:00
|
|
|
UserHistory.create!(
|
|
|
|
target_user_id: user.id,
|
2019-12-21 07:22:53 +08:00
|
|
|
acting_user_id: Discourse.system_user.id,
|
2018-04-25 13:38:37 +08:00
|
|
|
action: UserHistory.actions[:unsuspend_user],
|
|
|
|
)
|
2018-04-25 01:29:15 +08:00
|
|
|
|
2019-07-19 17:46:10 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return if the user been silenced or suspended over 6 months ago" do
|
|
|
|
freeze_time 1.year.ago do
|
|
|
|
UserSilencer.new(user, moderator, silenced_till: 1.months.from_now).silence
|
|
|
|
UserHistory.create!(target_user_id: user.id, action: UserHistory.actions[:suspend_user])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(0)
|
2018-04-25 01:29:15 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(0)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(0)
|
2019-07-19 17:46:10 +08:00
|
|
|
|
|
|
|
freeze_time 3.months.ago do
|
|
|
|
UserSilencer.new(user).unsilence
|
|
|
|
UserSilencer.new(user, moderator, silenced_till: 1.months.from_now).silence
|
|
|
|
UserHistory.create!(target_user_id: user.id, action: UserHistory.actions[:suspend_user])
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(1)
|
2019-11-08 18:23:57 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does return if the user has been silenced or suspended over 6 months ago and continues" do
|
|
|
|
freeze_time 1.year.ago do
|
|
|
|
UserSilencer.new(user, moderator, silenced_till: 10.years.from_now).silence
|
|
|
|
UserHistory.create!(target_user_id: user.id, action: UserHistory.actions[:suspend_user])
|
|
|
|
user.update(suspended_till: 10.years.from_now)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tl3_requirements.penalty_counts.silenced).to eq(1)
|
2019-07-19 17:46:10 +08:00
|
|
|
expect(tl3_requirements.penalty_counts.suspended).to eq(1)
|
|
|
|
expect(tl3_requirements.penalty_counts.total).to eq(2)
|
2018-04-25 01:29:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 00:13:39 +08:00
|
|
|
it "time_period uses site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_time_period = 80
|
2015-12-23 00:13:39 +08:00
|
|
|
expect(tl3_requirements.time_period).to eq(80)
|
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
it "min_days_visited uses site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_days_visited = 66
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_days_visited).to eq(66)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_topics_replied_to uses site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_topics_replied_to = 12
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_topics_replied_to).to eq(12)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_topics_viewed depends on site setting and number of topics created" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_topics_viewed = 75
|
2014-06-28 00:26:03 +08:00
|
|
|
described_class.stubs(:num_topics_in_time_period).returns(31)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_topics_viewed).to eq(23)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2016-05-26 02:55:37 +08:00
|
|
|
it "min_topics_viewed is capped" do
|
|
|
|
SiteSetting.tl3_requires_topics_viewed = 75
|
|
|
|
described_class.stubs(:num_topics_in_time_period).returns(31)
|
|
|
|
SiteSetting.tl3_requires_topics_viewed_cap = 20
|
|
|
|
expect(tl3_requirements.min_topics_viewed).to eq(20)
|
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
it "min_posts_read depends on site setting and number of posts created" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_posts_read = 66
|
2014-06-28 00:26:03 +08:00
|
|
|
described_class.stubs(:num_posts_in_time_period).returns(1234)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_posts_read).to eq(814)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2016-05-26 02:55:37 +08:00
|
|
|
it "min_posts_read is capped" do
|
|
|
|
SiteSetting.tl3_requires_posts_read = 66
|
|
|
|
described_class.stubs(:num_posts_in_time_period).returns(1234)
|
|
|
|
SiteSetting.tl3_requires_posts_read_cap = 600
|
|
|
|
expect(tl3_requirements.min_posts_read).to eq(600)
|
|
|
|
end
|
|
|
|
|
2014-07-02 01:34:48 +08:00
|
|
|
it "min_topics_viewed_all_time depends on site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_topics_viewed_all_time = 75
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_topics_viewed_all_time).to eq(75)
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_posts_read_all_time depends on site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_posts_read_all_time = 1001
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_posts_read_all_time).to eq(1001)
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
it "max_flagged_posts depends on site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_max_flagged = 3
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.max_flagged_posts).to eq(3)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
2014-08-23 05:33:05 +08:00
|
|
|
|
|
|
|
it "min_likes_given depends on site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_likes_given = 30
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_likes_given).to eq(30)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min_likes_received depends on site setting" do
|
2017-07-07 14:09:14 +08:00
|
|
|
SiteSetting.tl3_requires_likes_received = 20
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.min_likes_received).to eq(20)
|
|
|
|
expect(tl3_requirements.min_likes_received_days).to eq(7)
|
|
|
|
expect(tl3_requirements.min_likes_received_users).to eq(5)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
2015-12-30 01:50:35 +08:00
|
|
|
|
|
|
|
it "min_likes_received_days is capped" do
|
|
|
|
SiteSetting.tl3_requires_likes_received = 600
|
|
|
|
expect(tl3_requirements.min_likes_received).to eq(600)
|
|
|
|
expect(tl3_requirements.min_likes_received_days).to eq(75) # 0.75 * tl3_time_period
|
|
|
|
end
|
|
|
|
|
|
|
|
it "min_likes_received_days works when time_period is 1" do
|
|
|
|
SiteSetting.tl3_requires_likes_received = 20
|
|
|
|
SiteSetting.tl3_time_period = 1
|
|
|
|
expect(tl3_requirements.min_likes_received).to eq(20)
|
|
|
|
expect(tl3_requirements.min_likes_received_days).to eq(1)
|
|
|
|
expect(tl3_requirements.min_likes_received_users).to eq(5)
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "days_visited" do
|
2015-12-23 00:13:39 +08:00
|
|
|
it "counts visits when posts were read no further back than 100 days (default time period) ago" do
|
2014-06-28 00:26:03 +08:00
|
|
|
user.save
|
2015-07-08 00:31:07 +08:00
|
|
|
user.update_posts_read!(1, at: 2.days.ago)
|
|
|
|
user.update_posts_read!(1, at: 3.days.ago)
|
|
|
|
user.update_posts_read!(0, at: 4.days.ago)
|
|
|
|
user.update_posts_read!(3, at: 101.days.ago)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.days_visited).to eq(2)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
2015-12-23 00:13:39 +08:00
|
|
|
|
|
|
|
it "respects tl3_time_period setting" do
|
|
|
|
SiteSetting.tl3_time_period = 200
|
|
|
|
user.save
|
|
|
|
user.update_posts_read!(1, at: 2.days.ago)
|
|
|
|
user.update_posts_read!(1, at: 3.days.ago)
|
|
|
|
user.update_posts_read!(0, at: 4.days.ago)
|
|
|
|
user.update_posts_read!(3, at: 101.days.ago)
|
|
|
|
user.update_posts_read!(4, at: 201.days.ago)
|
|
|
|
expect(tl3_requirements.days_visited).to eq(3)
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "num_topics_replied_to" do
|
|
|
|
it "counts topics in which user replied in last 100 days" do
|
|
|
|
user.save
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
_not_a_reply = create_post(user: user) # user created the topic, so it doesn't count
|
2014-06-28 00:26:03 +08:00
|
|
|
|
|
|
|
topic1 = create_post.topic
|
2019-12-10 08:48:27 +08:00
|
|
|
_reply1 = create_post(topic: topic1, user: user)
|
2014-09-05 13:20:39 +08:00
|
|
|
_reply_again = create_post(topic: topic1, user: user) # two replies in one topic
|
2014-06-28 00:26:03 +08:00
|
|
|
|
|
|
|
topic2 = create_post(created_at: 101.days.ago).topic
|
2014-09-05 13:20:39 +08:00
|
|
|
_reply2 = create_post(topic: topic2, user: user, created_at: 101.days.ago) # topic is over 100 days old
|
2014-06-28 00:26:03 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.num_topics_replied_to).to eq(1)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
2020-04-08 05:32:18 +08:00
|
|
|
|
|
|
|
it "excludes private messages" do
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
private_topic =
|
|
|
|
create_post(
|
|
|
|
user: moderator,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: [user.username, moderator.username],
|
|
|
|
).topic
|
|
|
|
|
|
|
|
_reply1 = create_post(topic: private_topic, user: user)
|
|
|
|
|
|
|
|
expect(tl3_requirements.num_topics_replied_to).to eq(0)
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "topics_viewed" do
|
2015-12-23 00:13:39 +08:00
|
|
|
it "counts topics views within last 100 days (default time period), not counting a topic more than once" do
|
2014-06-28 00:26:03 +08:00
|
|
|
user.save
|
2020-04-18 00:23:40 +08:00
|
|
|
make_view(topic1, 1.day.ago, user.id)
|
|
|
|
make_view(topic1, 3.days.ago, user.id) # same topic, different day
|
|
|
|
make_view(topic2, 4.days.ago, user.id)
|
|
|
|
make_view(topic3, 101.days.ago, user.id) # too long ago
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.topics_viewed).to eq(2)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
2015-12-23 00:13:39 +08:00
|
|
|
|
|
|
|
it "counts topics views within last 200 days, respecting tl3_time_period setting" do
|
|
|
|
SiteSetting.tl3_time_period = 200
|
|
|
|
user.save
|
2020-04-18 00:23:40 +08:00
|
|
|
make_view(topic1, 1.day.ago, user.id)
|
|
|
|
make_view(topic1, 3.days.ago, user.id) # same topic, different day
|
|
|
|
make_view(topic2, 4.days.ago, user.id)
|
|
|
|
make_view(topic3, 101.days.ago, user.id)
|
|
|
|
make_view(topic4, 201.days.ago, user.id) # too long ago
|
2015-12-23 00:13:39 +08:00
|
|
|
expect(tl3_requirements.topics_viewed).to eq(3)
|
|
|
|
end
|
2020-04-08 05:32:18 +08:00
|
|
|
|
|
|
|
it "excludes private messages" do
|
|
|
|
user.save
|
|
|
|
private_topic =
|
|
|
|
create_post(
|
|
|
|
user: moderator,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: [user.username, moderator.username],
|
|
|
|
).topic
|
|
|
|
|
2020-04-18 00:23:40 +08:00
|
|
|
make_view(topic1, 1.day.ago, user.id)
|
|
|
|
make_view(private_topic, 1.day.ago, user.id)
|
2020-04-08 05:32:18 +08:00
|
|
|
expect(tl3_requirements.topics_viewed).to eq(1)
|
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "posts_read" do
|
|
|
|
it "counts posts read within the last 100 days" do
|
|
|
|
user.save
|
2015-07-08 00:31:07 +08:00
|
|
|
user.update_posts_read!(3, at: 2.days.ago)
|
|
|
|
user.update_posts_read!(1, at: 3.days.ago)
|
|
|
|
user.update_posts_read!(0, at: 4.days.ago)
|
|
|
|
user.update_posts_read!(5, at: 101.days.ago)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.posts_read).to eq(4)
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "topics_viewed_all_time" do
|
|
|
|
it "counts topics viewed at any time" do
|
|
|
|
user.save
|
2020-04-18 00:23:40 +08:00
|
|
|
make_view(topic1, 1.day.ago, user.id)
|
|
|
|
make_view(topic2, 100.days.ago, user.id)
|
|
|
|
make_view(topic3, 101.days.ago, user.id)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.topics_viewed_all_time).to eq(3)
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
2020-04-08 05:32:18 +08:00
|
|
|
|
|
|
|
it "excludes private messages" do
|
|
|
|
user.save
|
|
|
|
private_topic =
|
|
|
|
create_post(
|
|
|
|
user: moderator,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: [user.username, moderator.username],
|
|
|
|
).topic
|
|
|
|
|
2020-04-18 00:23:40 +08:00
|
|
|
make_view(topic1, 1.day.ago, user.id)
|
|
|
|
make_view(topic2, 100.days.ago, user.id)
|
|
|
|
make_view(topic3, 101.days.ago, user.id)
|
|
|
|
make_view(private_topic, 1.day.ago, user.id)
|
|
|
|
make_view(private_topic, 100.days.ago, user.id)
|
2020-04-08 05:32:18 +08:00
|
|
|
expect(tl3_requirements.topics_viewed_all_time).to eq(3)
|
|
|
|
end
|
2014-07-02 01:34:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "posts_read_all_time" do
|
|
|
|
it "counts posts read at any time" do
|
|
|
|
user.save
|
2015-07-08 00:31:07 +08:00
|
|
|
user.update_posts_read!(3, at: 2.days.ago)
|
|
|
|
user.update_posts_read!(1, at: 101.days.ago)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.posts_read_all_time).to eq(4)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with flagged posts" do
|
|
|
|
before do
|
|
|
|
user.save
|
|
|
|
flags =
|
|
|
|
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
|
2014-07-31 22:43:13 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
post: Fabricate(:post, user: user),
|
|
|
|
post_action_type_id: PostActionType.types[t],
|
|
|
|
agreed_at: 1.minute.ago,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
_deferred_flags =
|
|
|
|
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
|
2014-08-11 17:56:54 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
post: Fabricate(:post, user: user),
|
|
|
|
post_action_type_id: PostActionType.types[t],
|
|
|
|
deferred_at: 1.minute.ago,
|
|
|
|
)
|
2014-07-31 22:43:13 +08:00
|
|
|
end
|
|
|
|
|
2014-09-05 13:20:39 +08:00
|
|
|
_deleted_flags =
|
|
|
|
%i[off_topic inappropriate notify_user notify_moderators spam].map do |t|
|
2014-07-31 22:43:13 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
post: Fabricate(:post, user: user),
|
|
|
|
post_action_type_id: PostActionType.types[t],
|
|
|
|
deleted_at: 1.minute.ago,
|
|
|
|
)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Same post, different user:
|
2014-07-31 22:43:13 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
post: flags[1].post,
|
|
|
|
post_action_type_id: PostActionType.types[:spam],
|
|
|
|
agreed_at: 1.minute.ago,
|
|
|
|
)
|
2014-06-28 00:26:03 +08:00
|
|
|
|
|
|
|
# Flagged their own post:
|
2014-07-31 22:43:13 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
user: user,
|
|
|
|
post: Fabricate(:post, user: user),
|
|
|
|
post_action_type_id: PostActionType.types[:spam],
|
|
|
|
agreed_at: 1.minute.ago,
|
|
|
|
)
|
2014-06-28 00:26:03 +08:00
|
|
|
|
|
|
|
# More than 100 days ago:
|
2014-07-31 22:43:13 +08:00
|
|
|
Fabricate(
|
|
|
|
:flag,
|
|
|
|
post: Fabricate(:post, user: user, created_at: 101.days.ago),
|
|
|
|
post_action_type_id: PostActionType.types[:spam],
|
|
|
|
created_at: 101.days.ago,
|
|
|
|
agreed_at: 1.day.ago,
|
|
|
|
)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
|
2014-07-31 22:43:13 +08:00
|
|
|
it "num_flagged_posts and num_flagged_by_users count spam and inappropriate agreed flags in the last 100 days" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.num_flagged_posts).to eq(2)
|
|
|
|
expect(tl3_requirements.num_flagged_by_users).to eq(3)
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-23 05:33:05 +08:00
|
|
|
describe "num_likes_given" do
|
2020-04-08 05:32:18 +08:00
|
|
|
before do
|
2019-01-04 01:03:01 +08:00
|
|
|
UserActionManager.enable
|
2020-04-08 05:32:18 +08:00
|
|
|
user.save
|
|
|
|
end
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2020-04-08 05:32:18 +08:00
|
|
|
let(:recent_post1) { create_post(created_at: 1.hour.ago) }
|
|
|
|
let(:recent_post2) { create_post(created_at: 10.days.ago) }
|
|
|
|
let(:old_post) { create_post(created_at: 102.days.ago) }
|
|
|
|
let(:private_post) do
|
|
|
|
create_post(
|
|
|
|
user: moderator,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: [user.username, moderator.username],
|
|
|
|
)
|
|
|
|
end
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2020-04-08 05:32:18 +08:00
|
|
|
it "counts likes given in the last 100 days" do
|
2019-01-04 01:03:01 +08:00
|
|
|
like_at(user, recent_post1, 2.hours.ago)
|
|
|
|
like_at(user, recent_post2, 5.days.ago)
|
|
|
|
like_at(user, old_post, 101.days.ago)
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.num_likes_given).to eq(2)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
2020-04-08 05:32:18 +08:00
|
|
|
|
|
|
|
it "excludes private messages" do
|
|
|
|
like_at(user, recent_post1, 2.hours.ago)
|
|
|
|
like_at(user, private_post, 2.hours.ago)
|
|
|
|
|
|
|
|
expect(tl3_requirements.num_likes_given).to eq(1)
|
|
|
|
end
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "num_likes_received" do
|
2024-01-25 14:28:26 +08:00
|
|
|
before { UserActionManager.enable }
|
2020-04-08 05:32:18 +08:00
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic, user: user, created_at: 102.days.ago) }
|
|
|
|
let(:old_post) { create_post(topic: topic, user: user, created_at: 102.days.ago) }
|
|
|
|
let(:recent_post1) { create_post(topic: topic, user: user, created_at: 1.hour.ago) }
|
|
|
|
let(:recent_post2) { create_post(topic: topic, user: user, created_at: 10.days.ago) }
|
|
|
|
let(:private_post) do
|
|
|
|
create_post(
|
|
|
|
user: user,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: [liker.username, liker2.username],
|
|
|
|
)
|
|
|
|
end
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2020-04-08 05:32:18 +08:00
|
|
|
let(:liker) { Fabricate(:user) }
|
|
|
|
let(:liker2) { Fabricate(:user) }
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2020-04-08 05:32:18 +08:00
|
|
|
it "counts likes received in the last 100 days" do
|
2019-01-04 01:03:01 +08:00
|
|
|
like_at(liker, recent_post1, 2.hours.ago)
|
|
|
|
like_at(liker2, recent_post1, 2.hours.ago)
|
|
|
|
like_at(liker, recent_post2, 5.days.ago)
|
|
|
|
like_at(liker, old_post, 101.days.ago)
|
2020-04-08 05:32:18 +08:00
|
|
|
like_at(liker, private_post, 2.hours.ago)
|
|
|
|
like_at(liker2, private_post, 5.days.ago)
|
2014-08-23 05:33:05 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.num_likes_received).to eq(3)
|
|
|
|
expect(tl3_requirements.num_likes_received_days).to eq(2)
|
|
|
|
expect(tl3_requirements.num_likes_received_users).to eq(2)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "requirements with defaults" do
|
2014-07-28 14:42:38 +08:00
|
|
|
before do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:min_days_visited).returns(50)
|
|
|
|
tl3_requirements.stubs(:min_topics_replied_to).returns(10)
|
|
|
|
tl3_requirements.stubs(:min_topics_viewed).returns(25)
|
|
|
|
tl3_requirements.stubs(:min_posts_read).returns(25)
|
|
|
|
tl3_requirements.stubs(:min_topics_viewed_all_time).returns(200)
|
|
|
|
tl3_requirements.stubs(:min_posts_read_all_time).returns(500)
|
|
|
|
tl3_requirements.stubs(:max_flagged_posts).returns(5)
|
|
|
|
tl3_requirements.stubs(:max_flagged_by_users).returns(5)
|
|
|
|
tl3_requirements.stubs(:min_likes_given).returns(30)
|
|
|
|
tl3_requirements.stubs(:min_likes_received).returns(20)
|
2014-10-08 03:52:54 +08:00
|
|
|
tl3_requirements.stubs(:min_likes_received_days).returns(7)
|
|
|
|
tl3_requirements.stubs(:min_likes_received_users).returns(5)
|
2014-09-05 13:20:39 +08:00
|
|
|
|
|
|
|
tl3_requirements.stubs(:days_visited).returns(50)
|
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(10)
|
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(25)
|
|
|
|
tl3_requirements.stubs(:posts_read).returns(25)
|
|
|
|
tl3_requirements.stubs(:topics_viewed_all_time).returns(200)
|
|
|
|
tl3_requirements.stubs(:posts_read_all_time).returns(500)
|
|
|
|
tl3_requirements.stubs(:num_flagged_posts).returns(0)
|
|
|
|
tl3_requirements.stubs(:num_flagged_by_users).returns(0)
|
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(30)
|
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(20)
|
2014-10-08 03:52:54 +08:00
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(7)
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(5)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are met when all requirements are met" do
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(true)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if too few days visited" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(49)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not lost if requirements are close" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(45)
|
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(9)
|
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(23)
|
|
|
|
tl3_requirements.stubs(:posts_read).returns(23)
|
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(29)
|
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(19)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(false)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough visited" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:days_visited).returns(44)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough topics replied to" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:num_topics_replied_to).returns(8)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough topics viewed" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:topics_viewed).returns(22)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough posts read" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:posts_read).returns(22)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
|
|
|
|
2014-08-23 05:33:05 +08:00
|
|
|
it "are not met if not enough likes given" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(29)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if not enough likes received" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(19)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
|
2014-10-08 03:52:54 +08:00
|
|
|
it "are not met if not enough likes received on different days" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(6)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-10-08 03:52:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if not enough likes received by different users" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(4)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-10-08 03:52:54 +08:00
|
|
|
end
|
|
|
|
|
2014-08-23 05:33:05 +08:00
|
|
|
it "are lost if not enough likes given" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:num_likes_given).returns(26)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough likes received" do
|
2014-09-05 13:20:39 +08:00
|
|
|
tl3_requirements.stubs(:num_likes_received).returns(17)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-08-23 05:33:05 +08:00
|
|
|
end
|
2014-08-30 00:33:34 +08:00
|
|
|
|
|
|
|
it "are not met if suspended" do
|
2018-02-13 06:19:47 +08:00
|
|
|
user.suspended_till = 3.weeks.from_now
|
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if silenced" do
|
|
|
|
user.silenced_till = 3.weeks.from_now
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
2014-08-30 00:33:34 +08:00
|
|
|
end
|
|
|
|
|
2018-04-25 01:29:15 +08:00
|
|
|
it "are not met if previously silenced" do
|
|
|
|
user.save
|
|
|
|
UserHistory.create(target_user_id: user.id, action: UserHistory.actions[:silence_user])
|
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "are not met if previously suspended" do
|
|
|
|
user.save
|
|
|
|
UserHistory.create(target_user_id: user.id, action: UserHistory.actions[:suspend_user])
|
|
|
|
expect(tl3_requirements.requirements_met?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2014-10-08 03:52:54 +08:00
|
|
|
it "are lost if not enough likes received on different days" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_days).returns(4)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-10-08 03:52:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if not enough likes received by different users" do
|
|
|
|
tl3_requirements.stubs(:num_likes_received_users).returns(3)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-10-08 03:52:54 +08:00
|
|
|
end
|
|
|
|
|
2014-08-30 00:33:34 +08:00
|
|
|
it "are lost if suspended" do
|
2018-02-13 06:19:47 +08:00
|
|
|
user.suspended_till = 4.weeks.from_now
|
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "are lost if silenced" do
|
|
|
|
user.silenced_till = 4.weeks.from_now
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(true)
|
2014-08-30 00:33:34 +08:00
|
|
|
end
|
2020-04-17 00:28:16 +08:00
|
|
|
|
|
|
|
[3, 4].each do |default_tl|
|
|
|
|
it "is not lost if default_trust_level is #{default_tl}" do
|
|
|
|
SiteSetting.default_trust_level = default_tl
|
|
|
|
tl3_requirements.stubs(:days_visited).returns(1)
|
|
|
|
expect(tl3_requirements.requirements_lost?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2014-07-28 14:42:38 +08:00
|
|
|
end
|
2014-06-28 00:26:03 +08:00
|
|
|
end
|