diff --git a/app/assets/javascripts/discourse/lib/utilities.js b/app/assets/javascripts/discourse/lib/utilities.js index 52cc93303c8..fc9acfb55a8 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js +++ b/app/assets/javascripts/discourse/lib/utilities.js @@ -339,7 +339,7 @@ Discourse.Utilities = { return t.add('days', 1).toJSON(); } } else { - return arg; + return (arg === '' ? null : arg); } } } diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index d199e3326d4..e660ebf8bb3 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -443,7 +443,6 @@ Discourse.Composer = Discourse.Model.extend({ postStream = this.get('topic.postStream'), addedToStream = false; - // Build the post object var createdPost = Discourse.Post.create({ raw: this.get('reply'), diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 684f2e463a6..d4036486aea 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -55,7 +55,7 @@ class TopicCreator end def setup_auto_close_time - @guardian.ensure_can_moderate!(@topic) + return unless @guardian.can_moderate?(@topic) @topic.set_auto_close(@opts[:auto_close_time], @user) end diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 9ecbdf4f017..7b6fe2e4115 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -187,11 +187,10 @@ describe PostCreator do end context 'when auto-close param is given' do - it 'ensures the user can auto-close the topic' do + it 'ensures the user can auto-close the topic, but ignores auto-close param silently' do Guardian.any_instance.stubs(:can_moderate?).returns(false) - expect { - PostCreator.new(user, basic_topic_params.merge(auto_close_time: 2)).create - }.to raise_error(Discourse::InvalidAccess) + post = PostCreator.new(user, basic_topic_params.merge(auto_close_time: 2)).create + post.topic.auto_close_at.should be_nil end end end diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb new file mode 100644 index 00000000000..1969218eae9 --- /dev/null +++ b/spec/components/topic_creator_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe TopicCreator do + + let(:user) { Fabricate(:user) } + let(:moderator) { Fabricate(:moderator) } + let(:admin) { Fabricate(:admin) } + + let(:valid_attrs) { Fabricate.attributes_for(:topic) } + + describe '#create' do + context 'success cases' do + before do + TopicCreator.any_instance.expects(:save_topic).returns(true) + TopicCreator.any_instance.expects(:watch_topic).returns(true) + SiteSetting.stubs(:allow_duplicate_topic_titles?).returns(true) + end + + it "should be possible for an admin to create a topic" do + TopicCreator.create(admin, Guardian.new(admin), valid_attrs).should be_valid + end + + it "should be possible for a moderator to create a topic" do + TopicCreator.create(moderator, Guardian.new(moderator), valid_attrs).should be_valid + end + + context 'regular user' do + before { SiteSetting.stubs(:min_trust_to_create_topic).returns(TrustLevel.levels[:newuser]) } + + it "should be possible for a regular user to create a topic" do + TopicCreator.create(user, Guardian.new(user), valid_attrs).should be_valid + end + + it "should be possible for a regular user to create a topic with blank auto_close_time" do + TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(auto_close_time: '')).should be_valid + end + + it "ignores auto_close_time without raising an error" do + topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(auto_close_time: '24')) + topic.should be_valid + topic.auto_close_at.should be_nil + end + end + end + end + +end