2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe WatchedWord do
|
2023-11-22 02:31:42 +08:00
|
|
|
fab!(:tl2_user) { Fabricate(:user, trust_level: TrustLevel[2], refresh_auto_groups: true) }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:topic)
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:first_post) { Fabricate(:post, topic: topic) }
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
let(:require_approval_word) do
|
|
|
|
Fabricate(:watched_word, action: WatchedWord.actions[:require_approval])
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2017-06-29 04:56:44 +08:00
|
|
|
let(:flag_word) { Fabricate(:watched_word, action: WatchedWord.actions[:flag]) }
|
|
|
|
let(:block_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
|
2019-07-22 19:59:56 +08:00
|
|
|
let(:another_block_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-06-03 08:21:38 +08:00
|
|
|
before_all { WordWatcher.clear_cache! }
|
|
|
|
|
|
|
|
after { WordWatcher.clear_cache! }
|
2017-07-28 00:26:55 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with block" do
|
2017-06-29 04:56:44 +08:00
|
|
|
def should_block_post(manager)
|
|
|
|
expect {
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to_not be_success
|
2019-07-22 19:59:56 +08:00
|
|
|
expect(result.errors[:base]&.first).to eq(
|
|
|
|
I18n.t("contains_blocked_word", word: block_word.word),
|
|
|
|
)
|
2017-06-29 04:56:44 +08:00
|
|
|
}.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:55:15 +08:00
|
|
|
it "escapes the blocked word in error message" do
|
|
|
|
block_word = Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "<a>")
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
raw: "Want some #{block_word.word} for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to_not be_success
|
|
|
|
expect(result.errors[:base]&.first).to eq(I18n.t("contains_blocked_word", word: "<a>"))
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
it "should prevent the post from being created" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
raw: "Want some #{block_word.word} for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
should_block_post(manager)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "look at title too" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
title: "We sell #{block_word.word} online",
|
|
|
|
raw: "Want some poutine for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
should_block_post(manager)
|
|
|
|
end
|
|
|
|
|
2023-05-15 17:45:04 +08:00
|
|
|
it "should handle UTF-8 characters" do
|
|
|
|
block_word = Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "abc")
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(tl2_user, title: "Hello world", raw: "abcódef", topic_id: topic.id)
|
|
|
|
|
|
|
|
expect(manager.perform).to be_success
|
|
|
|
end
|
|
|
|
|
2019-05-16 22:19:41 +08:00
|
|
|
it "should block the post from admin" do
|
2017-06-29 04:56:44 +08:00
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
admin,
|
|
|
|
raw: "Want some #{block_word.word} for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
2019-05-16 22:19:41 +08:00
|
|
|
should_block_post(manager)
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
2019-05-16 22:19:41 +08:00
|
|
|
it "should block the post from moderator" do
|
2017-06-29 04:56:44 +08:00
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
moderator,
|
|
|
|
raw: "Want some #{block_word.word} for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
2019-05-16 22:19:41 +08:00
|
|
|
should_block_post(manager)
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
it "should block the post if it contains multiple blocked words" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
moderator,
|
|
|
|
raw: "Want some #{block_word.word} #{another_block_word.word} for cheap?",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
expect {
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to_not be_success
|
2019-07-24 00:04:53 +08:00
|
|
|
expect(result.errors[:base]&.first).to eq(
|
|
|
|
I18n.t(
|
|
|
|
"contains_blocked_words",
|
|
|
|
words: [block_word.word, another_block_word.word].sort.join(", "),
|
2023-01-09 19:18:21 +08:00
|
|
|
),
|
2019-07-24 00:04:53 +08:00
|
|
|
)
|
2019-07-22 19:59:56 +08:00
|
|
|
}.to_not change { Post.count }
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
it "should block in a private message too" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
raw: "Want some #{block_word.word} for cheap?",
|
|
|
|
title: "this is a new title",
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: Fabricate(:user, trust_level: TrustLevel[2]).username,
|
|
|
|
)
|
|
|
|
should_block_post(manager)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "blocks on revisions" do
|
2019-05-09 11:28:28 +08:00
|
|
|
post = Fabricate(:post, topic: Fabricate(:topic, user: tl2_user), user: tl2_user)
|
2017-06-29 04:56:44 +08:00
|
|
|
expect {
|
|
|
|
PostRevisor.new(post).revise!(
|
|
|
|
post.user,
|
|
|
|
{ raw: "Want some #{block_word.word} for cheap?" },
|
|
|
|
revised_at: post.updated_at + 10.seconds,
|
|
|
|
)
|
|
|
|
expect(post.errors).to be_present
|
|
|
|
post.reload
|
|
|
|
}.to_not change { post.raw }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with require_approval" do
|
2017-06-29 04:56:44 +08:00
|
|
|
it "should queue the post for approval" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
raw: "My dog's name is #{require_approval_word.word}.",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-11 05:42:49 +08:00
|
|
|
expect(result.reason).to eq(:watched_word)
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "looks at title too" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
title: "You won't believe these #{require_approval_word.word} dog names!",
|
|
|
|
raw: "My dog's name is Porkins.",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not queue posts from admin" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
admin,
|
|
|
|
raw: "My dog's name is #{require_approval_word.word}.",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not queue posts from moderator" do
|
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
moderator,
|
|
|
|
raw: "My dog's name is #{require_approval_word.word}.",
|
|
|
|
topic_id: topic.id,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't need approval in a private message" do
|
2022-10-05 08:50:20 +08:00
|
|
|
Group.refresh_automatic_groups!
|
2017-06-29 04:56:44 +08:00
|
|
|
manager =
|
|
|
|
NewPostManager.new(
|
|
|
|
tl2_user,
|
|
|
|
raw: "Want some #{require_approval_word.word} for cheap?",
|
|
|
|
title: "this is a new title",
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: Fabricate(:user, trust_level: TrustLevel[2]).username,
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with flag" do
|
2017-06-29 04:56:44 +08:00
|
|
|
def should_flag_post(author, raw, topic)
|
|
|
|
post = Fabricate(:post, raw: raw, topic: topic, user: author)
|
|
|
|
expect { Jobs::ProcessPost.new.execute(post_id: post.id) }.to change { PostAction.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
1,
|
2017-06-29 04:56:44 +08:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PostAction.where(
|
|
|
|
post_id: post.id,
|
|
|
|
post_action_type_id: PostActionType.types[:inappropriate],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_not_flag_post(author, raw, topic)
|
|
|
|
post = Fabricate(:post, raw: raw, topic: topic, user: author)
|
|
|
|
expect { Jobs::ProcessPost.new.execute(post_id: post.id) }.to_not change { PostAction.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should flag the post as inappropriate" do
|
2020-06-02 23:49:02 +08:00
|
|
|
topic = Fabricate(:topic, user: tl2_user)
|
|
|
|
post = Fabricate(:post, raw: "I said.... #{flag_word.word}", topic: topic, user: tl2_user)
|
|
|
|
Jobs::ProcessPost.new.execute(post_id: post.id)
|
|
|
|
expect(
|
|
|
|
PostAction.where(
|
|
|
|
post_id: post.id,
|
|
|
|
post_action_type_id: PostActionType.types[:inappropriate],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
reviewable = ReviewableFlaggedPost.where(target: post)
|
|
|
|
expect(reviewable).to be_present
|
|
|
|
expect(ReviewableScore.where(reviewable: reviewable, reason: "watched_word")).to be_present
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should look at the title too" do
|
|
|
|
should_flag_post(
|
|
|
|
tl2_user,
|
|
|
|
"I thought the movie was not bad actually.",
|
|
|
|
Fabricate(:topic, user: tl2_user, title: "Read my #{flag_word.word} review!"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't flag posts by admin" do
|
|
|
|
should_not_flag_post(
|
|
|
|
admin,
|
|
|
|
"I thought the #{flag_word.word} was bad.",
|
|
|
|
Fabricate(:topic, user: admin),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't flag posts by moderator" do
|
|
|
|
should_not_flag_post(
|
|
|
|
moderator,
|
|
|
|
"I thought the #{flag_word.word} was bad.",
|
|
|
|
Fabricate(:topic, user: moderator),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is compatible with flag_sockpuppets" do
|
|
|
|
SiteSetting.flag_sockpuppets = true
|
|
|
|
ip_address = "182.189.119.174"
|
2023-12-13 11:50:13 +08:00
|
|
|
user1 =
|
|
|
|
Fabricate(:user, ip_address: ip_address, created_at: 2.days.ago, refresh_auto_groups: true)
|
|
|
|
user2 = Fabricate(:user, ip_address: ip_address, refresh_auto_groups: true)
|
2017-06-29 04:56:44 +08:00
|
|
|
first = create_post(user: user1, created_at: 2.days.ago)
|
|
|
|
sockpuppet_post =
|
|
|
|
create_post(
|
|
|
|
user: user2,
|
|
|
|
topic: first.topic,
|
|
|
|
raw: "I thought the #{flag_word.word} was bad.",
|
|
|
|
)
|
|
|
|
expect(PostAction.where(post_id: sockpuppet_post.id).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "flags in private message too" do
|
|
|
|
post =
|
|
|
|
Fabricate(
|
|
|
|
:private_message_post,
|
|
|
|
raw: "Want some #{flag_word.word} for cheap?",
|
|
|
|
user: tl2_user,
|
|
|
|
)
|
|
|
|
expect { Jobs::ProcessPost.new.execute(post_id: post.id) }.to change { PostAction.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
1,
|
2017-06-29 04:56:44 +08:00
|
|
|
)
|
|
|
|
expect(
|
|
|
|
PostAction.where(
|
|
|
|
post_id: post.id,
|
|
|
|
post_action_type_id: PostActionType.types[:inappropriate],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "flags on revisions" do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2017-06-29 04:56:44 +08:00
|
|
|
post = Fabricate(:post, topic: Fabricate(:topic, user: tl2_user), user: tl2_user)
|
|
|
|
expect {
|
|
|
|
PostRevisor.new(post).revise!(
|
|
|
|
post.user,
|
|
|
|
{ raw: "Want some #{flag_word.word} for cheap?" },
|
|
|
|
revised_at: post.updated_at + 10.seconds,
|
|
|
|
)
|
|
|
|
}.to change { PostAction.count }.by(1)
|
|
|
|
expect(
|
|
|
|
PostAction.where(
|
|
|
|
post_id: post.id,
|
|
|
|
post_action_type_id: PostActionType.types[:inappropriate],
|
|
|
|
).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
2017-10-13 03:34:22 +08:00
|
|
|
|
|
|
|
it "should not flag on rebake" do
|
|
|
|
post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: Fabricate(:topic, user: tl2_user),
|
|
|
|
user: tl2_user,
|
|
|
|
raw: "I have coupon codes. Message me.",
|
|
|
|
)
|
|
|
|
Fabricate(:watched_word, action: WatchedWord.actions[:flag], word: "coupon")
|
|
|
|
expect { post.rebake! }.to_not change { PostAction.count }
|
|
|
|
end
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
end
|