PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/el-dorado/test/functional/topics_controller_test.rb

http://irkensrailsspace.googlecode.com/
Ruby | 331 lines | 266 code | 54 blank | 11 comment | 1 complexity | 245f6813a57ed2dfe53c3b286c630230 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. require 'topics_controller'
  3. # Re-raise errors caught by the controller.
  4. class TopicsController; def rescue_action(e) raise e end; end
  5. class TopicsControllerTest < Test::Unit::TestCase
  6. fixtures :all
  7. def setup
  8. @controller = TopicsController.new
  9. @request = ActionController::TestRequest.new
  10. @response = ActionController::TestResponse.new
  11. end
  12. def test_should_get_index
  13. get :index
  14. assert_response :success
  15. assert assigns(:topics)
  16. end
  17. def test_should_create_topic
  18. login_as :trevor
  19. old_topic_count = Topic.count
  20. old_post_count = Post.count
  21. post :create, :topic => { :title => "test topic create", :body => "this is a test for topic create", :forum_id => "1" }
  22. assert assigns(:topic)
  23. assert assigns(:post)
  24. assert_equal old_topic_count+1, Topic.count
  25. t = Topic.find(:first, :order => 'id desc')
  26. assert_equal t.user_id, users(:trevor).id
  27. assert_equal t.title, 'test topic create'
  28. assert_equal t.forum_id, 1
  29. assert_equal old_post_count+1, Post.count
  30. p = Post.find(:first, :order => 'id desc')
  31. assert_equal p.user_id, users(:trevor).id
  32. assert_equal p.topic_id, t.id
  33. assert_equal p.body, 'this is a test for topic create'
  34. assert_redirected_to topic_path(assigns(:topic))
  35. end
  36. def test_must_be_logged_in_to_post_topic
  37. old_count = Topic.count
  38. post :create, :topic => { :title => "test", :body => "this is a test" }
  39. assert_redirected_to login_path
  40. end
  41. def test_should_fail_to_get_new_if_not_logged_in
  42. get :new
  43. assert_redirected_to login_path
  44. end
  45. def test_should_get_new_if_logged_in
  46. login_as :trevor
  47. get :new
  48. assert_response :success
  49. end
  50. def test_should_show_topic
  51. get :show, :id => 1
  52. assert_response :success
  53. end
  54. def test_should_fail_to_get_edit_unless_user_created_topic
  55. get :edit, :id => 1
  56. assert_redirected_to login_path
  57. end
  58. def test_should_get_edit_if_user_created_topic
  59. login_as :trevor
  60. get :edit, :id => 1
  61. assert_response :success
  62. end
  63. def test_should_get_edit_if_admin
  64. login_as :Administrator
  65. get :edit, :id => 1
  66. assert_response :success
  67. end
  68. def test_should_fail_to_update_topic_if_not_authorized
  69. put :update, :id => 1, :topic => { :title => "bogus!"}
  70. assert_redirected_to login_path
  71. assert_equal "Testing", topics(:Testing).title
  72. end
  73. def test_should_update_topic_if_authorized
  74. login_as :trevor
  75. put :update, :id => 1, :topic => { :title => "ok!", :body => "ok!" }
  76. assert_redirected_to topic_path(assigns(:topic))
  77. assert_equal "ok!", topics(:Testing).title
  78. end
  79. def test_should_fail_to_update_topic_if_wrong_user
  80. login_as :Timothy
  81. put :update, :id => 1, :topic => { :title => "bogus!" }
  82. assert_redirected_to root_path
  83. assert_equal "Testing", topics(:Testing).title
  84. end
  85. def test_should_update_topic_if_admin
  86. login_as :Administrator
  87. put :update, :id => 1, :topic => { :title => "admin!" }
  88. assert_redirected_to topic_path(assigns(:topic))
  89. assert_equal "admin!", topics(:Testing).title
  90. end
  91. def test_should_fail_to_destroy_topic_if_not_authorized
  92. old_count = Topic.count
  93. delete :destroy, :id => 1
  94. assert_equal old_count, Topic.count
  95. assert_redirected_to login_path
  96. end
  97. def test_should_destroy_topic_if_user_that_created_topic
  98. login_as :trevor
  99. old_count = Topic.count
  100. delete :destroy, :id => 1
  101. assert_equal old_count-1, Topic.count
  102. assert_redirected_to topics_path
  103. end
  104. def test_should_fail_to_destroy_topic_if_wrong_user
  105. login_as :Timothy
  106. old_count = Topic.count
  107. delete :destroy, :id => 1
  108. assert_equal old_count, Topic.count
  109. assert_redirected_to root_path
  110. end
  111. def test_should_destroy_topic_if_admin
  112. login_as :Administrator
  113. old_count = Topic.count
  114. delete :destroy, :id => 1
  115. assert_equal old_count-1, Topic.count
  116. assert_redirected_to topics_path
  117. end
  118. def test_should_redirect_to_topic_with_viewtopic_php_style_url_with_post_id
  119. get :unknown_request, :path => "viewtopic.php", :pid => "2"
  120. assert_redirected_to topic_path(:id => "1")
  121. end
  122. def test_should_redirect_to_topic_with_other_viewtopic_php_style_url_with_topic_id
  123. get :unknown_request, :path => "viewtopic.php", :id => "1"
  124. assert_redirected_to topic_path(:id => "1")
  125. end
  126. def test_should_redirect_to_topic_with_empty_viewtopic_php_style_url
  127. get :unknown_request, :path => "viewtopic.php"
  128. assert_redirected_to root_path
  129. end
  130. def test_should_redirect_to_forum_with_viewforum_php_style_url
  131. get :unknown_request, :path => "viewforum.php", :id => "1"
  132. assert_redirected_to forum_path(:id => "1")
  133. end
  134. def test_should_redirect_to_forum_with_empty_viewforum_php_style_url
  135. get :unknown_request, :path => "viewforum.php"
  136. assert_redirected_to root_path
  137. end
  138. def test_should_redirect_to_topic_with_viewtopic_php_style_url_second_version
  139. end
  140. def test_should_redirect_to_root_path_if_viewtopic_id_not_found
  141. # get :unknown_request, :path => "viewtopic.php", :id => "23823"
  142. # assert_redirected_to topic_path(:id => "23823")
  143. # assert_redirected_to root_path
  144. end
  145. def test_should_set_last_post_info_in_forum_and_topic_on_new_topic
  146. end
  147. def test_should_set_last_post_info_in_forum_and_topic_on_new_post_in_existing_topic
  148. end
  149. def test_should_set_last_post_info_in_forum_and_topic_if_most_recent_post_is_deleted
  150. end
  151. def test_should_show_topic_as_new
  152. end
  153. def test_should_not_show_topic_as_new
  154. end
  155. def test_should_redirect_to_newest_post_with_show_new_action
  156. login_as :post_test
  157. get :show_new, :id => 1
  158. # assert_redirected_to topic_path(:id => "1", :anchor => 'p' + posts(:one2).id.to_s)
  159. end
  160. def test_should_redirect_to_last_post_if_no_new_posts_with_show_new_action
  161. login_as :trevor
  162. get :show_new, :id => 1
  163. # assert_redirected_to topic_path(:id => "1", :anchor => 'p' + posts(:one3).id.to_s)
  164. end
  165. def test_should_redirect_to_top_of_page_for_topic_with_no_anchor_if_first_post_in_thread
  166. login_as :trevor
  167. get :show_new, :id => 2
  168. assert_redirected_to topic_path(:id => "2")
  169. end
  170. def test_should_redirect_to_same_path_when_logging_in_with_cookie
  171. # log in
  172. # delete cookie
  173. # use show_new action, which should use the cookie to log in and redirect to the requested url
  174. # get :show_new, :id => 1
  175. # assert_redirected_to topic_path(:id => "1", :anchor => 'p' + posts(:one3).id.to_s)
  176. end
  177. def test_should_allow_post_if_logged_in
  178. login_as :trevor
  179. get :show, :id => 1
  180. assert_response :success
  181. assert_select "span#reply"
  182. end
  183. def test_should_not_allow_post_if_not_logged_in
  184. get :show, :id => 1
  185. assert_response :success
  186. assert_select "span#reply", false
  187. end
  188. def test_should_not_allow_post_if_logged_in_and_locked
  189. login_as :Timothy
  190. get :show, :id => 3
  191. assert_response :success
  192. assert_select "span#reply", false
  193. end
  194. def test_should_allow_post_if_logged_in_and_locked_but_is_admin
  195. login_as :Administrator
  196. get :show, :id => 3
  197. assert_response :success
  198. assert_select "span#reply"
  199. end
  200. def test_should_allow_post_if_logged_in_and_locked_but_is_topic_creator
  201. login_as :trevor
  202. get :show, :id => 3
  203. assert_response :success
  204. assert_select "span#reply"
  205. end
  206. def test_should_error_on_topic_create_if_attr_accessible_is_not_set_right_in_post_model
  207. end
  208. def test_should_error_on_topic_create_if_attr_accessor_is_not_set_right_in_post_model
  209. end
  210. def test_should_be_ok_with_bogus_params_page_value
  211. get :show, :id => 1, :page => 'sdlkfjsdfs'
  212. assert_redirected_to root_path
  213. assert_equal "Sorry, the page number you requested was not valid.", flash[:notice]
  214. end
  215. def test_should_redirect_to_first_page_if_page_is_too_many
  216. get :show, :id => 1, :page => '2'
  217. assert_redirected_to topic_path(:id => 1)
  218. end
  219. def test_should_not_show_topic_if_private_site_and_not_logged_in
  220. private_site
  221. get :show, :id => 1
  222. assert_redirected_to login_path
  223. end
  224. def test_should_show_topic_if_private_site_and_logged_in
  225. private_site
  226. login_as :trevor
  227. get :show, :id => 1
  228. assert_response :success
  229. end
  230. def test_should_update_cached_fields_when_moving_topics_between_forums
  231. login_as :Administrator
  232. assert_equal 2, topics(:moving).forum_id
  233. assert_equal 1, forums(:moving_from).topics_count
  234. assert_equal 1, forums(:moving_from).posts_count
  235. assert_equal 0, forums(:moving_to).topics_count
  236. assert_equal 0, forums(:moving_to).posts_count
  237. put :update, :id => 4, :topic => { :forum_id => 3 }
  238. topics(:moving).reload
  239. forums(:moving_from).reload
  240. forums(:moving_to).reload
  241. assert_redirected_to topic_path(topics(:moving))
  242. assert_equal 3, topics(:moving).forum_id
  243. assert_equal 0, forums(:moving_from).topics_count
  244. assert_equal 0, forums(:moving_from).posts_count
  245. assert_equal 1, forums(:moving_to).topics_count
  246. assert_equal 1, forums(:moving_to).posts_count
  247. end
  248. def test_should_update_cached_forum_fields_if_topic_destroyed
  249. login_as :Administrator
  250. assert_equal 1, forums(:moving_from).topics_count
  251. assert_equal 1, forums(:moving_from).posts_count
  252. delete :destroy, :id => 4
  253. forums(:moving_from).reload
  254. assert_equal 0, forums(:moving_from).topics_count
  255. assert_equal 0, forums(:moving_from).posts_count
  256. end
  257. def test_should_allow_admin_to_create_sticky_topic
  258. login_as :Administrator
  259. post :create, :topic => { :title => "test_sticky", :body => "body", :forum_id => "1", :sticky => true }
  260. assert_equal true, Topic.find_by_title('test_sticky').sticky
  261. end
  262. def test_should_allow_admin_to_make_topic_sticky_via_update
  263. login_as :Administrator
  264. put :update, :id => 1, :topic => { :sticky => true }
  265. assert_equal true, topics(:Testing).sticky
  266. end
  267. def test_should_not_allow_non_admin_to_create_sticky_topic
  268. login_as :trevor
  269. post :create, :topic => { :title => "test_sticky", :body => "body", :forum_id => "1", :sticky => true }
  270. assert_equal false, Topic.find_by_title('test_sticky').sticky
  271. end
  272. def test_should_not_allow_non_admin_to_make_topic_sticky_via_update
  273. login_as :trevor
  274. put :update, :id => 1, :topic => { :sticky => true }
  275. assert_equal false, topics(:Testing).sticky
  276. end
  277. end