Skip to content

Commit 879accb

Browse files
committed
added test for CleanUpSpammyUsers job + improved system user handling
1 parent ae0dfa3 commit 879accb

8 files changed

Lines changed: 58 additions & 8 deletions

File tree

app/helpers/application_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def current_user
339339
# Gets the special System user
340340
# @return [User, nil]
341341
def system_user
342-
User.find(-1)
342+
User.system
343343
end
344344

345345
##

app/jobs/clean_up_spammy_users_job.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ def perform(created_after: 1.month.ago)
88
.where('users.created_at >= ?', created_after)
99
.where(users: { deleted: false }).group('users.id').having('count(posts.id) > 0')
1010
.having('count(distinct if(posts.deleted = true, null, posts.id)) = 0')
11+
1112
possible_spammers.each do |spammer|
1213
all_posts_spam = spammer.posts.all? do |post|
1314
# A post is considered spam if there are any helpful spam flags on it.
14-
post.flags.any? { |flag| flag.post_flag_type.name == "it's spam" && flag.status == 'helpful' }
15+
post.flags.any? do |flag|
16+
flag.post_flag_type.name == "it's spam" && flag.status == 'helpful'
17+
end
1518
end
1619
if all_posts_spam
1720
spammer.block('automatic block from spam cleanup job', length: 2.years)
18-
spammer.soft_delete(User.find(-1))
21+
spammer.soft_delete(User.system)
1922
end
2023
end
2124
end

app/models/user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def self.search(term)
6060
where('username LIKE ?', "%#{sanitize_sql_like(term)}%")
6161
end
6262

63+
# Gets the system user
64+
# @return [User, nil]
65+
def self.system
66+
find_by(id: -1)
67+
end
68+
6369
# Safely gets the user's reputation even if they don't have a community user
6470
# @return [Integer] user's reputation
6571
def reputation

test/fixtures/flags.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ helpful_flag_scorer:
4444
user: flag_scorer
4545
community: sample
4646
status: helpful
47+
48+
helpful_on_spam_question:
49+
reason: It's obviously spam
50+
post: deleted_spam_question (Post)
51+
post_flag_type: spam
52+
user: standard_user
53+
community: sample
54+
status: helpful

test/fixtures/post_flag_types.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ not_confidential:
1919
community: sample
2020
confidential: false
2121
name: Definitely not confidential
22+
23+
spam:
24+
community: sample
25+
confidential: false
26+
name: it's spam

test/fixtures/posts.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,20 @@ imported_question:
530530
community: sample
531531
category: main
532532
license: cc_by_sa
533+
534+
deleted_spam_question:
535+
post_type: question
536+
title: Best spam in the neighborhood! Call 555-55-55
537+
body: Find the best spam in your neighborhood on Spam Central
538+
body_markdown: <p>Find the best spam in your neighborhood on <a href="https://example.com/spam">Spam Central</a></p>
539+
tags_cache:
540+
- discussion
541+
tags:
542+
- discussion
543+
user: spammer
544+
community: sample
545+
category: main
546+
license: cc_by_sa
547+
deleted: true
548+
deleted_at: 2019-01-01T00:00:00.000000Z
549+
deleted_by: moderator
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
require 'test_helper'
22

33
class CleanUpSpammyUsersJobTest < ActiveJob::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
test 'should correctly clean up spammy users' do
5+
spammer = users(:spammer)
6+
7+
assert_not spammer.deleted?
8+
9+
perform_enqueued_jobs do
10+
CleanUpSpammyUsersJob.perform_later
11+
end
12+
13+
assert_performed_jobs 1
14+
spammer.reload
15+
16+
assert spammer.deleted?
17+
assert spammer.deleted_by.same_as?(User.system)
18+
end
719
end

test/models/user_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,8 @@ class UserTest < ActiveSupport::TestCase
234234
ability = abilities(:unrestricted)
235235
community = communities(:sample)
236236
basic = users(:basic_user)
237-
system = users(:system)
238237

239-
restricted_users = [basic, system]
238+
restricted_users = [basic, User.system]
240239

241240
restricted_users.each do |user|
242241
assert_not user.ability_on?(community.id, ability.internal_id),

0 commit comments

Comments
 (0)