/test/functional/forums_controller_test.rb

https://github.com/assimovt/lovd-by-less · Ruby · 379 lines · 303 code · 57 blank · 19 comment · 28 complexity · b74ae8771dea5fa9c4f8f43787d53b9b MD5 · raw file

  1. ##
  2. # ForumsController tests
  3. # Author: Les Freeman (lesliefreeman3@gmail.com)
  4. #
  5. require 'test_helper'
  6. class ForumsControllerTest < ActionController::TestCase
  7. # include ForumsTestHelper
  8. ##
  9. # :index
  10. should "get the index as guest" do
  11. assert_nothing_raised do
  12. get :index, {}
  13. assert_response 200
  14. assert_template 'index'
  15. assert_no_tag :tag => 'a', :content => "Create a new forum"
  16. forums.each do |forum|
  17. assert_no_tag :tag => "a", :attributes => {:href => forum_url(forum), :class => "destroy"},
  18. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  19. assert_no_tag :tag => "a", :attributes => {:href => edit_forum_url(forum)},
  20. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  21. end
  22. end
  23. end
  24. should "get the index as :user" do
  25. assert_nothing_raised do
  26. get :index, {}, {:user => profiles(:user).id}
  27. assert_response 200
  28. assert_template 'index'
  29. assert_no_tag :tag => 'a', :content => "Create a new forum"
  30. forums.each do |forum|
  31. assert_no_tag :tag => "a", :attributes => {:href => forum_url(forum), :class => "destroy"},
  32. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  33. assert_no_tag :tag => "a", :attributes => {:href => edit_forum_url(forum)},
  34. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  35. end
  36. end
  37. end
  38. should "get the index as :admin" do
  39. assert_nothing_raised do
  40. get :index, {}, {:user => profiles(:admin).id}
  41. assert_response 200
  42. assert_template 'index'
  43. assert_tag :tag => 'a', :content => "Create a new forum"
  44. forums.each do |forum|
  45. assert_tag :tag => "a", :attributes => {:href => forum_url(forum), :class => "destroy"},
  46. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  47. assert_tag :tag => "a", :attributes => {:href => edit_forum_url(forum)},
  48. :parent => {:tag => "div", :attributes => {:id => dom_id(forum)}}
  49. end
  50. end
  51. end
  52. ##
  53. # :show
  54. should "get show as guest" do
  55. assert_nothing_raised do
  56. get :show, {:id => forums(:one)}
  57. assert_response 200
  58. assert_template 'show'
  59. assert_no_tag :tag => 'a', :attributes => {:id => "new_topic_link"}
  60. forums(:one).topics.each do |topic|
  61. assert_no_tag :tag => "a", :content => "Destroy"
  62. assert_no_tag :tag => "a", :content => "Edit"
  63. end
  64. end
  65. end
  66. should "get show as :user" do
  67. assert_nothing_raised do
  68. get :show, {:id => forums(:one)}, {:user => profiles(:user).id}
  69. assert_response 200
  70. assert_template 'show'
  71. assert_tag :tag => 'a', :attributes => {:id => "new_topic_link"}
  72. assert_no_tag :tag => "a", :attributes => {:href => signup_path},
  73. :ancestor => {:tag => "div", :attributes => {:class => "forum"}}
  74. assert_no_tag :tag => "a", :attributes => {:href => login_path},
  75. :ancestor => {:tag => "div", :attributes => {:class => "forum"}}
  76. forums(:one).topics.each do |topic|
  77. assert_no_tag :tag => "a", :content => "Destroy"
  78. assert_no_tag :tag => "a", :content => "Edit"
  79. end
  80. end
  81. end
  82. should "get show as :admin" do
  83. assert_nothing_raised do
  84. get :show, {:id => forums(:one)}, {:user => profiles(:admin).id}
  85. assert_response 200
  86. assert_template 'show'
  87. assert_no_tag :tag => "a", :attributes => {:href => signup_path},
  88. :ancestor => {:tag => "div", :attributes => {:class => "forum"}}
  89. assert_no_tag :tag => "a", :attributes => {:href => login_path},
  90. :ancestor => {:tag => "div", :attributes => {:class => "forum"}}
  91. assert_tag :tag => 'a', :attributes => {:id => "new_topic_link"}
  92. forums(:one).topics.each do |topic|
  93. assert_tag :tag => "a", :content => "Destroy"
  94. assert_tag :tag => "a", :content => "Edit"
  95. end
  96. end
  97. end
  98. ##
  99. # :new
  100. should "not get new for guest" do
  101. assert_nothing_raised do
  102. get :new, {}
  103. assert_response 302
  104. assert_redirected_to :login
  105. end
  106. end
  107. should "not get new for :user" do
  108. assert_nothing_raised do
  109. get :new, {}, {:user => profiles(:user).id}
  110. assert_response 302
  111. assert flash[:error]
  112. end
  113. end
  114. should "get new for :admin" do
  115. assert_nothing_raised do
  116. get :new, {}, {:user => profiles(:admin).id}
  117. assert_response 200
  118. assert_template 'new'
  119. end
  120. end
  121. ##
  122. # create
  123. should "not create a new forum for guest" do
  124. assert_nothing_raised do
  125. assert_no_difference "Forum.count" do
  126. post :create, {:forum => valid_forum_attributes}
  127. assert_response 302
  128. assert_redirected_to :login
  129. end
  130. end
  131. end
  132. should "not create a new forum for :user" do
  133. assert_nothing_raised do
  134. assert_no_difference "Forum.count" do
  135. post :create, {:forum => valid_forum_attributes}, {:user => profiles(:user).id}
  136. assert_response 302
  137. assert flash[:error]
  138. end
  139. end
  140. end
  141. should "create a new forum for :admin" do
  142. assert_nothing_raised do
  143. assert_difference "Forum.count" do
  144. post :create, {:forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  145. assert_redirected_to :controller => 'forums', :action => 'index'
  146. end
  147. end
  148. end
  149. should "not create a new forum for :user .js" do
  150. assert_nothing_raised do
  151. assert_no_difference "Forum.count" do
  152. post :create, {:format=>'js', :forum => valid_forum_attributes}, {:user => profiles(:user).id}
  153. assert_response 302
  154. end
  155. end
  156. end
  157. should "create a new forum for :admin .js" do
  158. assert_nothing_raised do
  159. assert_difference "Forum.count" do
  160. post :create, {:format=>'js', :forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  161. assert_response 200
  162. end
  163. end
  164. end
  165. should "not create a new forum for :user .xml" do
  166. assert_nothing_raised do
  167. assert_no_difference "Forum.count" do
  168. post :create, {:format=>'xml', :forum => valid_forum_attributes}, {:user => profiles(:user).id}
  169. assert_response 302
  170. end
  171. end
  172. end
  173. should "create a new forum for :admin .xml" do
  174. assert_nothing_raised do
  175. assert_difference "Forum.count" do
  176. post :create, {:format=>'xml', :forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  177. assert_response 200
  178. end
  179. end
  180. end
  181. ##
  182. # :edit
  183. should "not get edit for guest" do
  184. assert_nothing_raised do
  185. get :edit, {:id => forums(:one).id}
  186. assert_response 302
  187. assert_redirected_to :login
  188. end
  189. end
  190. should "not get edit for :user" do
  191. assert_nothing_raised do
  192. get :edit, {:id => forums(:one).id}, {:user => profiles(:user).id}
  193. assert_response 302
  194. assert flash[:error]
  195. end
  196. end
  197. should "get edit for :admin" do
  198. assert_nothing_raised do
  199. get :edit, {:id => forums(:one).id}, {:user => profiles(:admin).id}
  200. assert_response 200
  201. assert_template 'edit'
  202. end
  203. end
  204. ##
  205. # :update
  206. should "not update a forum for guest" do
  207. assert_nothing_raised do
  208. put :update, {:id => forums(:one).id, :forum => valid_forum_attributes}
  209. assert_response 302
  210. assert_redirected_to :login
  211. end
  212. end
  213. should "not update a forum for :user" do
  214. assert_nothing_raised do
  215. put :update, {:id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:user).id}
  216. assert_response 302
  217. assert flash[:error]
  218. end
  219. end
  220. should "update a forum for :admin" do
  221. assert_nothing_raised do
  222. put :update, {:id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  223. assert_redirected_to :controller => 'forums', :action => 'index' #, :id => forums(:one).to_param
  224. end
  225. end
  226. should "not update a forum for :admin" do
  227. assert_nothing_raised do
  228. put :update, {:id => forums(:one).id, :forum => unvalid_forum_attributes}, {:user => profiles(:admin).id}
  229. assert_response 200
  230. end
  231. end
  232. should "not update a forum for :user js" do
  233. assert_nothing_raised do
  234. put :update, {:format=>'js', :id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:user).id}
  235. assert_response 302
  236. end
  237. end
  238. should "update a forum for :admin js" do
  239. assert_nothing_raised do
  240. put :update, {:format=>'js', :id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  241. assert_response 200
  242. end
  243. end
  244. should "not update a forum for :admin js" do
  245. assert_nothing_raised do
  246. put :update, {:format=>'js', :id => forums(:one).id, :forum => unvalid_forum_attributes}, {:user => profiles(:admin).id}
  247. assert_response 200
  248. end
  249. end
  250. should "not update a forum for :user xml" do
  251. assert_nothing_raised do
  252. put :update, {:format=>'xml', :id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:user).id}
  253. assert_response 302
  254. end
  255. end
  256. should "update a forum for :admin xml" do
  257. assert_nothing_raised do
  258. put :update, {:format=>'xml', :id => forums(:one).id, :forum => valid_forum_attributes}, {:user => profiles(:admin).id}
  259. assert_response 200
  260. end
  261. end
  262. should "not update a forum for :admin xml" do
  263. assert_nothing_raised do
  264. put :update, {:format=>'xml', :id => forums(:one).id, :forum => unvalid_forum_attributes}, {:user => profiles(:admin).id}
  265. assert_response 422
  266. end
  267. end
  268. ##
  269. # :destroy
  270. should "not destroy a forum for guest" do
  271. assert_nothing_raised do
  272. assert_no_difference "Forum.count" do
  273. delete :destroy, {:id => forums(:one).id}
  274. assert_response 302
  275. assert_redirected_to :login
  276. end
  277. end
  278. end
  279. should "not destroy a forum for :user" do
  280. assert_nothing_raised do
  281. assert_no_difference "Forum.count" do
  282. delete :destroy, {:id => forums(:one).id}, {:user => profiles(:user).id}
  283. assert_response 302
  284. assert flash[:error]
  285. end
  286. end
  287. end
  288. should "destroy a forum for :admin" do
  289. assert_nothing_raised do
  290. assert_difference "Forum.count", -1 do
  291. delete :destroy, {:id => forums(:one).id}, {:user => profiles(:admin).id}
  292. assert_redirected_to :controller => 'forums', :action => 'index'
  293. end
  294. end
  295. end
  296. should "destroy a forum for :admin .js" do
  297. assert_nothing_raised do
  298. assert_difference "Forum.count", -1 do
  299. delete :destroy, {:format=>'js', :id => forums(:one).id}, {:user => profiles(:admin).id}
  300. assert_response 200
  301. end
  302. end
  303. end
  304. should "destroy a forum for :admin .xml" do
  305. assert_nothing_raised do
  306. assert_difference "Forum.count", -1 do
  307. delete :destroy, {:format=>'xml', :id => forums(:one).id}, {:user => profiles(:admin).id}
  308. assert_response 200
  309. end
  310. end
  311. end
  312. should "change the positions of the forums" do
  313. assert_no_difference "Forum.count" do
  314. assert_difference "Forum[forums(:one).id].position", 1 do
  315. assert_difference "Forum[forums(:two).id].position", -1 do
  316. post :update_positions, {:forums_list=>[forums(:two).id, forums(:one).id]}, {:user => profiles(:admin).id}
  317. assert_response 200
  318. end
  319. end
  320. end
  321. end
  322. end