discourse/spec/components/post_creator_spec.rb

104 lines
3.4 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
require 'spec_helper'
require 'post_creator'
describe PostCreator do
let(:user) { Fabricate(:user) }
it 'raises an error without a raw value' do
lambda { PostCreator.new(user, {}) }.should raise_error(Discourse::InvalidParameters)
end
context 'new topic' do
let(:category) { Fabricate(:category, user: user) }
let(:basic_topic_params) { {title: 'hello world topic', raw: 'my name is fred', archetype_id: 1} }
2013-02-05 13:16:51 -06:00
let(:image_sizes) { {'http://an.image.host/image.jpg' => {'width' => 111, 'height' => 222}} }
let(:creator) { PostCreator.new(user, basic_topic_params) }
let(:creator_with_category) { PostCreator.new(user, basic_topic_params.merge(category: category.name )) }
let(:creator_with_meta_data) { PostCreator.new(user, basic_topic_params.merge(meta_data: {hello: 'world'} )) }
let(:creator_with_image_sizes) { PostCreator.new(user, basic_topic_params.merge(image_sizes: image_sizes)) }
it 'ensures the user can create the topic' do
Guardian.any_instance.expects(:can_create?).with(Topic,nil).returns(false)
lambda { creator.create }.should raise_error(Discourse::InvalidAccess)
end
context 'success' do
it 'creates a topic' do
lambda { creator.create }.should change(Topic, :count).by(1)
end
it 'returns a post' do
creator.create.is_a?(Post).should be_true
end
it 'extracts links from the post' do
TopicLink.expects(:extract_from).with(instance_of(Post))
creator.create
end
it 'assigns a category when supplied' do
creator_with_category.create.topic.category.should == category
end
it 'adds meta data from the post' do
creator_with_meta_data.create.topic.meta_data['hello'].should == 'world'
end
it 'passes the image sizes through' do
Post.any_instance.expects(:image_sizes=).with(image_sizes)
creator_with_image_sizes.create
2013-02-25 10:42:20 -06:00
end
end
2013-02-05 13:16:51 -06:00
end
context 'existing topic' do
let!(:topic) { Fabricate(:topic, user: user) }
let(:creator) { PostCreator.new(user, raw: 'test reply', topic_id: topic.id, reply_to_post_number: 4) }
it 'ensures the user can create the post' do
Guardian.any_instance.expects(:can_create?).with(Post, topic).returns(false)
lambda { creator.create }.should raise_error(Discourse::InvalidAccess)
end
context 'success' do
it 'should create the post' do
lambda { creator.create }.should change(Post, :count).by(1)
end
it "doesn't create a topic" do
lambda { creator.create }.should_not change(Topic, :count)
end
it "passes through the reply_to_post_number" do
creator.create.reply_to_post_number.should == 4
2013-02-25 10:42:20 -06:00
end
2013-02-05 13:16:51 -06:00
end
end
context 'private message' do
let(:target_user1) { Fabricate(:coding_horror) }
let(:target_user2) { Fabricate(:moderator) }
let(:post) do
PostCreator.create(user, title: 'hi there welcome to my topic',
2013-02-05 13:16:51 -06:00
raw: 'this is my awesome message',
archetype: Archetype.private_message,
target_usernames: [target_user1.username, target_user2.username].join(','))
end
2013-02-25 10:42:20 -06:00
it 'has the right archetype' do
2013-02-05 13:16:51 -06:00
post.topic.archetype.should == Archetype.private_message
end
2013-02-25 10:42:20 -06:00
it 'has the right count (me and 2 other users)' do
2013-02-05 13:16:51 -06:00
post.topic.topic_allowed_users.count.should == 3
end
end
end