1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'posts_controller'
4 # Re-raise errors caught by the controller.
5 class PostsController; def rescue_action(e) raise e end; end
7 class PostsControllerTest < Test::Unit::TestCase
10 @controller = PostsController.new
11 @request = ActionController::TestRequest.new
12 @response = ActionController::TestResponse.new
15 def test_should_create_reply
16 counts = lambda { [Post.count, forums(:rails).posts_count, users(:aaron).posts_count, topics(:pdi).posts_count] }
17 equal = lambda { [forums(:rails).topics_count] }
18 old_counts = counts.call
19 old_equal = equal.call
22 post :create, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :post => { :body => 'blah' }
23 assert_redirected_to topic_path(:forum_id => forums(:rails).id, :id => topics(:pdi).id, :anchor => assigns(:post).dom_id, :page => '1')
24 assert_equal topics(:pdi), assigns(:topic)
25 [forums(:rails), users(:aaron), topics(:pdi)].each &:reload
27 assert_equal old_counts.collect { |n| n + 1}, counts.call
28 assert_equal old_equal, equal.call
31 def test_should_create_reply_with_xml
32 content_type 'application/xml'
34 post :create, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :post => { :body => 'blah' }, :format => 'xml'
35 assert_response :created
36 assert_equal formatted_post_url(:forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => assigns(:post), :format => :xml), @response.headers["Location"]
39 def test_should_update_topic_replied_at_upon_replying
40 old=topics(:pdi).replied_at
42 post :create, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :post => { :body => 'blah' }
43 assert_not_equal(old, topics(:pdi).reload.replied_at)
44 assert old < topics(:pdi).reload.replied_at
47 def test_should_reply_with_no_body
48 assert_difference Post, :count, 0 do
50 post :create, :forum_id => forums(:rails).id, :topic_id => posts(:pdi).id, :post => {}
51 assert_redirected_to topic_path(:forum_id => forums(:rails).id, :id => posts(:pdi).id, :anchor => 'reply-form', :page => '1')
55 def test_should_delete_reply
56 counts = lambda { [Post.count, forums(:rails).posts_count, users(:sam).posts_count, topics(:pdi).posts_count] }
57 equal = lambda { [forums(:rails).topics_count] }
58 old_counts = counts.call
59 old_equal = equal.call
62 delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_reply).id
63 assert_redirected_to topic_path(:forum_id => forums(:rails), :id => topics(:pdi))
64 [forums(:rails), users(:sam), topics(:pdi)].each &:reload
66 assert_equal old_counts.collect { |n| n - 1}, counts.call
67 assert_equal old_equal, equal.call
70 def test_should_delete_reply_with_xml
71 content_type 'application/xml'
73 delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_reply).id, :format => 'xml'
74 assert_response :success
77 def test_should_delete_reply_as_moderator
78 assert_difference Post, :count, -1 do
80 delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_rebuttal).id
84 def test_should_delete_topic_if_deleting_the_last_reply
85 assert_difference Post, :count, -1 do
86 assert_difference Topic, :count, -1 do
88 delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:il8n).id, :id => posts(:il8n).id
89 assert_redirected_to forum_path(forums(:rails))
90 assert_raise(ActiveRecord::RecordNotFound) { topics(:il8n).reload }
95 def test_can_edit_own_post
97 put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:silver_surfer).id, :post => {}
98 assert_redirected_to topic_path(:forum_id => forums(:comics), :id => topics(:galactus), :anchor => posts(:silver_surfer).dom_id, :page => '1')
101 def test_can_edit_own_post_with_xml
102 content_type 'application/xml'
104 put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:silver_surfer).id, :post => {}, :format => 'xml'
105 assert_response :success
109 def test_can_edit_other_post_as_moderator
111 put :update, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_rebuttal).id, :post => {}
112 assert_redirected_to topic_path(:forum_id => forums(:rails), :id => posts(:pdi), :anchor => posts(:pdi_rebuttal).dom_id, :page => '1')
115 def test_cannot_edit_other_post
117 put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:galactus).id, :post => {}
118 assert_redirected_to login_path
121 def test_cannot_edit_other_post_with_xml
122 content_type 'application/xml'
124 put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:galactus).id, :post => {}, :format => 'xml'
128 def test_cannot_edit_own_post_user_id
130 put :update, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_reply).id, :post => { :user_id => 32 }
131 assert_redirected_to topic_path(:forum_id => forums(:rails), :id => posts(:pdi), :anchor => posts(:pdi_reply).dom_id, :page => '1')
132 assert_equal users(:sam).id, posts(:pdi_reply).reload.user_id
135 def test_can_edit_other_post_as_admin
137 put :update, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_rebuttal).id, :post => {}
138 assert_redirected_to topic_path(:forum_id => forums(:rails), :id => posts(:pdi), :anchor => posts(:pdi_rebuttal).dom_id, :page => '1')
141 def test_should_view_post_as_xml
142 get :show, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_rebuttal).id, :format => 'xml'
143 assert_response :success
147 def test_should_view_recent_posts
149 assert_response :success
150 assert_models_equal [posts(:il8n), posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus), posts(:ponies), posts(:pdi_rebuttal), posts(:pdi_reply), posts(:pdi), posts(:sticky)], assigns(:posts)
151 assert_select 'html>head'
154 def test_should_view_posts_by_forum
155 get :index, :forum_id => forums(:comics).id
156 assert_response :success
157 assert_models_equal [posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus)], assigns(:posts)
158 assert_select 'html>head'
161 def test_should_view_posts_by_user
162 get :index, :user_id => users(:sam).id
163 assert_response :success
164 assert_models_equal [posts(:shield), posts(:silver_surfer), posts(:ponies), posts(:pdi_reply), posts(:sticky)], assigns(:posts)
165 assert_select 'html>head'
168 def test_should_view_recent_posts_with_xml
169 content_type 'application/xml'
170 get :index, :format => 'xml'
171 assert_response :success
172 assert_models_equal [posts(:il8n), posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus), posts(:ponies), posts(:pdi_rebuttal), posts(:pdi_reply), posts(:pdi), posts(:sticky)], assigns(:posts)
173 assert_select 'posts>post'
176 def test_should_view_posts_by_forum_with_xml
177 content_type 'application/xml'
178 get :index, :forum_id => forums(:comics).id, :format => 'xml'
179 assert_response :success
180 assert_models_equal [posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus)], assigns(:posts)
181 assert_select 'posts>post'
184 def test_should_view_posts_by_user_with_xml
185 content_type 'application/xml'
186 get :index, :user_id => users(:sam).id, :format => 'xml'
187 assert_response :success
188 assert_models_equal [posts(:shield), posts(:silver_surfer), posts(:ponies), posts(:pdi_reply), posts(:sticky)], assigns(:posts)
189 assert_select 'posts>post'
192 def test_should_view_monitored_posts
193 get :monitored, :user_id => users(:aaron).id
194 assert_models_equal [posts(:pdi_reply)], assigns(:posts)
197 def test_should_not_view_unmonitored_posts
198 get :monitored, :user_id => users(:sam).id
199 assert_models_equal [], assigns(:posts)
203 def test_should_search_recent_posts
204 get :search, :q => 'pdi'
205 assert_response :success
206 assert_models_equal [posts(:pdi_rebuttal), posts(:pdi_reply), posts(:pdi)], assigns(:posts)
209 def test_should_search_posts_by_forum
210 get :search, :forum_id => forums(:comics).id, :q => 'galactus'
211 assert_response :success
212 assert_models_equal [posts(:silver_surfer), posts(:galactus)], assigns(:posts)
215 def test_should_view_recent_posts_as_rss
216 get :index, :format => 'rss'
217 assert_response :success
218 assert_models_equal [posts(:il8n), posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus), posts(:ponies), posts(:pdi_rebuttal), posts(:pdi_reply), posts(:pdi), posts(:sticky)], assigns(:posts)
221 def test_should_view_posts_by_forum_as_rss
222 get :index, :forum_id => forums(:comics).id, :format => 'rss'
223 assert_response :success
224 assert_models_equal [posts(:shield_reply), posts(:shield), posts(:silver_surfer), posts(:galactus)], assigns(:posts)
227 def test_should_view_posts_by_user_as_rss
228 get :index, :user_id => users(:sam).id, :format => 'rss'
229 assert_response :success
230 assert_models_equal [posts(:shield), posts(:silver_surfer), posts(:ponies), posts(:pdi_reply), posts(:sticky)], assigns(:posts)
233 def test_disallow_new_post_to_locked_topic
234 galactus = topics(:galactus)
238 post :create, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :post => { :body => 'blah' }
239 assert_redirected_to topic_path(:forum_id => forums(:comics), :id => topics(:galactus))
240 assert_equal 'This topic is locked', flash[:notice]