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)
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
2015-05-04 11:07:46 -04:00
|
|
|
|
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
|
|
|
|
handler = ->{ nil }
|
|
|
|
|
|
|
|
NewPostManager.add_handler(&handler)
|
|
|
|
expect(NewPostManager.handlers).to eq([default_handler, handler])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be added in high priority" do
|
|
|
|
a = ->{ nil }
|
|
|
|
b = ->{ nil }
|
|
|
|
c = ->{ nil }
|
|
|
|
|
|
|
|
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)
|
2015-04-10 16:29:13 -04:00
|
|
|
expect(QueuedPost.new_count).to be(0)
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls custom enqueuing handlers" do
|
2015-04-10 17:00:50 -04:00
|
|
|
manager = NewPostManager.new(topic.user, raw: 'to the handler I say enqueue me!', title: 'this is the title of the queued post')
|
2015-03-31 12:58:56 -04:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
2015-04-10 17:00:50 -04:00
|
|
|
enqueued = result.queued_post
|
|
|
|
|
|
|
|
expect(enqueued).to be_present
|
|
|
|
expect(enqueued.post_options['title']).to eq('this is the title of the queued post')
|
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
|
2015-04-24 15:44:59 -04:00
|
|
|
expect(QueuedPost.new_count).to eq(1)
|
2015-03-31 12:58:56 -04:00
|
|
|
expect(@counter).to be(0)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it "handles user_needs_approval? correctly" do
|
|
|
|
u = user
|
|
|
|
default = NewPostManager.new(u,{})
|
|
|
|
expect(NewPostManager.user_needs_approval?(default)).to eq(false)
|
|
|
|
|
|
|
|
with_check = NewPostManager.new(u,{first_post_checks: true})
|
|
|
|
expect(NewPostManager.user_needs_approval?(with_check)).to eq(true)
|
|
|
|
|
2015-08-05 20:38:30 -04:00
|
|
|
u.user_stat.post_count = 1
|
|
|
|
with_check_and_post = NewPostManager.new(u,{first_post_checks: true})
|
|
|
|
expect(NewPostManager.user_needs_approval?(with_check_and_post)).to eq(false)
|
|
|
|
|
|
|
|
u.user_stat.post_count = 0
|
2015-08-05 20:07:07 -04:00
|
|
|
u.trust_level = 1
|
|
|
|
with_check_tl1 = NewPostManager.new(u,{first_post_checks: true})
|
|
|
|
expect(NewPostManager.user_needs_approval?(with_check_tl1)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
end
|