2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2015-03-25 14:24:34 -04:00
|
|
|
require_dependency 'queued_post'
|
|
|
|
|
|
|
|
describe QueuedPost do
|
|
|
|
|
2016-01-08 05:53:52 -05:00
|
|
|
describe '#states' do
|
|
|
|
context "verify enum sequence" do
|
|
|
|
before do
|
|
|
|
@states = QueuedPost.states
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'new' should be at 1st position" do
|
|
|
|
expect(@states[:new]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "'rejected' should be at 3rd position" do
|
|
|
|
expect(@states[:rejected]).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 14:24:34 -04:00
|
|
|
context "creating a post" do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
2015-04-14 14:21:02 -04:00
|
|
|
let(:qp) { Fabricate(:queued_post, topic: topic, user: user) }
|
2015-03-25 14:24:34 -04:00
|
|
|
|
|
|
|
it "returns the appropriate options for posting" do
|
|
|
|
create_options = qp.create_options
|
|
|
|
|
|
|
|
expect(create_options[:topic_id]).to eq(topic.id)
|
|
|
|
expect(create_options[:raw]).to eq('This post should be queued up')
|
|
|
|
expect(create_options[:reply_to_post_number]).to eq(1)
|
|
|
|
expect(create_options[:via_email]).to eq(true)
|
|
|
|
expect(create_options[:raw_email]).to eq('store_me')
|
|
|
|
expect(create_options[:auto_track]).to eq(true)
|
|
|
|
expect(create_options[:custom_fields]).to eq('hello' => 'world')
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(create_options[:cooking_options]).to eq(cat: 'hat')
|
|
|
|
expect(create_options[:cook_method]).to eq(Post.cook_methods[:raw_html])
|
2015-03-25 14:24:34 -04:00
|
|
|
expect(create_options[:not_create_option]).to eq(nil)
|
|
|
|
expect(create_options[:image_sizes]).to eq("http://foo.bar/image.png" => {"width" => 0, "height" => 222})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "follows the correct workflow for approval" do
|
2015-04-24 15:25:47 -04:00
|
|
|
qp.create_pending_action
|
2015-03-25 14:24:34 -04:00
|
|
|
post = qp.approve!(admin)
|
|
|
|
|
|
|
|
# Creates the post with the attributes
|
|
|
|
expect(post).to be_present
|
|
|
|
expect(post).to be_valid
|
|
|
|
expect(post.topic).to eq(topic)
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(post.custom_fields['hello']).to eq('world')
|
2015-03-25 14:24:34 -04:00
|
|
|
|
|
|
|
# Updates the QP record
|
|
|
|
expect(qp.approved_by).to eq(admin)
|
|
|
|
expect(qp.state).to eq(QueuedPost.states[:approved])
|
|
|
|
expect(qp.approved_at).to be_present
|
|
|
|
|
2015-04-24 15:25:47 -04:00
|
|
|
# It removes the pending action
|
|
|
|
expect(UserAction.where(queued_post_id: qp.id).count).to eq(0)
|
|
|
|
|
2015-03-25 14:24:34 -04:00
|
|
|
# We can't approve twice
|
|
|
|
expect(-> { qp.approve!(admin) }).to raise_error(QueuedPost::InvalidStateTransition)
|
2015-04-21 11:59:35 -04:00
|
|
|
end
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-04-21 11:59:35 -04:00
|
|
|
it "skips validations" do
|
|
|
|
qp.post_options[:title] = 'too short'
|
|
|
|
post = qp.approve!(admin)
|
|
|
|
expect(post).to be_present
|
2015-03-25 14:24:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "follows the correct workflow for rejection" do
|
2015-04-24 15:25:47 -04:00
|
|
|
qp.create_pending_action
|
2015-03-25 14:24:34 -04:00
|
|
|
qp.reject!(admin)
|
|
|
|
|
|
|
|
# Updates the QP record
|
|
|
|
expect(qp.rejected_by).to eq(admin)
|
|
|
|
expect(qp.state).to eq(QueuedPost.states[:rejected])
|
|
|
|
expect(qp.rejected_at).to be_present
|
|
|
|
|
2015-04-24 15:25:47 -04:00
|
|
|
# It removes the pending action
|
|
|
|
expect(UserAction.where(queued_post_id: qp.id).count).to eq(0)
|
|
|
|
|
2015-03-25 14:24:34 -04:00
|
|
|
# We can't reject twice
|
|
|
|
expect(-> { qp.reject!(admin) }).to raise_error(QueuedPost::InvalidStateTransition)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating a topic" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
context "with a valid topic" do
|
|
|
|
let!(:category) { Fabricate(:category) }
|
2015-04-15 14:32:10 -04:00
|
|
|
let(:qp) { QueuedPost.create(queue: 'eviltrout',
|
2015-03-26 16:57:50 -04:00
|
|
|
state: QueuedPost.states[:new],
|
|
|
|
user_id: user.id,
|
|
|
|
raw: 'This post should be queued up',
|
|
|
|
post_options: {
|
|
|
|
title: 'This is the topic title to queue up',
|
|
|
|
archetype: 'regular',
|
|
|
|
category: category.id,
|
|
|
|
meta_data: {evil: 'trout'}
|
|
|
|
}) }
|
2015-03-25 14:24:34 -04:00
|
|
|
|
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
it "returns the appropriate options for creating a topic" do
|
|
|
|
create_options = qp.create_options
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(create_options[:category]).to eq(category.id)
|
|
|
|
expect(create_options[:archetype]).to eq('regular')
|
|
|
|
expect(create_options[:meta_data]).to eq('evil' => 'trout')
|
|
|
|
end
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
it "creates the post and topic" do
|
|
|
|
topic_count, post_count = Topic.count, Post.count
|
|
|
|
post = qp.approve!(admin)
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(Topic.count).to eq(topic_count + 1)
|
|
|
|
expect(Post.count).to eq(post_count + 1)
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(post).to be_present
|
|
|
|
expect(post).to be_valid
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
topic = post.topic
|
|
|
|
expect(topic).to be_present
|
|
|
|
expect(topic.category).to eq(category)
|
|
|
|
end
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-04-15 15:29:37 -04:00
|
|
|
it "rejecting doesn't create the post and topic" do
|
2015-03-26 16:57:50 -04:00
|
|
|
topic_count, post_count = Topic.count, Post.count
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
qp.reject!(admin)
|
2015-03-25 14:24:34 -04:00
|
|
|
|
2015-03-26 16:57:50 -04:00
|
|
|
expect(Topic.count).to eq(topic_count)
|
|
|
|
expect(Post.count).to eq(post_count)
|
|
|
|
end
|
2015-03-25 14:24:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-20 17:19:05 -04:00
|
|
|
context "visibility" do
|
|
|
|
it "works as expected in the invisible queue" do
|
|
|
|
qp = Fabricate(:queued_post, queue: 'invisible')
|
|
|
|
expect(qp).to_not be_visible
|
|
|
|
expect(QueuedPost.visible).to_not include(qp)
|
|
|
|
expect(QueuedPost.new_count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works as expected in the visible queue" do
|
|
|
|
qp = Fabricate(:queued_post, queue: 'default')
|
|
|
|
expect(qp).to be_visible
|
|
|
|
expect(QueuedPost.visible).to include(qp)
|
|
|
|
expect(QueuedPost.new_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 14:24:34 -04:00
|
|
|
end
|