2020-01-29 12:38:27 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Jobs::EnqueueSuspectUsers do
|
2023-06-21 09:00:19 -05:00
|
|
|
subject(:job) { described_class.new }
|
|
|
|
|
2020-01-29 12:38:27 -06:00
|
|
|
before { SiteSetting.approve_suspect_users = true }
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "does nothing when there are no suspect users" do
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-01-29 12:38:27 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.count).to be_zero
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "with suspect users" do
|
2021-02-19 10:10:19 -06:00
|
|
|
let!(:suspect_user) { Fabricate(:active_user, created_at: 1.day.ago) }
|
2020-01-29 12:38:27 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "creates a reviewable when there is a suspect user" do
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-01-29 12:38:27 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "only creates one reviewable per user" do
|
|
|
|
review_user =
|
|
|
|
ReviewableUser.needs_review!(
|
|
|
|
target: suspect_user,
|
|
|
|
created_by: Discourse.system_user,
|
|
|
|
reviewable_by_moderator: true,
|
|
|
|
)
|
2020-01-29 12:38:27 -06:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-01-29 12:38:27 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.count).to eq(1)
|
|
|
|
expect(ReviewableUser.last).to eq(review_user)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "adds a score" do
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-01-29 12:38:27 -06:00
|
|
|
score = ReviewableScore.last
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(score.reason).to eq("suspect_user")
|
2020-01-29 12:38:27 -06:00
|
|
|
end
|
2020-03-11 15:05:44 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "only enqueues non-approved users" do
|
2020-03-11 15:05:44 -05:00
|
|
|
suspect_user.update!(approved: true)
|
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-03-11 15:05:44 -05:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "does nothing if must_approve_users is set to true" do
|
2020-03-11 15:05:44 -05:00
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
suspect_user.update!(approved: false)
|
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-03-11 15:05:44 -05:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
|
|
|
|
end
|
2020-03-14 06:47:53 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "ignores users created more than six months ago" do
|
2020-03-14 06:47:53 -05:00
|
|
|
suspect_user.update!(created_at: 1.year.ago)
|
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-03-14 06:47:53 -05:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "ignores users that were imported from another site" do
|
|
|
|
suspect_user.upsert_custom_fields({ import_id: "fake_id" })
|
2020-03-14 06:47:53 -05:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-03-14 06:47:53 -05:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "enqueues a suspect users with custom fields" do
|
|
|
|
suspect_user.upsert_custom_fields({ field_a: "value", field_b: "value" })
|
2020-03-14 06:47:53 -05:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-03-14 06:47:53 -05:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(true)
|
|
|
|
end
|
2020-12-22 11:28:07 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "ignores imported users even if they have multiple custom fields" do
|
|
|
|
suspect_user.upsert_custom_fields(
|
|
|
|
{ field_a: "value", field_b: "value", import_id: "fake_id" },
|
|
|
|
)
|
2020-12-22 11:28:07 -06:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2020-12-22 11:28:07 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
|
|
|
|
end
|
2021-02-19 10:10:19 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "enqueues a suspect user with not enough time read" do
|
|
|
|
suspect_user.user_stat.update!(
|
|
|
|
posts_read_count: 2,
|
|
|
|
topics_entered: 2,
|
|
|
|
time_read: 30.seconds.to_i,
|
|
|
|
)
|
2021-02-19 10:10:19 -06:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2021-02-19 10:10:19 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "ignores users if their time read is higher than one minute" do
|
|
|
|
suspect_user.user_stat.update!(
|
|
|
|
posts_read_count: 2,
|
|
|
|
topics_entered: 2,
|
|
|
|
time_read: 2.minutes.to_i,
|
|
|
|
)
|
2021-02-19 10:10:19 -06:00
|
|
|
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute({})
|
2021-02-19 10:10:19 -06:00
|
|
|
|
|
|
|
expect(ReviewableUser.count).to eq(0)
|
|
|
|
end
|
2020-01-29 12:38:27 -06:00
|
|
|
end
|
|
|
|
end
|