2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/jobs/enqueue_suspect_users_spec.rb
Krzysztof Kotlarek 33d0292170
FIX: Exclude suspended users from suspect users review queue (#37796)
What is the problem?

The `approve_suspect_users` feature flags new accounts as suspect if
they have a bio/website but minimal reading activity. The
`Jobs::EnqueueSuspectUsers` scheduled job runs every 2 hours and targets
accounts that are at least 1 day old. If an admin suspends a spammy user
before that job runs, the suspended user still gets added to the review
queue, creating unnecessary noise for moderators reviewing an
already-handled case.

What is the solution?

Added the `User.not_suspended` scope to the query in
`Jobs::EnqueueSuspectUsers` so that suspended users are excluded. Users
who have already been suspended by an admin have been dealt with and
should not appear in the suspect users review queue.
2026-02-16 10:46:52 +08:00

127 lines
3.2 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Jobs::EnqueueSuspectUsers do
subject(:job) { described_class.new }
before { SiteSetting.approve_suspect_users = true }
it "does nothing when there are no suspect users" do
job.execute({})
expect(ReviewableUser.count).to be_zero
end
context "with suspect users" do
let!(:suspect_user) { Fabricate(:active_user, created_at: 1.day.ago) }
it "creates a reviewable when there is a suspect user" do
job.execute({})
expect(ReviewableUser.count).to eq(1)
end
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,
)
job.execute({})
expect(ReviewableUser.count).to eq(1)
expect(ReviewableUser.last).to eq(review_user)
end
it "adds a score" do
job.execute({})
score = ReviewableScore.last
expect(score.reason).to eq("suspect_user")
end
it "only enqueues non-approved users" do
suspect_user.update!(approved: true)
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
it "does nothing if must_approve_users is set to true" do
SiteSetting.must_approve_users = true
suspect_user.update!(approved: false)
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
it "ignores suspended users" do
suspect_user.update!(suspended_till: 1.year.from_now)
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
it "ignores users created more than six months ago" do
suspect_user.update!(created_at: 1.year.ago)
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
it "ignores users that were imported from another site" do
suspect_user.upsert_custom_fields({ import_id: "fake_id" })
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
it "enqueues a suspect users with custom fields" do
suspect_user.upsert_custom_fields({ field_a: "value", field_b: "value" })
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(true)
end
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" },
)
job.execute({})
expect(ReviewableUser.where(target: suspect_user).exists?).to eq(false)
end
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,
)
job.execute({})
expect(ReviewableUser.count).to eq(1)
end
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,
)
job.execute({})
expect(ReviewableUser.count).to eq(0)
end
end
end