From 1cc72d564fd9b94a8c5e478eba4eafdeca538a1d Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Thu, 28 Nov 2013 11:06:04 -0500
Subject: [PATCH] Add specs for topic create with auto_close_time. Just ignore
 auto_close_time from users who are not authorized to use it instead of
 raising an error.

---
 .../javascripts/discourse/lib/utilities.js    |  2 +-
 .../javascripts/discourse/models/composer.js  |  1 -
 lib/topic_creator.rb                          |  2 +-
 spec/components/post_creator_spec.rb          |  7 ++-
 spec/components/topic_creator_spec.rb         | 47 +++++++++++++++++++
 5 files changed, 52 insertions(+), 7 deletions(-)
 create mode 100644 spec/components/topic_creator_spec.rb

diff --git a/app/assets/javascripts/discourse/lib/utilities.js b/app/assets/javascripts/discourse/lib/utilities.js
index 52cc93303..fc9acfb55 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 d199e3326..e660ebf8b 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 684f2e463..d4036486a 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 9ecbdf4f0..7b6fe2e41 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 000000000..1969218ea
--- /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