/test/functional/topics_controller_test.rb

https://github.com/bennett/communityengine · Ruby · 201 lines · 161 code · 38 blank · 2 comment · 0 complexity · 8aa4291e0954e2037a19de9b967b3e53 MD5 · raw file

  1. require File.dirname(__FILE__) + '/../test_helper'
  2. class TopicsControllerTest < ActionController::TestCase
  3. all_fixtures
  4. def test_should_get_index
  5. get :index, :forum_id => 1
  6. assert_redirected_to forum_path(1)
  7. end
  8. def test_should_get_index_as_xml
  9. content_type 'application/xml'
  10. get :index, :forum_id => 1, :format => 'xml'
  11. assert_response :success
  12. end
  13. def test_should_show_topic_as_rss
  14. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
  15. assert_response :success
  16. end
  17. def test_should_show_topic_as_xml
  18. content_type 'application/xml'
  19. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'xml'
  20. assert_response :success
  21. end
  22. def test_should_get_new
  23. login_as :aaron
  24. get :new, :forum_id => 1
  25. assert_response :success
  26. end
  27. def test_sticky_and_locked_protected_from_non_admin
  28. login_as :joe
  29. assert ! users(:joe).admin?
  30. assert ! users(:joe).moderator_of?(forums(:rails))
  31. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :body => 'foo' }
  32. assert assigns(:topic)
  33. assert ! assigns(:topic).sticky?
  34. assert ! assigns(:topic).locked?
  35. end
  36. def test_sticky_and_locked_allowed_to_moderator
  37. login_as :sam
  38. assert ! users(:sam).admin?
  39. assert users(:sam).moderator_of?(forums(:rails))
  40. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :body => 'foo' }
  41. assert assigns(:topic)
  42. assert assigns(:topic).sticky?
  43. assert assigns(:topic).locked?
  44. end
  45. def test_should_allow_admin_to_sticky_and_lock
  46. login_as :admin
  47. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah2', :sticky => "1", :locked => "1", :body => 'foo' }
  48. assert assigns(:topic).sticky?
  49. assert assigns(:topic).locked?
  50. end
  51. uses_transaction :test_should_not_create_topic_without_body
  52. def test_should_not_create_topic_without_body
  53. counts = lambda { [Topic.count, SbPost.count] }
  54. old = counts.call
  55. login_as :aaron
  56. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah' }
  57. assert assigns(:topic)
  58. assert assigns(:post)
  59. # both of these should be new records if the save fails so that the view can
  60. # render accordingly
  61. assert assigns(:topic).new_record?
  62. assert assigns(:post).new_record?
  63. assert_equal old, counts.call
  64. end
  65. def test_should_create_topic
  66. counts = lambda { [Topic.count, SbPost.count, forums(:rails).topics_count, forums(:rails).sb_posts_count, users(:aaron).sb_posts_count] }
  67. old = counts.call
  68. login_as :aaron
  69. post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :body => 'foo' }, :tag_list => 'tag1, tag2'
  70. assert assigns(:topic)
  71. assert assigns(:post)
  72. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  73. [forums(:rails), users(:aaron)].each &:reload
  74. assert_equal old.collect { |n| n + 1}, counts.call
  75. assert_equal ['tag1', 'tag2'], Topic.find(assigns(:topic).id).tag_list
  76. end
  77. def test_should_delete_topic
  78. counts = lambda { [SbPost.count, forums(:rails).topics_count, forums(:rails).sb_posts_count] }
  79. old = counts.call
  80. login_as :admin
  81. delete :destroy, :forum_id => forums(:rails).id, :id => topics(:ponies).id
  82. assert_redirected_to forum_path(forums(:rails))
  83. [forums(:rails), users(:aaron)].each &:reload
  84. assert_equal old.collect { |n| n - 1}, counts.call
  85. end
  86. def test_should_allow_moderator_to_delete_topic
  87. assert_difference Topic, :count, -1 do
  88. login_as :sam
  89. delete :destroy, :forum_id => forums(:rails).id, :id => topics(:pdi).id
  90. end
  91. end
  92. def test_should_update_views_for_show
  93. assert_difference topics(:pdi), :views do
  94. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
  95. assert_response :success
  96. topics(:pdi).reload
  97. end
  98. end
  99. def test_should_not_update_views_for_show_via_rss
  100. assert_difference topics(:pdi), :views, 0 do
  101. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
  102. assert_response :success
  103. topics(:pdi).reload
  104. end
  105. end
  106. def test_should_not_add_viewed_topic_to_session_on_show_rss
  107. login_as :aaron
  108. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
  109. assert_response :success
  110. assert session[:topics].blank?
  111. end
  112. def test_should_update_views_for_show_except_topic_author
  113. login_as :aaron
  114. views=topics(:pdi).views
  115. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
  116. assert_response :success
  117. assert_equal views, topics(:pdi).reload.views
  118. end
  119. def test_should_show_topic
  120. get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
  121. assert_response :success
  122. assert_equal topics(:pdi), assigns(:topic)
  123. assert_models_equal [sb_posts(:pdi), sb_posts(:pdi_reply), sb_posts(:pdi_rebuttal)], assigns(:posts)
  124. end
  125. def test_should_show_other_post
  126. get :show, :forum_id => forums(:rails).id, :id => topics(:ponies).id
  127. assert_response :success
  128. assert_equal topics(:ponies), assigns(:topic)
  129. assert_models_equal [sb_posts(:ponies)], assigns(:posts)
  130. end
  131. def test_should_get_edit
  132. login_as :admin
  133. get :edit, :forum_id => 1, :id => 1
  134. assert_response :success
  135. end
  136. def test_should_update_own_post
  137. login_as :sam
  138. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { }, :tag_list => 'tagX, tagY'
  139. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  140. assert_equal ['tagX', 'tagY'], topics(:ponies).reload.tag_list
  141. end
  142. def test_should_not_update_user_id_of_own_post
  143. login_as :sam
  144. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { :user_id => 32 }
  145. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  146. assert_equal users(:sam).id, sb_posts(:ponies).reload.user_id
  147. end
  148. def test_should_not_update_other_post
  149. login_as :sam
  150. put :update, :forum_id => forums(:comics).id, :id => topics(:galactus).id, :topic => { }
  151. assert_redirected_to login_path
  152. end
  153. def test_should_update_other_post_as_moderator
  154. login_as :sam
  155. put :update, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :topic => { }
  156. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  157. end
  158. def test_should_update_other_post_as_admin
  159. login_as :admin
  160. put :update, :forum_id => forums(:rails).id, :id => topics(:ponies), :topic => { }
  161. assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
  162. end
  163. end