2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-03-31 12:58:56 -04:00
|
|
|
require 'new_post_manager'
|
|
|
|
|
|
|
|
describe NewPostManager do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
context "default action" do
|
|
|
|
it "creates the post by default" do
|
|
|
|
manager = NewPostManager.new(topic.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
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(result.post).to be_a(Post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-04 11:07:46 -04:00
|
|
|
context "default action" do
|
|
|
|
let(:other_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "doesn't enqueue private messages" do
|
2015-05-13 12:08:39 -04:00
|
|
|
SiteSetting.approve_unless_trust_level = 4
|
|
|
|
|
2015-05-04 11:07:46 -04:00
|
|
|
manager = NewPostManager.new(topic.user,
|
|
|
|
raw: 'this is a new post',
|
|
|
|
title: 'this is a new title',
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: other_user.username)
|
|
|
|
|
2015-05-13 12:08:39 -04: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
|
|
|
|
manager = NewPostManager.new(topic.user,
|
|
|
|
raw: 'this is a new reply',
|
|
|
|
topic_id: result.post.topic_id)
|
|
|
|
|
2015-05-04 11:07:46 -04: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 12:08:39 -04:00
|
|
|
|
2015-05-04 11:07:46 -04:00
|
|
|
end
|
|
|
|
|
2015-04-15 12:12:20 -04:00
|
|
|
context "default handler" do
|
|
|
|
let(:manager) { NewPostManager.new(topic.user, raw: 'this is new post content', topic_id: topic.id) }
|
|
|
|
|
|
|
|
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 13:59:57 -04:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(false)
|
2015-04-15 12:12:20 -04:00
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-02 14:54:03 -05:00
|
|
|
context 'with a high approval post count and TL0' do
|
2015-04-15 12:12:20 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
2016-03-02 13:20:13 -05:00
|
|
|
topic.user.trust_level = 0
|
2015-04-15 12:12:20 -04:00
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2015-04-21 13:59:57 -04:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
2015-04-15 12:12:20 -04:00
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(result.reason).to eq(:post_count)
|
2015-04-15 12:12:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-02 14:54:03 -05:00
|
|
|
context 'with a high approval post count and TL1' do
|
2016-03-02 13:20:13 -05: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 14:54:03 -05:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(result.reason).to eq(:post_count)
|
2016-03-02 14:54:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a high approval post count, but TL2' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
|
|
|
topic.user.trust_level = 2
|
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
2016-03-02 13:20:13 -05:00
|
|
|
expect(result).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-09 12:24:28 -05: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 17:42:49 -04:00
|
|
|
Fabricate(:group_user, group: category_group.group, user_id: user.id)
|
2018-11-09 12:24:28 -05: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 12:12:20 -04: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 13:59:57 -04:00
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
2015-04-15 12:12:20 -04:00
|
|
|
expect(result.action).to eq(:enqueued)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(result.reason).to eq(:trust_level)
|
2015-04-15 12:12:20 -04:00
|
|
|
end
|
|
|
|
end
|
2015-05-04 11:07:46 -04:00
|
|
|
|
2019-04-02 11:11:43 -04: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 05:41:25 -04:00
|
|
|
context 'with staged moderation setting enabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_unless_staged = true
|
|
|
|
topic.user.staged = true
|
|
|
|
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 17:42:49 -04:00
|
|
|
expect(result.reason).to eq(:staged)
|
2018-04-06 05:41:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-22 14:51:36 -04: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
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "new topic handler" do
|
|
|
|
let(:manager) { NewPostManager.new(topic.user, raw: 'this is new topic content', title: 'new topic title') }
|
|
|
|
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 17:42:49 -04:00
|
|
|
expect(result.reason).to eq(:new_topics_unless_trust_level)
|
2016-09-22 14:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 12:12:20 -04:00
|
|
|
end
|
|
|
|
|
2015-04-28 13:53:05 -04:00
|
|
|
context "extensibility priority" do
|
|
|
|
|
|
|
|
after do
|
|
|
|
NewPostManager.clear_handlers!
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:default_handler) { NewPostManager.method(:default_handler) }
|
|
|
|
|
|
|
|
it "adds in order by default" do
|
2017-07-27 21:20:09 -04:00
|
|
|
handler = -> { nil }
|
2015-04-28 13:53:05 -04:00
|
|
|
|
|
|
|
NewPostManager.add_handler(&handler)
|
|
|
|
expect(NewPostManager.handlers).to eq([default_handler, handler])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be added in high priority" do
|
2017-07-27 21:20:09 -04:00
|
|
|
a = -> { nil }
|
|
|
|
b = -> { nil }
|
|
|
|
c = -> { nil }
|
2015-04-28 13:53:05 -04:00
|
|
|
|
|
|
|
NewPostManager.add_handler(100, &a)
|
|
|
|
NewPostManager.add_handler(50, &b)
|
|
|
|
NewPostManager.add_handler(101, &c)
|
|
|
|
expect(NewPostManager.handlers).to eq([c, a, b, default_handler])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
context "extensibility" do
|
|
|
|
|
|
|
|
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 17:19:05 -04:00
|
|
|
@queue_handler = -> (manager) { manager.args[:raw] =~ /queue me/ ? manager.enqueue('default') : nil }
|
2015-03-31 12:58:56 -04:00
|
|
|
|
|
|
|
NewPostManager.add_handler(&@counter_handler)
|
|
|
|
NewPostManager.add_handler(&@queue_handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2015-04-28 13:53:05 -04:00
|
|
|
NewPostManager.clear_handlers!
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|
|
|
|
|
2015-04-21 13:59:57 -04:00
|
|
|
it "has a queue enabled" do
|
|
|
|
expect(NewPostManager.queue_enabled?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
it "calls custom handlers" do
|
|
|
|
manager = NewPostManager.new(topic.user, raw: 'this post increases counter', topic_id: topic.id)
|
|
|
|
|
|
|
|
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 12:03:01 -05:00
|
|
|
expect(Reviewable.list_for(Discourse.system_user).count).to be(0)
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls custom enqueuing handlers" do
|
2019-01-03 12:03:01 -05:00
|
|
|
SiteSetting.min_score_default_visibility = 20.5
|
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'to the handler I say enqueue me!',
|
2019-04-05 14:07:47 -04:00
|
|
|
title: 'this is the title of the queued post',
|
2019-05-01 14:40:38 -04:00
|
|
|
tags: ['hello', 'world'],
|
|
|
|
category: topic.category_id
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
2015-03-31 12:58:56 -04:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
reviewable = result.reviewable
|
2015-04-10 17:00:50 -04:00
|
|
|
|
2019-01-03 12:03:01 -05: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
|
|
|
|
expect(reviewable.score).to eq(20.5)
|
|
|
|
expect(reviewable.reviewable_by_moderator?).to eq(true)
|
2019-05-01 14:40:38 -04:00
|
|
|
expect(reviewable.category).to be_present
|
2019-04-05 14:07:47 -04:00
|
|
|
expect(reviewable.payload['tags']).to eq(['hello', 'world'])
|
2015-03-31 12:58:56 -04:00
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result).to be_success
|
2015-04-24 15:44:59 -04:00
|
|
|
expect(result.pending_count).to eq(1)
|
2015-03-31 12:58:56 -04:00
|
|
|
expect(result.post).to be_blank
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(Reviewable.list_for(Discourse.system_user).count).to eq(1)
|
2015-03-31 12:58:56 -04:00
|
|
|
expect(@counter).to be(0)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-23 12:18:39 -04:00
|
|
|
reviewable.perform(Discourse.system_user, :approve_post)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
manager = NewPostManager.new(
|
|
|
|
topic.user,
|
|
|
|
raw: 'another post by this user queue me',
|
2019-05-01 14:40:38 -04:00
|
|
|
topic_id: topic.id
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
|
|
|
result = manager.perform
|
2019-05-01 14:40:38 -04:00
|
|
|
reviewable = result.reviewable
|
|
|
|
|
|
|
|
expect(reviewable.topic).to be_present
|
|
|
|
expect(reviewable.category).to be_present
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(result.pending_count).to eq(1)
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "if nothing returns a result it creates a post" do
|
|
|
|
manager = NewPostManager.new(topic.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
|
|
|
|
expect(result.post).to be_present
|
|
|
|
expect(@counter).to be(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-08-05 20:07:07 -04:00
|
|
|
context "user needs approval?" do
|
|
|
|
|
|
|
|
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 16:56:44 -04:00
|
|
|
it "handles post_needs_approval? correctly" do
|
2015-08-05 20:07:07 -04:00
|
|
|
u = user
|
2017-07-27 21:20:09 -04:00
|
|
|
default = NewPostManager.new(u, {})
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(NewPostManager.post_needs_approval?(default)).to eq(:skip)
|
2015-08-05 20:07:07 -04:00
|
|
|
|
2016-09-09 12:15:56 -04:00
|
|
|
with_check = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check)).to eq(:fast_typer)
|
2015-08-05 20:07:07 -04:00
|
|
|
|
2015-08-05 20:38:30 -04:00
|
|
|
u.user_stat.post_count = 1
|
2016-09-09 12:15:56 -04:00
|
|
|
with_check_and_post = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check_and_post)).to eq(:skip)
|
2015-08-05 20:38:30 -04:00
|
|
|
|
|
|
|
u.user_stat.post_count = 0
|
2015-08-05 20:07:07 -04:00
|
|
|
u.trust_level = 1
|
2016-09-09 12:15:56 -04:00
|
|
|
with_check_tl1 = NewPostManager.new(u, first_post_checks: true)
|
2019-04-10 17:42:49 -04:00
|
|
|
expect(NewPostManager.post_needs_approval?(with_check_tl1)).to eq(:skip)
|
2015-08-05 20:07:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-12 22:51:08 -04:00
|
|
|
context 'when posting in the category requires approval' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
context 'when new topics require approval' do
|
|
|
|
before do
|
|
|
|
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 17:42:49 -04:00
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
2018-07-12 22:51:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new posts require approval' do
|
|
|
|
let(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
|
|
|
|
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 17:42:49 -04:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result.reason).to eq(:category)
|
2018-07-12 22:51:08 -04: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
|
|
|
|
end
|
|
|
|
end
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|