2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
require 'new_post_manager'
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe NewPostManager do
|
2022-02-06 21:23:34 -06:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2015-03-31 11:58:56 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "default action" do
|
2015-03-31 11:58:56 -05:00
|
|
|
it "creates the post by default" do
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user, raw: 'this is a new post', topic_id: topic.id)
|
2015-03-31 11:58:56 -05:00
|
|
|
result = manager.perform
|
|
|
|
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(result.post).to be_a(Post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "default action" do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:other_user) { Fabricate(:user) }
|
2015-05-04 10:07:46 -05:00
|
|
|
|
|
|
|
it "doesn't enqueue private messages" do
|
2022-10-04 19:50:20 -05:00
|
|
|
Group.refresh_automatic_groups!
|
2015-05-13 11:08:39 -05:00
|
|
|
SiteSetting.approve_unless_trust_level = 4
|
|
|
|
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user,
|
2015-05-04 10:07:46 -05:00
|
|
|
raw: 'this is a new post',
|
|
|
|
title: 'this is a new title',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: other_user.username)
|
|
|
|
|
2015-05-13 11:08:39 -05:00
|
|
|
result = manager.perform
|
|
|
|
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(result.post.topic.private_message?).to eq(true)
|
|
|
|
expect(result.post).to be_a(Post)
|
|
|
|
|
|
|
|
# It doesn't enqueue replies to the private message either
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user,
|
2015-05-13 11:08:39 -05:00
|
|
|
raw: 'this is a new reply',
|
|
|
|
topic_id: result.post.topic_id)
|
|
|
|
|
2015-05-04 10:07:46 -05:00
|
|
|
result = manager.perform
|
|
|
|
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(result.post.topic.private_message?).to eq(true)
|
|
|
|
expect(result.post).to be_a(Post)
|
|
|
|
end
|
2015-05-13 11:08:39 -05:00
|
|
|
|
2022-03-28 13:25:26 -05:00
|
|
|
it "doesn't enqueue topic if it doesn't meet the tag restrictions of the category" do
|
|
|
|
tag1 = Fabricate(:tag)
|
|
|
|
tag2 = Fabricate(:tag)
|
|
|
|
tag3 = Fabricate(:tag)
|
|
|
|
tag_group = Fabricate(:tag_group, tags: [tag2])
|
|
|
|
category = Fabricate(:category, tags: [tag1], tag_groups: [tag_group])
|
|
|
|
category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = true
|
|
|
|
category.save!
|
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new post',
|
|
|
|
title: 'this is a new post',
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag1.name, tag3.name]
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.success?).to eq(false)
|
|
|
|
expect(result.reviewable.persisted?).to eq(false)
|
|
|
|
expect(result.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.category_does_not_allow_tags",
|
|
|
|
count: 1,
|
|
|
|
category: category.name,
|
|
|
|
tags: tag3.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new post',
|
|
|
|
title: 'this is a new post',
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag2.name, tag3.name]
|
|
|
|
)
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.success?).to eq(false)
|
|
|
|
expect(result.reviewable.persisted?).to eq(false)
|
|
|
|
expect(result.errors.full_messages).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"tags.forbidden.category_does_not_allow_tags",
|
|
|
|
count: 1,
|
|
|
|
category: category.name,
|
|
|
|
tags: tag3.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2015-05-04 10:07:46 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "default handler" do
|
2022-02-06 21:23:34 -06:00
|
|
|
let(:manager) { NewPostManager.new(user, raw: 'this is new post content', topic_id: topic.id) }
|
2015-04-15 11:12:20 -05:00
|
|
|
|
|
|
|
context 'with the settings zeroed out' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 0
|
|
|
|
SiteSetting.approve_unless_trust_level = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return a result action" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2015-04-21 12:59:57 -05:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(false)
|
2015-04-15 11:12:20 -05:00
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
context 'with basic post/topic count restrictions' do
|
2020-09-09 11:47:37 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with a correct `user_stat.post_count`" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:post_count)
|
|
|
|
|
|
|
|
manager.user.user_stat.update(post_count: 1)
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with a correct `user_stat.topic_count`" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:post_count)
|
|
|
|
|
|
|
|
manager.user.user_stat.update(topic_count: 1)
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-02 13:54:03 -06:00
|
|
|
context 'with a high approval post count and TL0' do
|
2015-04-15 11:12:20 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
2016-03-02 12:20:13 -06:00
|
|
|
topic.user.trust_level = 0
|
2015-04-15 11:12:20 -05:00
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2015-04-21 12:59:57 -05:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
2015-04-15 11:12:20 -05:00
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(result.reason).to eq(:post_count)
|
2015-04-15 11:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-02 13:54:03 -06:00
|
|
|
context 'with a high approval post count and TL1' do
|
2016-03-02 12:20:13 -06:00
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
|
|
|
topic.user.trust_level = 1
|
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2016-03-02 13:54:03 -06:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(result.reason).to eq(:post_count)
|
2016-03-02 13:54:03 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a high approval post count, but TL2' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
2022-02-06 21:23:34 -06:00
|
|
|
user.update!(trust_level: 2)
|
2016-03-02 13:54:03 -06:00
|
|
|
end
|
2022-02-06 21:23:34 -06:00
|
|
|
|
2016-03-02 13:54:03 -06:00
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2016-03-02 12:20:13 -06:00
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-09 11:24:28 -06:00
|
|
|
context 'with a high approval post count and secure category' do
|
|
|
|
it 'does not create topic' do
|
|
|
|
SiteSetting.approve_post_count = 100
|
|
|
|
user = Fabricate(:user)
|
|
|
|
category_group = Fabricate(:category_group, permission_type: 2)
|
2019-04-10 16:42:49 -05:00
|
|
|
Fabricate(:group_user, group: category_group.group, user_id: user.id)
|
2018-11-09 11:24:28 -06:00
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category_group.category_id
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(manager.perform.errors["base"][0]).to eq(I18n.t("js.errors.reasons.forbidden"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 11:12:20 -05:00
|
|
|
context 'with a high trust level setting' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_unless_trust_level = 4
|
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2015-04-21 12:59:57 -05:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
2015-04-15 11:12:20 -05:00
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(result.reason).to eq(:trust_level)
|
2015-04-15 11:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
2015-05-04 10:07:46 -05:00
|
|
|
|
2019-04-02 10:11:43 -05:00
|
|
|
context "with uncategorized disabled, and approval" do
|
|
|
|
before do
|
|
|
|
SiteSetting.allow_uncategorized_topics = false
|
|
|
|
SiteSetting.approve_unless_trust_level = 4
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
npm = NewPostManager.new(
|
|
|
|
Fabricate(:user),
|
|
|
|
title: 'this is a new topic title',
|
|
|
|
raw: "this is the raw content",
|
|
|
|
category: Fabricate(:category).id
|
|
|
|
)
|
|
|
|
|
|
|
|
result = NewPostManager.default_handler(npm)
|
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.errors).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-06 04:41:25 -05:00
|
|
|
context 'with staged moderation setting enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_unless_staged = true
|
2022-02-06 21:23:34 -06:00
|
|
|
user.update!(staged: true)
|
2018-04-06 04:41:25 -05:00
|
|
|
end
|
2022-02-06 21:23:34 -06:00
|
|
|
|
2018-04-06 04:41:25 -05:00
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(result.reason).to eq(:staged)
|
2018-04-06 04:41:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 13:51:36 -05:00
|
|
|
context 'with a high trust level setting for new topics but post responds to existing topic' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_new_topics_unless_trust_level = 4
|
|
|
|
end
|
|
|
|
it "doesn't return a result action" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-10 12:51:54 -05:00
|
|
|
context 'with a fast typer' do
|
|
|
|
before do
|
|
|
|
user.update!(trust_level: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the silence reason in the system locale" do
|
2020-09-09 12:36:22 -05:00
|
|
|
manager = build_manager_with('this is new post content')
|
2022-02-06 21:23:34 -06:00
|
|
|
|
2020-08-10 12:51:54 -05:00
|
|
|
I18n.with_locale(:fr) do # Simulate french user
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
end
|
2022-02-06 21:23:34 -06:00
|
|
|
|
2020-08-10 12:51:54 -05:00
|
|
|
expect(user.silenced?).to eq(true)
|
|
|
|
expect(user.silence_reason).to eq(I18n.t("user.new_user_typed_too_fast", locale: :en))
|
|
|
|
end
|
2020-09-09 12:36:22 -05:00
|
|
|
|
|
|
|
it 'runs the watched words check before checking if the user is a fast typer' do
|
|
|
|
Fabricate(:watched_word, word: "darn", action: WatchedWord.actions[:require_approval])
|
|
|
|
manager = build_manager_with('this is darn new post content')
|
|
|
|
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:watched_word)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_manager_with(raw)
|
2022-02-06 21:23:34 -06:00
|
|
|
NewPostManager.new(user, raw: raw, topic_id: topic.id, first_post_checks: true)
|
2020-09-09 12:36:22 -05:00
|
|
|
end
|
2020-08-10 12:51:54 -05:00
|
|
|
end
|
|
|
|
|
2020-09-18 10:45:09 -05:00
|
|
|
context 'with media' do
|
|
|
|
let(:manager_opts) do
|
|
|
|
{
|
|
|
|
raw: 'this is new post content', topic_id: topic.id, first_post_checks: false,
|
|
|
|
image_sizes: {
|
|
|
|
"http://localhost:3000/uploads/default/original/1X/652fc9667040b1b89dc4d9b061a823ddb3c0cef0.jpeg" => {
|
|
|
|
"width" => "500", "height" => "500"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.update!(trust_level: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'queues the post for review because if it contains embedded media.' do
|
|
|
|
SiteSetting.review_media_unless_trust_level = 1
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user, manager_opts)
|
2020-09-18 10:45:09 -05:00
|
|
|
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:contains_media)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not enqueue the post if the poster is a trusted user' do
|
|
|
|
SiteSetting.review_media_unless_trust_level = 0
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user, manager_opts)
|
2020-09-18 10:45:09 -05:00
|
|
|
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
|
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2016-09-22 13:51:36 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "new topic handler" do
|
2022-02-06 21:23:34 -06:00
|
|
|
let(:manager) { NewPostManager.new(user, raw: 'this is new topic content', title: 'new topic title') }
|
2016-09-22 13:51:36 -05:00
|
|
|
context 'with a high trust level setting for new topics' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_new_topics_unless_trust_level = 4
|
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(result.reason).to eq(:new_topics_unless_trust_level)
|
2016-09-22 13:51:36 -05:00
|
|
|
end
|
|
|
|
end
|
2015-04-15 11:12:20 -05:00
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "extensibility priority" do
|
2015-04-28 12:53:05 -05:00
|
|
|
after do
|
|
|
|
NewPostManager.clear_handlers!
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:default_handler) { NewPostManager.method(:default_handler) }
|
|
|
|
|
|
|
|
it "adds in order by default" do
|
2017-07-27 20:20:09 -05:00
|
|
|
handler = -> { nil }
|
2015-04-28 12:53:05 -05:00
|
|
|
|
|
|
|
NewPostManager.add_handler(&handler)
|
2019-05-25 08:53:03 -05:00
|
|
|
expect(NewPostManager.handlers).to eq([handler])
|
2015-04-28 12:53:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can be added in high priority" do
|
2017-07-27 20:20:09 -05:00
|
|
|
a = -> { nil }
|
|
|
|
b = -> { nil }
|
|
|
|
c = -> { nil }
|
2015-04-28 12:53:05 -05:00
|
|
|
|
|
|
|
NewPostManager.add_handler(100, &a)
|
|
|
|
NewPostManager.add_handler(50, &b)
|
|
|
|
NewPostManager.add_handler(101, &c)
|
2019-05-25 08:53:03 -05:00
|
|
|
expect(NewPostManager.handlers).to eq([c, a, b])
|
2015-04-28 12:53:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "extensibility" do
|
2015-03-31 11:58:56 -05:00
|
|
|
before do
|
|
|
|
@counter = 0
|
|
|
|
|
|
|
|
@counter_handler = lambda do |manager|
|
|
|
|
result = nil
|
|
|
|
if manager.args[:raw] == 'this post increases counter'
|
|
|
|
@counter += 1
|
|
|
|
result = NewPostResult.new(:counter, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-04-20 16:19:05 -05:00
|
|
|
@queue_handler = -> (manager) { manager.args[:raw] =~ /queue me/ ? manager.enqueue('default') : nil }
|
2015-03-31 11:58:56 -05:00
|
|
|
|
|
|
|
NewPostManager.add_handler(&@counter_handler)
|
|
|
|
NewPostManager.add_handler(&@queue_handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2015-04-28 12:53:05 -05:00
|
|
|
NewPostManager.clear_handlers!
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
2015-04-21 12:59:57 -05:00
|
|
|
it "has a queue enabled" do
|
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2015-03-31 11:58:56 -05:00
|
|
|
it "calls custom handlers" do
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user, raw: 'this post increases counter', topic_id: topic.id)
|
2015-03-31 11:58:56 -05:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
|
|
|
expect(result.action).to eq(:counter)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_blank
|
|
|
|
expect(@counter).to be(1)
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(Reviewable.list_for(Discourse.system_user).count).to be(0)
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls custom enqueuing handlers" do
|
2020-08-10 17:14:15 -05:00
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.min_trust_to_create_tag = 0
|
|
|
|
SiteSetting.min_trust_level_to_tag_topics = 0
|
2019-01-03 11:03:01 -06:00
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'to the handler I say enqueue me!',
|
2019-04-05 13:07:47 -05:00
|
|
|
title: 'this is the title of the queued post',
|
2019-05-01 13:40:38 -05:00
|
|
|
tags: ['hello', 'world'],
|
|
|
|
category: topic.category_id
|
2019-01-03 11:03:01 -06:00
|
|
|
)
|
2015-03-31 11:58:56 -05:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
reviewable = result.reviewable
|
2015-04-10 16:00:50 -05:00
|
|
|
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(reviewable).to be_present
|
|
|
|
expect(reviewable.payload['title']).to eq('this is the title of the queued post')
|
|
|
|
expect(reviewable.reviewable_scores).to be_present
|
2020-11-13 05:19:01 -06:00
|
|
|
expect(reviewable.force_review).to eq(true)
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(reviewable.reviewable_by_moderator?).to eq(true)
|
2019-05-01 13:40:38 -05:00
|
|
|
expect(reviewable.category).to be_present
|
2019-04-05 13:07:47 -05:00
|
|
|
expect(reviewable.payload['tags']).to eq(['hello', 'world'])
|
2015-03-31 11:58:56 -05:00
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result).to be_success
|
2015-04-24 14:44:59 -05:00
|
|
|
expect(result.pending_count).to eq(1)
|
2015-03-31 11:58:56 -05:00
|
|
|
expect(result.post).to be_blank
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(Reviewable.list_for(Discourse.system_user).count).to eq(1)
|
2015-03-31 11:58:56 -05:00
|
|
|
expect(@counter).to be(0)
|
2019-01-03 11:03:01 -06:00
|
|
|
|
2019-04-23 11:18:39 -05:00
|
|
|
reviewable.perform(Discourse.system_user, :approve_post)
|
2019-01-03 11:03:01 -06:00
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'another post by this user queue me',
|
2019-05-01 13:40:38 -05:00
|
|
|
topic_id: topic.id
|
2019-01-03 11:03:01 -06:00
|
|
|
)
|
|
|
|
result = manager.perform
|
2019-05-01 13:40:38 -05:00
|
|
|
reviewable = result.reviewable
|
|
|
|
|
|
|
|
expect(reviewable.topic).to be_present
|
|
|
|
expect(reviewable.category).to be_present
|
2019-01-03 11:03:01 -06:00
|
|
|
expect(result.pending_count).to eq(1)
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "if nothing returns a result it creates a post" do
|
2022-02-06 21:23:34 -06:00
|
|
|
manager = NewPostManager.new(user, raw: 'this is a new post', topic_id: topic.id)
|
2015-03-31 11:58:56 -05:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(@counter).to be(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "user needs approval?" do
|
2015-08-05 19:07:07 -05:00
|
|
|
let :user do
|
|
|
|
user = Fabricate.build(:user, trust_level: 0)
|
|
|
|
user_stat = UserStat.new(post_count: 0)
|
|
|
|
user.user_stat = user_stat
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2017-06-28 15:56:44 -05:00
|
|
|
it "handles post_needs_approval? correctly" do
|
2015-08-05 19:07:07 -05:00
|
|
|
u = user
|
2017-07-27 20:20:09 -05:00
|
|
|
default = NewPostManager.new(u, {})
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(NewPostManager.post_needs_approval?(default)).to eq(:skip)
|
2015-08-05 19:07:07 -05:00
|
|
|
|
2016-09-09 11:15:56 -05:00
|
|
|
with_check = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check)).to eq(:fast_typer)
|
2015-08-05 19:07:07 -05:00
|
|
|
|
2015-08-05 19:38:30 -05:00
|
|
|
u.user_stat.post_count = 1
|
2016-09-09 11:15:56 -05:00
|
|
|
with_check_and_post = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check_and_post)).to eq(:skip)
|
2015-08-05 19:38:30 -05:00
|
|
|
|
|
|
|
u.user_stat.post_count = 0
|
2015-08-05 19:07:07 -05:00
|
|
|
u.trust_level = 1
|
2016-09-09 11:15:56 -05:00
|
|
|
with_check_tl1 = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 16:42:49 -05:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check_tl1)).to eq(:skip)
|
2015-08-05 19:07:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-12 21:51:08 -05:00
|
|
|
context 'when posting in the category requires approval' do
|
2021-04-18 18:43:50 -05:00
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let!(:review_group) { Fabricate(:group) }
|
|
|
|
let!(:category) { Fabricate(:category, reviewable_by_group_id: review_group.id) }
|
2018-07-12 21:51:08 -05:00
|
|
|
|
|
|
|
context 'when new topics require approval' do
|
|
|
|
before do
|
2021-04-18 18:43:50 -05:00
|
|
|
SiteSetting.tagging_enabled = true
|
2018-07-12 21:51:08 -05:00
|
|
|
category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = true
|
|
|
|
category.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues new topics' do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id
|
|
|
|
)
|
|
|
|
|
2019-04-10 16:42:49 -05:00
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
2021-02-18 12:00:06 -06:00
|
|
|
|
|
|
|
it 'does not enqueue the topic when the poster is a category group moderator' do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
review_group.users << user
|
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
end
|
2021-04-18 18:43:50 -05:00
|
|
|
|
|
|
|
context "when the category has tagging rules" do
|
|
|
|
context "when there is a minimum number of tags required for the category" do
|
|
|
|
before do
|
|
|
|
category.update(minimum_required_tags: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "errors when there are no tags provided" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.errors.full_messages).to include(I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enqueues the topic if there are tags provided" do
|
|
|
|
tag = Fabricate(:tag)
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id,
|
|
|
|
tags: tag.name
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there is a minimum number of tags required from a certain tag group for the category" do
|
|
|
|
let(:tag_group) { Fabricate(:tag_group) }
|
|
|
|
let(:tag) { Fabricate(:tag) }
|
|
|
|
before do
|
|
|
|
TagGroupMembership.create(tag: tag, tag_group: tag_group)
|
2022-04-06 08:08:06 -05:00
|
|
|
category.update(category_required_tag_groups: [CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1)])
|
2021-04-18 18:43:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "errors when there are no tags from the group provided" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.errors.full_messages).to include(
|
|
|
|
I18n.t(
|
|
|
|
"tags.required_tags_from_group",
|
2022-04-06 08:08:06 -05:00
|
|
|
count: category.category_required_tag_groups.first.min_count,
|
|
|
|
tag_group_name: category.category_required_tag_groups.first.tag_group.name,
|
2021-04-18 18:43:50 -05:00
|
|
|
tags: tag.name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enqueues the topic if there are tags provided" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
title: "Let's start a new topic!",
|
|
|
|
category: category.id,
|
|
|
|
tags: [tag.name]
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new posts require approval' do
|
2021-04-18 18:43:50 -05:00
|
|
|
let!(:topic) { Fabricate(:topic, category: category) }
|
2018-07-12 21:51:08 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
category.custom_fields[Category::REQUIRE_REPLY_APPROVAL] = true
|
|
|
|
category.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues new posts' do
|
|
|
|
manager = NewPostManager.new(user, raw: 'this is a new post', topic_id: topic.id)
|
2019-04-10 16:42:49 -05:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't blow up with invalid topic_id" do
|
|
|
|
expect do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new topic',
|
|
|
|
topic_id: 97546
|
|
|
|
)
|
|
|
|
expect(manager.perform.action).to eq(:create_post)
|
|
|
|
end.not_to raise_error
|
|
|
|
end
|
2021-02-18 12:00:06 -06:00
|
|
|
|
|
|
|
it 'does not enqueue the post when the poster is a category group moderator' do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
review_group.users << user
|
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is a new post',
|
|
|
|
topic_id: topic.id
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(result).to be_success
|
|
|
|
end
|
2018-07-12 21:51:08 -05:00
|
|
|
end
|
|
|
|
end
|
2019-07-19 10:56:14 -05:00
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "via email" do
|
2019-07-19 10:56:14 -05:00
|
|
|
let(:manager) do
|
|
|
|
NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
topic_id: topic.id,
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
|
|
|
topic.user.trust_level = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will store via_email and raw_email in the enqueued post" do
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reviewable).to be_present
|
|
|
|
expect(result.reviewable.payload['via_email']).to eq(true)
|
|
|
|
expect(result.reviewable.payload['raw_email']).to eq('raw email contents')
|
|
|
|
|
|
|
|
post = result.reviewable.perform(Discourse.system_user, :approve_post).created_post
|
|
|
|
expect(post.via_email).to eq(true)
|
|
|
|
expect(post.raw_email).to eq("raw email contents")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "via email with a spam failure" do
|
2020-01-21 10:12:00 -06:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it "silences users if its their first post" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_spam: true,
|
|
|
|
first_post_checks: true
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(user.silenced?).to be(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't silence or enqueue exempt users" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
admin,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_spam: true,
|
|
|
|
first_post_checks: true
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
expect(admin.silenced?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "via email with an authentication results failure" do
|
2020-01-21 10:12:00 -06:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it "doesn't silence users" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
user,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_auth_res_action: :enqueue,
|
|
|
|
first_post_checks: true
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(user.silenced?).to be(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "still enqueues exempt users" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
admin,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_auth_res_action: :enqueue
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(user.silenced?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 11:14:14 -05:00
|
|
|
describe "private message via email" do
|
2021-05-20 20:43:47 -05:00
|
|
|
it "doesn't enqueue authentication results failure" do
|
2020-01-21 10:12:00 -06:00
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_auth_res_action: :enqueue
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue spam failure" do
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'this is emailed content',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
via_email: true,
|
|
|
|
raw_email: 'raw email contents',
|
|
|
|
email_spam: true
|
|
|
|
)
|
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:create_post)
|
|
|
|
end
|
|
|
|
end
|
2015-03-31 11:58:56 -05:00
|
|
|
end
|