[BURs] Add tests for mass_update blacklist updating and nuking

Also simplifies the logic by doing the same thing aliases are doing
This commit is contained in:
Earlopain 2023-02-26 11:58:30 +01:00
parent 0523058a5c
commit 0dada96f38
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
3 changed files with 60 additions and 18 deletions

View File

@ -34,26 +34,12 @@ class TagBatchJob < ApplicationJob
end
end
# this can't handle negated tags or other special cases
def migrate_blacklists(normalized_antecedent, normalized_consequent)
User.where("blacklisted_tags like ?", "%#{normalized_antecedent.to_escaped_for_sql_like}%").find_each do |user|
changed = false
repl = user.blacklisted_tags.split(/\r\n|\r|\n/).map do |line|
list = Tag.scan_tags(line)
if list.include?(normalized_antecedent)
changed = true
(list - [normalized_antecedent] + [normalized_consequent]).join(" ")
else
line
end
User.without_timeout do
User.where_ilike(:blacklisted_tags, "*#{normalized_antecedent}*").find_each(batch_size: 50) do |user|
fixed_blacklist = TagAlias.to_aliased_query(user.blacklisted_tags, overrides: { normalized_antecedent => normalized_consequent })
user.update_column(:blacklisted_tags, fixed_blacklist)
end
if changed
user.update(blacklisted_tags: repl.join("\n"))
end
rescue Exception => e
NewRelic::Agent.notice_error(e)
end
end
end

View File

@ -0,0 +1,43 @@
require "test_helper"
class TagBatchJobTest < ActiveJob::TestCase
should "update posts" do
p1 = create(:post, tag_string: "a aa b c")
p2 = create(:post, tag_string: "a dd y z")
TagBatchJob.perform_now("a", "d", create(:user).id)
p1.reload
p2.reload
assert_equal("aa b c d", p1.tag_string)
assert_equal("d dd y z", p2.tag_string)
end
should "migrate users blacklists" do
initial_blacklist = <<~BLACKLIST.chomp
tag
a tag
a tag b
b tag
-tag
something-else
tagging
BLACKLIST
expected_blacklist = <<~BLACKLIST.chomp
new
a new
a new b
b new
-new
something-else
tagging
BLACKLIST
u = create(:user, blacklisted_tags: initial_blacklist)
create(:user, blacklisted_tags: "aaa")
TagBatchJob.perform_now("tag", "new", u.id)
u.reload
assert_equal(expected_blacklist, u.blacklisted_tags)
end
end

View File

@ -0,0 +1,13 @@
require "test_helper"
class TagNukeJobTest < ActiveJob::TestCase
should "update posts" do
p1 = create(:post, tag_string: "a aa b c")
p2 = create(:post, tag_string: "a dd y z")
TagNukeJob.perform_now("a", create(:user).id)
p1.reload
p2.reload
assert_equal("aa b c", p1.tag_string)
assert_equal("dd y z", p2.tag_string)
end
end