beast rev 2066
[beast-modified.git] / test / functional / posts_controller_test.rb
blob1bdd7e36f330754de4b97e0ca0f5c1af8f1d8f9e
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
8   all_fixtures
9   def setup
10     @controller = PostsController.new
11     @request    = ActionController::TestRequest.new
12     @response   = ActionController::TestResponse.new
13   end
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
21     login_as :aaron
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
26   
27     assert_equal old_counts.collect { |n| n + 1}, counts.call
28     assert_equal old_equal, equal.call
29   end
31   def test_should_create_reply_with_xml
32     content_type 'application/xml'
33     authorize_as :aaron
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"]
37   end
39   def test_should_update_topic_replied_at_upon_replying
40     old=topics(:pdi).replied_at
41     login_as :aaron
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
45   end
47   def test_should_reply_with_no_body
48     assert_difference Post, :count, 0 do
49       login_as :aaron
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')
52     end
53   end
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
61     login_as :aaron
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
68   end
70   def test_should_delete_reply_with_xml
71     content_type 'application/xml'
72     authorize_as :aaron
73     delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_reply).id, :format => 'xml'
74     assert_response :success
75   end
77   def test_should_delete_reply_as_moderator
78     assert_difference Post, :count, -1 do
79       login_as :sam
80       delete :destroy, :forum_id => forums(:rails).id, :topic_id => topics(:pdi).id, :id => posts(:pdi_rebuttal).id
81     end
82   end
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
87         login_as :aaron
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 }
91       end
92     end
93   end
95   def test_can_edit_own_post
96     login_as :sam
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')
99   end
101   def test_can_edit_own_post_with_xml
102     content_type 'application/xml'
103     authorize_as :sam
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
106   end
109   def test_can_edit_other_post_as_moderator
110     login_as :sam
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')
113   end
115   def test_cannot_edit_other_post
116     login_as :sam
117     put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:galactus).id, :post => {}
118     assert_redirected_to login_path
119   end
121   def test_cannot_edit_other_post_with_xml
122     content_type 'application/xml'
123     authorize_as :sam
124     put :update, :forum_id => forums(:comics).id, :topic_id => topics(:galactus).id, :id => posts(:galactus).id, :post => {}, :format => 'xml'
125     assert_response 401
126   end
128   def test_cannot_edit_own_post_user_id
129     login_as :sam
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
133   end
135   def test_can_edit_other_post_as_admin
136     login_as :aaron
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')
139   end
140   
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
144     assert_select 'post'
145   end
146   
147   def test_should_view_recent_posts
148     get :index
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'
152   end
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'
159   end
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'
166   end
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'
174   end
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'
182   end
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'
190   end
192   def test_should_view_monitored_posts
193     get :monitored, :user_id => users(:aaron).id
194     assert_models_equal [posts(:pdi_reply)], assigns(:posts)
195   end
196   
197   def test_should_not_view_unmonitored_posts
198     get :monitored, :user_id => users(:sam).id
199     assert_models_equal [], assigns(:posts)
200   end
201   
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)
207   end
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)
213   end
214   
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)
219   end
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)
225   end
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)
231   end
232   
233   def test_disallow_new_post_to_locked_topic
234     galactus = topics(:galactus)
235     galactus.locked = 1
236     galactus.save
237     login_as :aaron
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]
241   end