jobs: migrate mass updates to ActiveJob.
Also fixes a bug where mod actions weren't logged on mass updates. Creating the mod action silently failed because it was called when CurrentUser wasn' set.
This commit is contained in:
55
test/jobs/tag_batch_change_job_test.rb
Normal file
55
test/jobs/tag_batch_change_job_test.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
require "test_helper"
|
||||
|
||||
class TagBatchChangeJobTest < ActiveJob::TestCase
|
||||
context "a tag batch change" do
|
||||
setup do
|
||||
@user = create(:moderator_user)
|
||||
@post = create(:post, :tag_string => "aaa")
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
should "find the correct count" do
|
||||
assert_equal(1, TagBatchChangeJob.estimate_update_count("aaa", "bbb"))
|
||||
end
|
||||
end
|
||||
|
||||
should "execute" do
|
||||
TagBatchChangeJob.perform_now("aaa", "bbb", @user, "127.0.0.1")
|
||||
assert_equal("bbb", @post.reload.tag_string)
|
||||
end
|
||||
|
||||
should "move saved searches" do
|
||||
ss = create(:saved_search, user: @user, query: "123 ... 456")
|
||||
TagBatchChangeJob.perform_now("...", "bbb", @user, "127.0.0.1")
|
||||
assert_equal("123 456 bbb", ss.reload.normalized_query)
|
||||
end
|
||||
|
||||
should "move blacklists" do
|
||||
@user.update(blacklisted_tags: "123 456\n789\n")
|
||||
TagBatchChangeJob.perform_now("456", "xxx", @user, "127.0.0.1")
|
||||
|
||||
assert_equal("123 xxx\n789", @user.reload.blacklisted_tags)
|
||||
end
|
||||
|
||||
should "move only saved searches that match the mass update exactly" do
|
||||
ss = create(:saved_search, user: @user, query: "123 ... 456")
|
||||
|
||||
TagBatchChangeJob.perform_now("1", "bbb", @user, "127.0.0.1")
|
||||
assert_equal("... 123 456", ss.reload.normalized_query, "expected '123' to remain unchanged")
|
||||
|
||||
TagBatchChangeJob.perform_now("123 456", "789", @user, "127.0.0.1")
|
||||
assert_equal("... 789", ss.reload.normalized_query, "expected '123 456' to be changed to '789'")
|
||||
end
|
||||
|
||||
should "log a modaction" do
|
||||
TagBatchChangeJob.perform_now("1", "2", @user, "127.0.0.1")
|
||||
assert_equal("mass_update", ModAction.last.category)
|
||||
end
|
||||
|
||||
should "raise an error if there is no predicate" do
|
||||
assert_raises(TagBatchChangeJob::Error) do
|
||||
TagBatchChangeJob.perform_now("", "bbb", @user, "127.0.0.1")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -64,15 +64,17 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
||||
|
||||
context "on approval" do
|
||||
setup do
|
||||
@post = create(:post, tag_string: "foo aaa")
|
||||
@script = %q(
|
||||
create alias foo -> bar
|
||||
create implication bar -> baz
|
||||
mass update aaa -> bbb
|
||||
)
|
||||
|
||||
@bur = FactoryBot.create(:bulk_update_request, :script => @script)
|
||||
@bur.approve!(@admin)
|
||||
|
||||
assert_enqueued_jobs(2)
|
||||
assert_enqueued_jobs(3)
|
||||
workoff_active_jobs
|
||||
|
||||
@ta = TagAlias.where(:antecedent_name => "foo", :consequent_name => "bar").first
|
||||
@@ -92,6 +94,10 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
||||
assert_equal("active", @ti.status)
|
||||
end
|
||||
|
||||
should "process mass updates" do
|
||||
assert_equal("bar baz bbb", @post.reload.tag_string)
|
||||
end
|
||||
|
||||
should "set the alias/implication approvers" do
|
||||
assert_equal(@admin.id, @ta.approver.id)
|
||||
assert_equal(@admin.id, @ti.approver.id)
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
require "test_helper"
|
||||
|
||||
module Moderator
|
||||
class TagBatchChangeTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
super
|
||||
mock_saved_search_service!
|
||||
end
|
||||
|
||||
context "a tag batch change" do
|
||||
setup do
|
||||
@user = FactoryBot.create(:moderator_user)
|
||||
CurrentUser.user = @user
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
@post = FactoryBot.create(:post, :tag_string => "aaa")
|
||||
end
|
||||
|
||||
teardown do
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "#estimate_update_count" do
|
||||
setup do
|
||||
@change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1")
|
||||
end
|
||||
|
||||
should "find the correct count" do
|
||||
assert_equal(1, @change.estimate_update_count)
|
||||
end
|
||||
end
|
||||
|
||||
should "execute" do
|
||||
tag_batch_change = TagBatchChange.new("aaa", "bbb", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
@post.reload
|
||||
assert_equal("bbb", @post.tag_string)
|
||||
end
|
||||
|
||||
should "move saved searches" do
|
||||
ss = FactoryBot.create(:saved_search, :user => @user, :query => "123 ... 456")
|
||||
tag_batch_change = TagBatchChange.new("...", "bbb", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
|
||||
assert_equal("123 456 bbb", ss.reload.normalized_query)
|
||||
end
|
||||
|
||||
should "move blacklists" do
|
||||
@user.update(blacklisted_tags: "123 456\n789\n")
|
||||
tag_batch_change = TagBatchChange.new("456", "xxx", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
@user.reload
|
||||
assert_equal("123 xxx\n789", @user.blacklisted_tags)
|
||||
end
|
||||
|
||||
should "move only saved searches that match the mass update exactly" do
|
||||
ss = FactoryBot.create(:saved_search, :user => @user, :query => "123 ... 456")
|
||||
tag_batch_change = TagBatchChange.new("1", "bbb", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
|
||||
assert_equal("... 123 456", ss.reload.normalized_query, "expected '123' to remain unchanged")
|
||||
|
||||
tag_batch_change = TagBatchChange.new("123 456", "789", @user.id, "127.0.0.1")
|
||||
tag_batch_change.perform
|
||||
|
||||
assert_equal("... 789", ss.reload.normalized_query, "expected '123 456' to be changed to '789'")
|
||||
end
|
||||
|
||||
should "raise an error if there is no predicate" do
|
||||
tag_batch_change = TagBatchChange.new("", "bbb", @user.id, "127.0.0.1")
|
||||
assert_raises(TagBatchChange::Error) do
|
||||
tag_batch_change.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user