2015-04-01 00:58:56 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
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-04-16 00:12:20 +08: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)
|
|
|
|
expect(result).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a high approval post count' do
|
|
|
|
before do
|
|
|
|
SiteSetting.approve_post_count = 100
|
|
|
|
end
|
|
|
|
it "will return an enqueue result" do
|
|
|
|
result = NewPostManager.default_handler(manager)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-01 00:58:56 +08: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-21 05:19:05 +08:00
|
|
|
@queue_handler = -> (manager) { manager.args[:raw] =~ /queue me/ ? manager.enqueue('default') : nil }
|
2015-04-01 00:58:56 +08:00
|
|
|
|
|
|
|
NewPostManager.add_handler(&@counter_handler)
|
|
|
|
NewPostManager.add_handler(&@queue_handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
NewPostManager.handlers.delete(@counter_handler)
|
|
|
|
NewPostManager.handlers.delete(@queue_handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
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-11 04:29:13 +08:00
|
|
|
expect(QueuedPost.new_count).to be(0)
|
2015-04-01 00:58:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "calls custom enqueuing handlers" do
|
2015-04-11 05:00:50 +08:00
|
|
|
manager = NewPostManager.new(topic.user, raw: 'to the handler I say enqueue me!', title: 'this is the title of the queued post')
|
2015-04-01 00:58:56 +08:00
|
|
|
|
|
|
|
result = manager.perform
|
|
|
|
|
2015-04-11 05:00:50 +08: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-04-01 00:58:56 +08:00
|
|
|
expect(result.action).to eq(:enqueued)
|
|
|
|
expect(result).to be_success
|
|
|
|
expect(result.post).to be_blank
|
2015-04-11 04:29:13 +08:00
|
|
|
expect(QueuedPost.new_count).to be(1)
|
2015-04-01 00:58:56 +08: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
|
|
|
|
|
|
|
|
end
|