PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functional/topics_controller_test.rb

https://github.com/newrooky/lftwb
Ruby | 267 lines | 217 code | 38 blank | 12 comment | 3 complexity | 268ed53125e1ab88ae48c893ce7bea93 MD5 | raw file
Possible License(s): MIT
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. class TopicsControllerTest < Test::Unit::TestCase
  3. def setup
  4. @controller = TopicsController.new
  5. @request = ActionController::TestRequest.new
  6. @response = ActionController::TestResponse.new
  7. end
  8. # # page sure we have a special page link back to the last page
  9. # # of the forum we're currently viewing
  10. # should "have page link to forum" do
  11. # @request.session[:forum_page]=Hash.new(1)
  12. # @request.session[:forum_page][1]=911
  13. # get :show, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id
  14. # assert_tag :tag => "a", :content => "page 911"
  15. # end
  16. context "main forum functionality" do
  17. should "get index" do
  18. get :index, :forum_id => forums(:rails).to_param
  19. assert_redirected_to forum_path(forums(:rails).to_param)
  20. end
  21. should "get index as xml" do
  22. content_type 'application/xml'
  23. get :index, :forum_id => forums(:rails).to_param, :format => 'xml'
  24. assert_response :success
  25. assert_select 'topics>topic'
  26. end
  27. should "show topic as rss" do
  28. get :show, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id, :format => 'rss'
  29. assert_response :success
  30. assert_select 'channel'
  31. end
  32. should "show topic as xml" do
  33. content_type 'application/xml'
  34. get :show, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id, :format => 'xml'
  35. assert_response :success
  36. assert_select 'topic'
  37. end
  38. should "get new" do
  39. login_as :aaron
  40. get :new, :forum_id => forums(:rails).to_param
  41. assert_response :success
  42. end
  43. should "protect sticky and locked from non admin" do
  44. login_as :joe
  45. assert ! users(:joe).admin?
  46. assert ! users(:joe).moderator_of?(:rails)
  47. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :body => 'foo' }
  48. assert assigns(:topic)
  49. assert ! assigns(:topic).sticky?
  50. assert ! assigns(:topic).locked?
  51. end
  52. should "allow sticky and locked to moderator" do
  53. login_as :sam
  54. assert ! users(:sam).admin?
  55. assert users(:sam).moderator_of?(forums(:rails))
  56. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :body => 'foo' }
  57. assert assigns(:topic)
  58. assert assigns(:topic).sticky?
  59. assert assigns(:topic).locked?
  60. end
  61. should "allow admin to sticky and lock" do
  62. login_as :admin
  63. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah2', :sticky => "1", :locked => "1", :body => 'foo' }
  64. assert assigns(:topic).sticky?
  65. assert assigns(:topic).locked?
  66. end
  67. uses_transaction :test_should_not_create_topic_without_body
  68. should "not create topic without body" do
  69. counts = lambda { [Topic.count, Post.count] }
  70. old = counts.call
  71. login_as :admin
  72. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah' }
  73. assert assigns(:topic)
  74. assert assigns(:post)
  75. # both of these should be new records if the save fails so that the view can
  76. # render accordingly
  77. assert assigns(:topic).new_record?
  78. assert assigns(:post).new_record?
  79. assert_equal old, counts.call
  80. end
  81. should "not create topic without title" do
  82. counts = lambda { [Topic.count, Post.count] }
  83. old = counts.call
  84. login_as :admin
  85. post :create, :forum_id => forums(:rails).id, :topic => { :body => 'blah' }
  86. assert_equal "blah", assigns(:topic).body
  87. assert assigns(:post)
  88. # both of these should be new records if the save fails so that the view can
  89. # render accordingly
  90. assert assigns(:topic).new_record?
  91. assert assigns(:post).new_record?
  92. assert_equal old, counts.call
  93. end
  94. should "create topic" do
  95. counts = lambda { [Topic.count, Post.count, forums(:rails).topics_count, forums(:rails).posts_count, users(:admin).posts_count] }
  96. old = counts.call
  97. login_as :admin
  98. post :create, :forum_id => forums(:rails).to_param, :topic => { :title => 'blah', :body => 'foo' }
  99. assert assigns(:topic)
  100. assert assigns(:post)
  101. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  102. [forums(:rails), users(:admin)].each &:reload
  103. assert_equal old.collect { |n| n + 1}, counts.call
  104. end
  105. should "create topic with xml" do
  106. content_type 'application/xml'
  107. authorize_as :admin
  108. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :body => 'foo' }, :format => 'xml'
  109. assert_response :created
  110. assert_equal formatted_forum_topic_url(:forum_id => forums(:rails), :id => assigns(:topic), :format => :xml), @response.headers["Location"]
  111. end
  112. should "delete topic" do
  113. counts = lambda { [Post.count, forums(:rails).topics_count, forums(:rails).posts_count] }
  114. old = counts.call
  115. login_as :admin
  116. delete :destroy, :forum_id => forums(:rails).to_param, :id => topics(:ponies).id
  117. assert_redirected_to forum_path(forums(:rails))
  118. [forums(:rails), users(:aaron)].each &:reload
  119. assert_equal old.collect { |n| n - 1}, counts.call
  120. end
  121. should "delete topic with xml" do
  122. content_type 'application/xml'
  123. authorize_as :admin
  124. delete :destroy, :forum_id => forums(:rails).to_param, :id => topics(:ponies).id, :format => 'xml'
  125. assert_response :success
  126. end
  127. should "allow moderator to delete topic" do
  128. assert_difference "Topic.count", -1 do
  129. login_as :sam
  130. delete :destroy, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id
  131. end
  132. end
  133. should "update views for show" do
  134. assert_difference "topics(:pdi).views" do
  135. get :show, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id
  136. assert_response :success
  137. topics(:pdi).reload
  138. end
  139. end
  140. should "not update views for show via rss" do
  141. assert_difference "topics(:pdi).views", 0 do
  142. get :show, :forum_id => forums(:rails).to_param, :id => topics(:pdi).id, :format => 'rss'
  143. assert_response :success
  144. topics(:pdi).reload
  145. end
  146. end
  147. should "not add viewed topic to session on show rss" do
  148. login_as :aaron
  149. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
  150. assert_response :success
  151. assert session[:topics].blank?
  152. end
  153. should "update views for show except topic author" do
  154. login_as :aaron
  155. views=topics(:pdi).views
  156. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
  157. assert_response :success
  158. assert_equal views, topics(:pdi).reload.views
  159. end
  160. should "show topic" do
  161. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :per_page => 20
  162. assert_response :success
  163. assert_equal topics(:pdi), assigns(:topic)
  164. assert_models_equal [posts(:pdi), posts(:pdi_reply), posts(:pdi_rebuttal)], assigns(:posts)
  165. end
  166. should "show other post" do
  167. get :show, :forum_id => forums(:rails).id, :id => topics(:ponies).id
  168. assert_response :success
  169. assert_equal topics(:ponies), assigns(:topic)
  170. assert_models_equal [posts(:ponies)], assigns(:posts)
  171. end
  172. should "get edit" do
  173. login_as :admin
  174. get :edit, :forum_id => forums(:rails).id, :id => topics(:ponies).id
  175. assert_response :success
  176. end
  177. should "update own post" do
  178. login_as :sam
  179. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { }
  180. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  181. end
  182. should "update with xml" do
  183. content_type 'application/xml'
  184. authorize_as :sam
  185. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { }, :format => 'xml'
  186. assert_response :success
  187. end
  188. should "not update user id of own post" do
  189. login_as :sam
  190. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { :user_id => 32 }
  191. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  192. assert_equal users(:sam).id, posts(:ponies).reload.user_id
  193. end
  194. should "not update other post" do
  195. login_as :sam
  196. put :update, :forum_id => forums(:comics).id, :id => topics(:galactus).id, :topic => { }
  197. assert_redirected_to login_path
  198. end
  199. should "not update other post with xml" do
  200. content_type 'application/xml'
  201. authorize_as :sam
  202. put :update, :forum_id => forums(:comics).id, :id => topics(:galactus).id, :topic => { }, :format => 'xml'
  203. assert_response :unauthorized
  204. end
  205. should "update other post as moderator" do
  206. login_as :sam
  207. put :update, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :topic => { }
  208. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  209. end
  210. should "update other post as admin" do
  211. login_as :admin
  212. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies), :topic => { }
  213. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  214. end
  215. end
  216. context "forums inside a group" do
  217. context 'show topic' do
  218. setup do
  219. get :show, :forum_id => forums(:africa).id, :id => topics(:nigeria).id, :per_page => 20
  220. end
  221. should_respond_with :success
  222. should_render_template "groups/topics/show"
  223. should_assign_to :topic
  224. should "assign topic" do
  225. assert_equal topics(:nigeria), assigns(:topic)
  226. end
  227. end
  228. end
  229. end